Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.

refactor: data structure #223

Merged
merged 365 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
365 commits
Select commit Hold shift + click to select a range
ff858ff
refactor: simplified return values by combining them in a `ModuleData…
lukarade Aug 4, 2023
67f7ed5
feat: added detection of super classes
lukarade Aug 4, 2023
89472cc
feat: added detection walrus operator, corrected detection of functio…
lukarade Aug 4, 2023
ebd4210
Merge branch 'main' into resolve-references_new
lukarade Aug 4, 2023
baf1313
refactor: WIP make `get_symbols` traverse parent nodes recursively if…
Aug 5, 2023
195d80e
feat: (WIP) reworked `find_references`
Aug 6, 2023
f9f11c6
test: added testcases for call
lukarade Aug 9, 2023
3da7d59
feat: added collection of function calls (no reference detection yet)
lukarade Aug 9, 2023
829ec4c
refactor: merged `get_name_nodes` into `get_module_data` to remove on…
lukarade Aug 10, 2023
1c8bf20
feat: added detection of function calls and class instantiations
lukarade Aug 10, 2023
b817714
feat: added detection of builtin functions and functions shadowing bu…
lukarade Aug 10, 2023
73ca7c5
test: removed all builtin functions from other testcases (to simplify…
lukarade Aug 10, 2023
680ab88
cleanup
lukarade Aug 10, 2023
380240b
feat: added detection of 'InstanceVariables' and *vararg and **kwarg
lukarade Aug 11, 2023
7884f9b
Merge branch 'main' into resolve-references_new
lukarade Aug 11, 2023
a386178
feat: refined detection of 'InstanceVariables'
lukarade Aug 15, 2023
2897cbf
test: added cases using 'InstanceVariables'
lukarade Aug 15, 2023
8c847fe
fix: added typing and missing returns
lukarade Aug 15, 2023
aa22f65
fix: ruff linter fixes
lukarade Aug 15, 2023
777457a
test: added tests (9): chained `MemberAccess`, lambda functions, gene…
lukarade Aug 15, 2023
514fa27
test: added more lambda and function call testcases
lukarade Aug 16, 2023
9987a48
fix: linter fixes
lukarade Aug 16, 2023
5f7a27a
feat: added detection of chained `MemberAccess`
lukarade Aug 16, 2023
1aa7647
test: added ids to testcases, filled some missing result values
lukarade Aug 16, 2023
824ce31
test: added recursive call testcase
lukarade Aug 17, 2023
b6ec002
test: meeting discussions
lukarade Aug 18, 2023
e71cec2
feat: detect function usages, where the function is not called directly
lukarade Aug 18, 2023
a31c1be
feat: (WIP) lambda detection
lukarade Aug 18, 2023
2c41a95
feat: lambda detection
Aug 19, 2023
eeb4f1d
test: added tests and edited tests results
Aug 19, 2023
4ce2187
test: added 3 testcases for function calls
lukarade Aug 23, 2023
32ef05e
fix: fixed a bug where functions where handled as lambdas (causes by …
lukarade Aug 23, 2023
4b6b404
test: corrected testdata to fit our requirements
lukarade Aug 30, 2023
db40bbe
test: restructured the tests
lukarade Aug 30, 2023
8ee8c9a
feat: detect parameters used as function calls
lukarade Aug 30, 2023
9c18876
fix: detection of member access expressions
lukarade Aug 31, 2023
bbdd537
fix: removed duplicated line
lukarade Aug 31, 2023
9accfec
backup with meeting discussions
lukarade Aug 31, 2023
58f21e5
Merge branch 'main' into resolve-references_new
lukarade Aug 31, 2023
807dfa6
refactor: split resolve reference into three separate classes
lukarade Aug 31, 2023
e1216c2
refactor: (WIP) get symbols at scope detection
lukarade Sep 1, 2023
97a15d9
refactor: separated test files
lukarade Sep 4, 2023
23fcbda
refactor: (WIP) get symbols at scope detection
lukarade Sep 4, 2023
6b81b24
feat: get symbols at scope detection
lukarade Sep 4, 2023
a594ba7
refactor: reworked generation of `MemberAccess` (now fits the common …
lukarade Sep 4, 2023
ed21549
refactor: sorted remaining testcases
lukarade Sep 5, 2023
954a92d
refactor: added value and target nodes to `ScopeFinder` and `ModuleDa…
lukarade Sep 6, 2023
87d9f6a
refactor: implemented all previous finished features with new data st…
lukarade Sep 7, 2023
76505b3
refactor: cleanup - removed legacy code
lukarade Sep 7, 2023
18b6a63
test: added testcases for call detection
lukarade Sep 7, 2023
277afea
feat: functions are now stored in a list if there is more than one fu…
lukarade Sep 8, 2023
a09ecf1
fix: miscellaneous fixes
lukarade Sep 8, 2023
3155613
test: new testcases for call detection, added expected results
lukarade Sep 8, 2023
ca5186c
feat: functions with the same name are now all referenced as potentia…
lukarade Sep 8, 2023
e9a635d
Merge branch 'main' into resolve-references_symbol_rework
lukarade Sep 8, 2023
941c769
fix: fixed a bug where Parameters where detected when dealing with In…
Sep 9, 2023
06dac7b
feat: symbols for class/instance variables are now always detected as…
lukarade Sep 10, 2023
417846b
docs: added documentation
lukarade Sep 11, 2023
e96a5a4
fix: Parameters are now only detected when the value node is in the s…
lukarade Sep 11, 2023
5da5ce6
fix: Names other than Parameters are no longer detected if they have …
lukarade Sep 11, 2023
d3ef02e
fix: fixed a bug where ids for function defs were not correctly calcu…
lukarade Sep 11, 2023
ac08f90
test: added tests for `ListComprehensions`
lukarade Sep 11, 2023
b7e604a
feat: added Scope detection for `ListComprehensions`
lukarade Sep 11, 2023
265e053
feat: resolve references for `ListComprehensions`
lukarade Sep 12, 2023
e02cd33
feat: (WIP) scope detection for TryExcept and TryExceptFinally
lukarade Sep 12, 2023
b66cf65
test: added testcases for `test_get_module_data_classes`
lukarade Sep 12, 2023
b6ffa74
fix: function defs and class defs within class scope are now added to…
lukarade Sep 12, 2023
f9fb13a
fix: linter fixes
lukarade Sep 12, 2023
f6afece
fix: Meeting discussions (new Todos)
lukarade Sep 13, 2023
f823aa0
fix: FunctionDefs are now always stored in a list, no matter how ofte…
lukarade Sep 13, 2023
e83a777
refactor: reworked `analyse_constructor` so it can analyse the constr…
lukarade Sep 13, 2023
a71558b
refactor: restructured anything regarding Purity into api/purity_anal…
lukarade Sep 13, 2023
a0d79ee
refactor: reworked detection of class variables. Now detects all clas…
lukarade Sep 13, 2023
2c55aac
test: added testcases for With scope
lukarade Sep 13, 2023
7d47731
feat: added scope detection for With nodes
lukarade Sep 13, 2023
6f87783
feat: (WIP) resolve reference for target nodes
lukarade Sep 15, 2023
b78076d
feat: (WIP) resolve reference for target nodes
lukarade Sep 26, 2023
e814dc3
feat: resolve reference for target nodes
lukarade Sep 27, 2023
8288195
Merge branch 'main' into resolve-references_new
lukarade Sep 27, 2023
2cc3fb3
feat: only detect own class for "self" receivers
lukarade Sep 27, 2023
3cf2133
fix: cleanup and ruff fixes
lukarade Sep 27, 2023
e4cc1c4
fix: mypy fixes
lukarade Sep 27, 2023
d41b86f
fix: mypy fixes
lukarade Sep 27, 2023
633ed45
fix: mypy fixes
lukarade Sep 27, 2023
1af2295
fix: removed unfinished tests
lukarade Sep 28, 2023
7a6fe4d
fix: linter fixes
lukarade Sep 28, 2023
910485a
fix: codecov coverage
lukarade Sep 28, 2023
181da9d
style: apply automated linter fixes
megalinter-bot Sep 28, 2023
ba16338
style: apply automated linter fixes
megalinter-bot Sep 28, 2023
3f450d8
fix: circular import
lukarade Sep 28, 2023
7bb26d8
fix: fixed documentation parsing error
Oct 2, 2023
b8896e0
Merge branch 'main' into resolve-references_new
Oct 2, 2023
89d8def
fix: fixed documentation parsing error
Oct 2, 2023
813a176
fix: fixed documentation parsing error
Oct 2, 2023
71e347c
fix: moved dataclasses to model
Oct 2, 2023
cfb33c2
test: added basic testcases for purity
Oct 2, 2023
46e44a1
fix: assignments on global scope are now detected as global variables
lukarade Oct 9, 2023
e15d747
feat: added `FunctionScope` to store value nodes of functions
lukarade Oct 9, 2023
03cd699
test: add testcase for function_references
lukarade Oct 9, 2023
5b59332
feat: collect all name nodes (target, value, call) inside a function
lukarade Oct 9, 2023
5ccc672
feat: add `FunctionReference` to simplify the handling of function re…
lukarade Oct 10, 2023
cd009c5
Merge branch 'main' into purity-analysis
lukarade Oct 10, 2023
e400a38
fix: merge conflicts and edge cases
lukarade Oct 10, 2023
0c2e63d
fix: added `module_data.function_references` as return for `resolve_r…
lukarade Oct 11, 2023
230d909
refactor: replaced deprecated FunctionID with `NodeID`
lukarade Oct 11, 2023
5213bb3
test: added testcases for builtins
lukarade Oct 11, 2023
47fec63
feat: finer detection of Write and Read/ handling of builtins
lukarade Oct 11, 2023
173dbd7
test: removed old testcases and added new ones
lukarade Oct 12, 2023
682f86f
feat: adapted Purity data model to fit our new definitions
lukarade Oct 12, 2023
4783cef
feat: added purity inference for simple pure functions
lukarade Oct 12, 2023
1cf5c5f
(WIP) weekend commit
lukarade Oct 13, 2023
3e5b27e
feat: Added `Reasons` class: later performance boost, by separating c…
lukarade Oct 16, 2023
8a7f7ef
feat: added caching of pure functions to `ìnfer_purity` + testcases
lukarade Oct 16, 2023
cff2ff2
feat: Add purity detection for builtins (pure ones)
lukarade Oct 16, 2023
4dca2e3
feat: (WIP) build call graph
lukarade Oct 27, 2023
0ef835a
Merge branch 'main' into purity-analysis
lukarade Oct 27, 2023
99af371
Merge branch 'main' into purity-analysis
lukarade Nov 8, 2023
c61f57f
feat: build call graph
lukarade Nov 9, 2023
3ff2e12
feat: find cycles
lukarade Nov 9, 2023
77445cf
meeting discussions
lukarade Nov 10, 2023
511e57d
fix: Uppercase comments
lukarade Nov 15, 2023
b44a17f
feat: contract cycles
lukarade Nov 16, 2023
fe789fc
fix: fixed a bug where the data of all functions where overwritten wh…
lukarade Nov 16, 2023
1f0ae34
feat: build call graph for functions with same name
lukarade Nov 16, 2023
1bfd813
feat: added reasons to call graph forest
lukarade Nov 16, 2023
093b786
feat: (WIP) infer purity using call graph
lukarade Nov 17, 2023
c61fa5c
feat: (WIP) infer purity using call graph
lukarade Nov 21, 2023
f7d1208
feat: (WIP) propagate purity for nested function calls and resolve pu…
lukarade Nov 22, 2023
85b876e
fix: adapted purity terminologie to requirements
lukarade Nov 22, 2023
02041d9
fix: builtins within cycles are now handled correctly
lukarade Nov 22, 2023
44fe007
feat: return PurityResult for Impure functions (and removed old code)
lukarade Nov 22, 2023
1bbd3ec
test: added testcases for impurity
lukarade Nov 23, 2023
9e81e6a
feat: (WIP) No longer need an "entry" call to start purity analysis, …
lukarade Nov 24, 2023
02bd5e9
feat: No longer need an "entry" call to start purity analysis, correc…
lukarade Nov 28, 2023
312a3d5
test: added tests for open like functions
lukarade Nov 29, 2023
4cbeac1
feat: infer purity for special case: open()
lukarade Nov 30, 2023
a50b548
test: added more testcases for impurity
lukarade Nov 30, 2023
9468302
fix: spelling fixes
lukarade Nov 30, 2023
00eb34a
Merge branch 'main' into purity-analysis
lukarade Nov 30, 2023
3f22119
feat: removed With scope to better match python's scoping
lukarade Nov 30, 2023
313dcb7
feat: infer purity for all open() cases correctly
lukarade Nov 30, 2023
93954cd
feat: infer pure lambda functions
lukarade Nov 30, 2023
17a7cb3
feat: infer impure lambda functions
lukarade Nov 30, 2023
d90fb93
feat: infer unknown calls
lukarade Dec 1, 2023
714c1c9
feat: (WIP) infer impurity for cycles
lukarade Dec 1, 2023
2df6447
refactor: created a function to get impurity of a child node
lukarade Dec 1, 2023
64baba3
Merge branch 'main' into purity-analysis
lukarade Dec 1, 2023
bbe0863
feat: add correct module to combined nodes
lukarade Dec 18, 2023
d4d2f9f
feat: added `UnknownCall` class as subclass of `Unknown`
lukarade Dec 18, 2023
06a72ae
Merge branch 'main' into purity-analysis
lukarade Dec 18, 2023
ce6c77b
fix: ruff linter fixes
lukarade Dec 18, 2023
5bfa414
fix: propagate class info to infer_purity to later find class instant…
lukarade Dec 19, 2023
f3831b0
test: removed all failing testcases - they will need new features
lukarade Dec 19, 2023
7382f42
test: removed all failing testcases - they will need new features
lukarade Dec 19, 2023
6710651
fix: refactored resolve_references to return a dict
lukarade Dec 19, 2023
e788ae5
Merge remote-tracking branch 'origin/purity-analysis' into purity-ana…
lukarade Dec 19, 2023
8e7f814
fix: mypy linter fixes
lukarade Dec 19, 2023
0966d0d
fix: fixed a bug (references with the same name were lost due to the …
lukarade Dec 20, 2023
1bcddbb
fix: linter fixes
lukarade Dec 21, 2023
2d35422
fix: more linter fixes
lukarade Dec 21, 2023
4411cde
fix: more linter fixes
lukarade Dec 21, 2023
e4a1637
fix: more linter fixes
lukarade Dec 21, 2023
5e9908b
fix: fixed infer_purity type errors
lukarade Dec 21, 2023
0d2bf76
fix: fixed infer_purity type errors
lukarade Dec 21, 2023
0b4c2e4
fix: fixed infer_purity type errors
lukarade Dec 22, 2023
d3bfa76
fix: fixed infer_purity type errors
lukarade Dec 22, 2023
13bb0a6
fix: fixed infer_purity type errors
lukarade Dec 22, 2023
be4048d
fix: final linter fixes
lukarade Dec 22, 2023
b4bead8
fix: docs
lukarade Dec 22, 2023
5dace54
style: apply automated linter fixes
megalinter-bot Dec 22, 2023
7bab4c7
fix: automated fixes fixed
lukarade Dec 22, 2023
fa21d97
style: apply automated linter fixes
megalinter-bot Dec 22, 2023
1278de8
feat: get module data for member access (target & value)
lukarade Dec 30, 2023
690bd8d
feat: collect calls and class inits from chained member accesses
lukarade Dec 30, 2023
6f6c557
fix: refactor `collect_function_references` to return a dict
lukarade Dec 30, 2023
7da1ad3
feat: `remove_class_method_calls_from_reads` if they are duplicates f…
lukarade Dec 30, 2023
1d78910
fix: (WIP) deal with functions with the same name
lukarade Dec 30, 2023
4925598
fix: new docstring style _scope.py
lukarade Dec 31, 2023
a17b317
fix: new docstring style _reference.py
lukarade Dec 31, 2023
4844b5d
fix: fixed ":" spacing in all docstrings
lukarade Dec 31, 2023
6db6fde
fix: new docstring style _purity.py
lukarade Dec 31, 2023
89b3a7a
style: apply automated linter fixes
megalinter-bot Jan 1, 2024
f673e9b
fix: new docstring style _get_module_data.py
lukarade Jan 1, 2024
2725d3b
Merge remote-tracking branch 'origin/purity-analysis-docs-remastered'…
lukarade Jan 1, 2024
94357a9
fix: new docstring style _resolve_references.py
lukarade Jan 2, 2024
18bc102
fix: new docstring style _build_call_graph.py
lukarade Jan 2, 2024
c024cf7
fix: (WIP) new docstring style _infer_purity.py - needs refactoring o…
lukarade Jan 2, 2024
a1f1e27
fix: new docstring style for tests
lukarade Jan 2, 2024
09a648a
style: apply automated linter fixes
megalinter-bot Jan 2, 2024
2771aaf
Merge branch 'main' into purity-analysis-docs-remastered
lukarade Jan 2, 2024
e251ef6
Merge branch 'purity-analysis-docs-remastered' into purity-analysis-m…
lukarade Jan 3, 2024
d325c49
fix: (WIP) fixed analysis to deal with functions with the same name c…
lukarade Jan 4, 2024
44fece8
fix: added class `ModuleAnalysisResult` which serves as a return type…
lukarade Jan 4, 2024
5cdbdca
fix: fixed analysis to deal with functions with the same name correctly
lukarade Jan 5, 2024
2cabb4f
test: added test for `FunctionScope`, adapted all other cases to use …
lukarade Jan 6, 2024
2db3efb
test: added member access test for built_call_graph
lukarade Jan 9, 2024
d8edc8b
test: added member access test for resolve_references
lukarade Jan 9, 2024
436fe22
test: added member access test for infer_purity
lukarade Jan 9, 2024
f33a22a
feat: added resolve reference for member access and fixed a bug with …
lukarade Jan 10, 2024
a04d938
feat: added resolve reference for member access and fixed a bug with …
lukarade Jan 10, 2024
4becb13
Merge remote-tracking branch 'origin/purity-analysis-member-access' i…
lukarade Jan 10, 2024
35049b2
feat: added built_call_graph for member access
lukarade Jan 11, 2024
935f400
fix: meeting discussions and TODOS
lukarade Jan 11, 2024
bbfcca4
Merge branch 'main' into purity-analysis-member-access
lukarade Jan 12, 2024
21b4d17
feat: infer_purity for member accesses
lukarade Jan 19, 2024
63df946
feat: infer CallOfParameter
lukarade Jan 19, 2024
604b4ca
fix: added separate test folder for purity analysis
lukarade Jan 19, 2024
fdce5d4
test: added case for unknown import
lukarade Feb 5, 2024
abc29e9
fix: added bugfix for lambda function detection
lukarade Feb 6, 2024
3264990
fix: (WIP) added bugfix for detection of global variables in functions
lukarade Feb 7, 2024
81cd9ee
feat: detect global variables in local scopes (ListComp, SetComp, Dic…
lukarade Feb 11, 2024
383802f
Merge branch 'main' into purity-analysis-member-access
lukarade Feb 11, 2024
72aac00
fix: changed FunctionScope calls and values lists to dict to remove d…
lukarade Feb 13, 2024
b6c9cc7
fix: fixed data structure so all value nodes are saved
lukarade Feb 14, 2024
8080f83
fix: removed FunctionReference completely
lukarade Feb 14, 2024
d670625
fix: removed FunctionReference completely
lukarade Feb 14, 2024
06a44d7
Merge remote-tracking branch 'origin/purity-analysis-member-access' i…
lukarade Feb 15, 2024
0f280d7
fix: now uses `Reference` to represent nodes that reference a `Symbol…
lukarade Feb 16, 2024
2dafb77
feat: added `target_symbols` to `FunctionScope` and added detection o…
lukarade Feb 17, 2024
c532178
fix: made infer_purity the only function to call to start the whole a…
lukarade Feb 17, 2024
77ff7b6
fix: moved `function_references` from ModuleData to ResolveReferences…
lukarade Feb 17, 2024
3487ebc
feat: completely reworked resolve_references to address all sorts of …
lukarade Feb 21, 2024
8e240b9
feat: implemented `infer_purity` to the cli command and adjusted all …
lukarade Feb 21, 2024
e2fa4f8
feat: (WIP) completely reworked the collection of `Reasons` and made …
lukarade Feb 22, 2024
8d9b2c2
fix: cleanup and divided data classes for better separation
lukarade Feb 22, 2024
c1fb09f
fix: moved Reasons class to _reference.py
lukarade Feb 24, 2024
611bcea
fix: resolve_references cleanup
lukarade Feb 24, 2024
8f12308
fix: changes to generation of MemberAccess
lukarade Feb 25, 2024
f7b6e29
feat: use new Reasons in infer_purity
lukarade Feb 25, 2024
9971db0
fix: removed purity command from cli command to merge to main safely
lukarade Feb 25, 2024
6876e46
fix: infer_purity returns NodeID instead of FunctionDef
lukarade Feb 25, 2024
e35e8e2
fix: linter fixes and cleanup
lukarade Feb 25, 2024
0f9bd73
fix: ruff fixes and mypy fixes for tests
lukarade Feb 26, 2024
69e2f4d
fix: no longer use Reference instances to represent builtins in the c…
lukarade Feb 26, 2024
dc753da
fix: mypy linter fixes
lukarade Feb 26, 2024
1bf8143
fix: mypy linter fixes
lukarade Feb 27, 2024
407288f
fix: reworked detection of open-like functions / more linter fixes
lukarade Feb 27, 2024
f2749f2
fix: final linter fixes
lukarade Feb 27, 2024
47ce744
fix: final linter fixes
lukarade Feb 27, 2024
31ad233
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
ad451af
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
0b1c1c8
fix: codecov improved
lukarade Feb 27, 2024
fb79411
style: apply automated linter fixes
megalinter-bot Feb 27, 2024
5955549
fix: requested changes from review (removed all unnecessary code, nam…
lukarade Feb 28, 2024
7064720
fix: more requested changes
lukarade Feb 28, 2024
fc62e94
fix: linter fixes
lukarade Feb 29, 2024
8c19c1b
fix: last linter fixes
lukarade Feb 29, 2024
8f3d5b5
fix: last linter fixes
lukarade Feb 29, 2024
be19a50
style: apply automated linter fixes
megalinter-bot Feb 29, 2024
00bd28c
style: apply automated linter fixes
megalinter-bot Feb 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/library_analyzer/cli/_run_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _run_api_command(
out_dir_path : Path
The path to the output directory.
docstring_style : DocstringStyle
The style of docstrings that used in the library.
The style of docstrings that is used in the library.
"""
api = get_api(package, src_dir_path, docstring_style)
out_file_api = out_dir_path.joinpath(f"{package}__api.json")
Expand All @@ -32,5 +32,3 @@ def _run_api_command(
api_dependencies = get_dependencies(api)
out_file_api_dependencies = out_dir_path.joinpath(f"{package}__api_dependencies.json")
api_dependencies.to_json_file(out_file_api_dependencies)

# TODO: call resolve_references here

Large diffs are not rendered by default.

988 changes: 589 additions & 399 deletions src/library_analyzer/processing/api/purity_analysis/_get_module_data.py

Large diffs are not rendered by default.

488 changes: 239 additions & 249 deletions src/library_analyzer/processing/api/purity_analysis/_infer_purity.py

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
"""Data model for purity analysis."""

from library_analyzer.processing.api.purity_analysis.model._purity import (
CallOfParameter,
Expression,
FileRead,
FileWrite,
Impure,
ImpurityReason,
NativeCall,
NonLocalVariableRead,
NonLocalVariableWrite,
OpenMode,
ParameterAccess,
Pure,
PurityResult,
StringLiteral,
UnknownCall,
)
from library_analyzer.processing.api.purity_analysis.model._reference import (
from library_analyzer.processing.api.purity_analysis.model._call_graph import (
CallGraphForest,
CallGraphNode,
ReferenceNode,
)
from library_analyzer.processing.api.purity_analysis.model._scope import (
from library_analyzer.processing.api.purity_analysis.model._module_data import (
Builtin,
BuiltinOpen,
ClassScope,
ClassVariable,
FunctionReference,
FunctionScope,
GlobalVariable,
Import,
Expand All @@ -38,17 +20,42 @@
ModuleData,
NodeID,
Parameter,
Reasons,
Reference,
Scope,
Symbol,
)
from library_analyzer.processing.api.purity_analysis.model._purity import (
APIPurity,
CallOfParameter,
Expression,
FileRead,
FileWrite,
Impure,
ImpurityReason,
NativeCall,
NonLocalVariableRead,
NonLocalVariableWrite,
OpenMode,
ParameterAccess,
Pure,
PurityResult,
StringLiteral,
UnknownCall,
)
from library_analyzer.processing.api.purity_analysis.model._reference import (
ModuleAnalysisResult,
Reasons,
ReferenceNode,
TargetReference,
ValueReference,
)

__all__ = [
"ModuleAnalysisResult",
"ModuleData",
"Scope",
"ClassScope",
"FunctionScope",
"FunctionReference",
"MemberAccess",
"MemberAccessTarget",
"MemberAccessValue",
Expand Down Expand Up @@ -80,4 +87,9 @@
"NativeCall",
"UnknownCall",
"CallOfParameter",
"Reference",
"TargetReference",
"ValueReference",
"APIPurity",
"BuiltinOpen",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from library_analyzer.processing.api.purity_analysis.model._module_data import (
ClassScope,
FunctionScope,
NodeID,
)
from library_analyzer.processing.api.purity_analysis.model._reference import Reasons


@dataclass
class CallGraphNode:
"""Class for call graph nodes.

A call graph node represents a function in the call graph.

Attributes
----------
scope : FunctionScope | ClassScope
The function that the node represents.
This is a ClassScope if the class has a __init__ method.
In this case, the node is used for propagating the reasons of the
__init__ method to function calling the class.
reasons : Reasons
The raw Reasons for the node.
children : set[CallGraphNode]
The set of children of the node, (i.e., the set of nodes that this node calls)
combined_node_ids : list[NodeID]
A list of the names of all nodes that are combined into this node.
This is only set if the node is a combined node.
This is later used for transferring the reasons of the combined node to the original nodes.
is_builtin : bool
True if the function is a builtin function, False otherwise.
"""

scope: FunctionScope | ClassScope # TODO: change to symbol
reasons: (
Reasons # TODO: remove calls from reasons after they were added to the call graph (except for unknown calls)
)
children: set[CallGraphNode] = field(default_factory=set)
combined_node_ids: list[NodeID] = field(default_factory=list)
is_builtin: bool = False

def __hash__(self) -> int:
return hash(str(self))

def __repr__(self) -> str:
return f"{self.scope.symbol.id}"

def add_child(self, child: CallGraphNode) -> None:
"""Add a child to the node.

Parameters
----------
child : CallGraphNode
The child to add.
"""
self.children.add(child)

def is_leaf(self) -> bool:
"""Check if the node is a leaf node.

Returns
-------
bool
True if the node is a leaf node, False otherwise.
"""
return len(self.children) == 0

def combined_node_id_to_string(self) -> list[str]:
"""Return the combined node IDs as a string.

Returns
-------
str
The combined node IDs as a string.
"""
return [str(node_id) for node_id in self.combined_node_ids]


@dataclass
class CallGraphForest:
"""Class for call graph forests.

A call graph forest represents a collection of call graph trees.

Attributes
----------
graphs : dict[str, CallGraphNode]
The dictionary of call graph trees.
The key is the name of the tree, the value is the root CallGraphNode of the tree.
"""

graphs: dict[NodeID, CallGraphNode] = field(default_factory=dict)

def add_graph(self, graph_id: NodeID, graph: CallGraphNode) -> None:
"""Add a call graph tree to the forest.

Parameters
----------
graph_id : NodeID
The NodeID of the tree node.
graph : CallGraphNode
The root of the tree.
"""
self.graphs[graph_id] = graph

def get_graph(self, graph_id: NodeID) -> CallGraphNode:
"""Get a call graph tree from the forest.

Parameters
----------
graph_id : NodeID
The NodeID of the tree node to get.

Returns
-------
CallGraphNode
The CallGraphNode that is the root of the tree.

Raises
------
KeyError
If the graph_id is not in the forest.
"""
result = self.graphs.get(graph_id)
if result is None:
raise KeyError(f"Graph with id {graph_id} not found inside the call graph.")

Check warning on line 132 in src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py

View check run for this annotation

Codecov / codecov/patch

src/library_analyzer/processing/api/purity_analysis/model/_call_graph.py#L132

Added line #L132 was not covered by tests
return result

def has_graph(self, graph_id: NodeID) -> bool:
"""Check if the forest contains a call graph tree with the given NodeID.

Parameters
----------
graph_id : NodeID
The NodeID of the tree to check for.

Returns
-------
bool
True if the forest contains a tree with the given NodeID, False otherwise.
"""
return graph_id in self.graphs

def delete_graph(self, graph_id: NodeID) -> None:
"""Delete a call graph tree from the forest.

Parameters
----------
graph_id : NodeID
The NodeID of the tree to delete.
"""
del self.graphs[graph_id]
Loading
Loading