mapFolding 0.9.5__py3-none-any.whl → 0.11.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mapFolding/__init__.py +29 -27
- mapFolding/someAssemblyRequired/Z0Z_makeSomeModules.py +44 -45
- mapFolding/someAssemblyRequired/__init__.py +17 -35
- mapFolding/someAssemblyRequired/_toolIfThis.py +53 -0
- mapFolding/someAssemblyRequired/_toolboxContainers.py +15 -287
- mapFolding/someAssemblyRequired/makeJobTheorem2Numba.py +14 -14
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +13 -13
- mapFolding/someAssemblyRequired/toolboxNumba.py +1 -1
- mapFolding/someAssemblyRequired/transformationTools.py +25 -195
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/METADATA +3 -1
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/RECORD +15 -20
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/WHEEL +1 -1
- mapFolding/someAssemblyRequired/_theTypes.py +0 -57
- mapFolding/someAssemblyRequired/_tool_Make.py +0 -132
- mapFolding/someAssemblyRequired/_tool_Then.py +0 -152
- mapFolding/someAssemblyRequired/_toolboxAST.py +0 -57
- mapFolding/someAssemblyRequired/_toolboxAntecedents.py +0 -404
- mapFolding/someAssemblyRequired/_toolboxPython.py +0 -188
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/entry_points.txt +0 -0
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/licenses/LICENSE +0 -0
- {mapfolding-0.9.5.dist-info → mapfolding-0.11.0.dist-info}/top_level.txt +0 -0
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
AST Node Transformation Actions for Python Code Manipulation
|
|
3
|
-
|
|
4
|
-
This module provides the Then class with static methods for generating callable action functions that specify what to do
|
|
5
|
-
with AST nodes that match predicates. These action functions are used primarily with NodeChanger and NodeTourist to
|
|
6
|
-
transform or extract information from AST nodes.
|
|
7
|
-
|
|
8
|
-
The module also contains the grab class that provides functions for modifying specific attributes of AST nodes while
|
|
9
|
-
preserving their structure, enabling fine-grained control when transforming AST structures.
|
|
10
|
-
|
|
11
|
-
Together, these classes provide a complete system for manipulating AST nodes once they have been identified using
|
|
12
|
-
predicate functions from ifThis.
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
from collections.abc import Callable, Sequence
|
|
16
|
-
from mapFolding.someAssemblyRequired import ast_Identifier, astClassHasDOTvalue, ImaCallToName, NodeORattribute
|
|
17
|
-
from typing import Any
|
|
18
|
-
import ast
|
|
19
|
-
|
|
20
|
-
class grab:
|
|
21
|
-
"""
|
|
22
|
-
Modify specific attributes of AST nodes while preserving the node structure.
|
|
23
|
-
|
|
24
|
-
The grab class provides static methods that create transformation functions to modify specific attributes of AST
|
|
25
|
-
nodes. Unlike DOT which provides read-only access, grab allows for targeted modifications of node attributes without
|
|
26
|
-
replacing the entire node.
|
|
27
|
-
|
|
28
|
-
Each method returns a function that takes a node, applies a transformation to a specific attribute of that node, and
|
|
29
|
-
returns the modified node. This enables fine-grained control when transforming AST structures.
|
|
30
|
-
"""
|
|
31
|
-
@staticmethod
|
|
32
|
-
def andDoAllOf(listOfActions: list[Callable[[NodeORattribute], NodeORattribute]]) -> Callable[[NodeORattribute], NodeORattribute]:
|
|
33
|
-
def workhorse(node: NodeORattribute) -> NodeORattribute:
|
|
34
|
-
for action in listOfActions:
|
|
35
|
-
node = action(node)
|
|
36
|
-
return node
|
|
37
|
-
return workhorse
|
|
38
|
-
|
|
39
|
-
@staticmethod
|
|
40
|
-
def argAttribute(action: Callable[[ast_Identifier | None], ast_Identifier]) -> Callable[[ast.arg | ast.keyword], ast.arg | ast.keyword]:
|
|
41
|
-
def workhorse(node: ast.arg | ast.keyword) -> ast.arg | ast.keyword:
|
|
42
|
-
node.arg = action(node.arg)
|
|
43
|
-
return node
|
|
44
|
-
return workhorse
|
|
45
|
-
|
|
46
|
-
@staticmethod
|
|
47
|
-
def attrAttribute(action: Callable[[ast_Identifier], ast_Identifier]) -> Callable[[ast.Attribute], ast.Attribute]:
|
|
48
|
-
def workhorse(node: ast.Attribute) -> ast.Attribute:
|
|
49
|
-
node.attr = action(node.attr)
|
|
50
|
-
return node
|
|
51
|
-
return workhorse
|
|
52
|
-
|
|
53
|
-
@staticmethod
|
|
54
|
-
def comparatorsAttribute(action: Callable[[list[ast.expr]], list[ast.expr]]) -> Callable[[ast.Compare], ast.Compare]:
|
|
55
|
-
def workhorse(node: ast.Compare) -> ast.Compare:
|
|
56
|
-
node.comparators = action(node.comparators)
|
|
57
|
-
return node
|
|
58
|
-
return workhorse
|
|
59
|
-
|
|
60
|
-
@staticmethod
|
|
61
|
-
def funcAttribute(action: Callable[[ast.expr], ast.expr]) -> Callable[[ast.Call], ast.Call]:
|
|
62
|
-
def workhorse(node: ast.Call) -> ast.Call:
|
|
63
|
-
node.func = action(node.func)
|
|
64
|
-
return node
|
|
65
|
-
return workhorse
|
|
66
|
-
|
|
67
|
-
@staticmethod
|
|
68
|
-
def funcDOTidAttribute(action: Callable[[ast_Identifier], Any]) -> Callable[[ImaCallToName], ImaCallToName]:
|
|
69
|
-
def workhorse(node: ImaCallToName) -> ImaCallToName:
|
|
70
|
-
node.func = grab.idAttribute(action)(node.func)
|
|
71
|
-
return node
|
|
72
|
-
return workhorse
|
|
73
|
-
|
|
74
|
-
@staticmethod
|
|
75
|
-
def idAttribute(action: Callable[[ast_Identifier], ast_Identifier]) -> Callable[[ast.Name], ast.Name]:
|
|
76
|
-
def workhorse(node: ast.Name) -> ast.Name:
|
|
77
|
-
node.id = action(node.id)
|
|
78
|
-
return node
|
|
79
|
-
return workhorse
|
|
80
|
-
|
|
81
|
-
@staticmethod
|
|
82
|
-
def leftAttribute(action: Callable[[ast.expr], ast.expr]) -> Callable[[ast.BinOp | ast.Compare], ast.BinOp | ast.Compare]:
|
|
83
|
-
def workhorse(node: ast.BinOp | ast.Compare) -> ast.BinOp | ast.Compare:
|
|
84
|
-
node.left = action(node.left)
|
|
85
|
-
return node
|
|
86
|
-
return workhorse
|
|
87
|
-
|
|
88
|
-
@staticmethod
|
|
89
|
-
def opsAttribute(action: Callable[[list[ast.cmpop]], list[ast.cmpop]]) -> Callable[[ast.Compare], ast.Compare]:
|
|
90
|
-
def workhorse(node: ast.Compare) -> ast.Compare:
|
|
91
|
-
node.ops = action(node.ops)
|
|
92
|
-
return node
|
|
93
|
-
return workhorse
|
|
94
|
-
|
|
95
|
-
@staticmethod
|
|
96
|
-
def testAttribute(action: Callable[[ast.expr], ast.expr]) -> Callable[[ast.Assert | ast.If | ast.IfExp | ast.While], ast.Assert | ast.If | ast.IfExp | ast.While]:
|
|
97
|
-
def workhorse(node: ast.Assert | ast.If | ast.IfExp | ast.While) -> ast.Assert | ast.If | ast.IfExp | ast.While:
|
|
98
|
-
node.test = action(node.test)
|
|
99
|
-
return node
|
|
100
|
-
return workhorse
|
|
101
|
-
|
|
102
|
-
@staticmethod
|
|
103
|
-
def valueAttribute(action: Callable[[Any], Any]) -> Callable[[astClassHasDOTvalue], astClassHasDOTvalue]:
|
|
104
|
-
def workhorse(node: astClassHasDOTvalue) -> astClassHasDOTvalue:
|
|
105
|
-
node.value = action(node.value)
|
|
106
|
-
return node
|
|
107
|
-
return workhorse
|
|
108
|
-
|
|
109
|
-
class Then:
|
|
110
|
-
"""
|
|
111
|
-
Provide action functions that specify what to do with AST nodes that match predicates.
|
|
112
|
-
|
|
113
|
-
The Then class contains static methods that generate action functions used with NodeChanger and NodeTourist to
|
|
114
|
-
transform or extract information from AST nodes that match specific predicates. These actions include node
|
|
115
|
-
replacement, insertion, extraction, and collection operations.
|
|
116
|
-
|
|
117
|
-
When paired with predicates from the ifThis class, Then methods complete the pattern-matching-and-action workflow
|
|
118
|
-
for AST manipulation.
|
|
119
|
-
"""
|
|
120
|
-
@staticmethod
|
|
121
|
-
def appendTo(listOfAny: list[Any]) -> Callable[[ast.AST | ast_Identifier], ast.AST | ast_Identifier]:
|
|
122
|
-
def workhorse(node: ast.AST | ast_Identifier) -> ast.AST | ast_Identifier:
|
|
123
|
-
listOfAny.append(node)
|
|
124
|
-
return node
|
|
125
|
-
return workhorse
|
|
126
|
-
|
|
127
|
-
@staticmethod
|
|
128
|
-
def extractIt(node: NodeORattribute) -> NodeORattribute:
|
|
129
|
-
return node
|
|
130
|
-
|
|
131
|
-
@staticmethod
|
|
132
|
-
def insertThisAbove(list_astAST: Sequence[ast.AST]) -> Callable[[ast.AST], Sequence[ast.AST]]:
|
|
133
|
-
return lambda aboveMe: [*list_astAST, aboveMe]
|
|
134
|
-
|
|
135
|
-
@staticmethod
|
|
136
|
-
def insertThisBelow(list_astAST: Sequence[ast.AST]) -> Callable[[ast.AST], Sequence[ast.AST]]:
|
|
137
|
-
return lambda belowMe: [belowMe, *list_astAST]
|
|
138
|
-
|
|
139
|
-
@staticmethod
|
|
140
|
-
def removeIt(_removeMe: ast.AST) -> None:
|
|
141
|
-
return None
|
|
142
|
-
|
|
143
|
-
@staticmethod
|
|
144
|
-
def replaceWith(astAST: NodeORattribute) -> Callable[[NodeORattribute], NodeORattribute]:
|
|
145
|
-
return lambda _replaceMe: astAST
|
|
146
|
-
|
|
147
|
-
@staticmethod
|
|
148
|
-
def updateKeyValueIn(key: Callable[..., Any], value: Callable[..., Any], dictionary: dict[Any, Any]) -> Callable[[ast.AST], dict[Any, Any]]:
|
|
149
|
-
def workhorse(node: ast.AST) -> dict[Any, Any]:
|
|
150
|
-
dictionary.setdefault(key(node), value(node))
|
|
151
|
-
return dictionary
|
|
152
|
-
return workhorse
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
from mapFolding.someAssemblyRequired import ast_Identifier, ifThis, IngredientsFunction, LedgerOfImports, NodeTourist, Then
|
|
2
|
-
from mapFolding.theSSOT import raiseIfNoneGitHubIssueNumber3
|
|
3
|
-
import ast
|
|
4
|
-
|
|
5
|
-
def astModuleToIngredientsFunction(astModule: ast.AST, identifierFunctionDef: ast_Identifier) -> IngredientsFunction:
|
|
6
|
-
"""
|
|
7
|
-
Extract a function definition from an AST module and create an `IngredientsFunction`.
|
|
8
|
-
|
|
9
|
-
This function finds a function definition with the specified identifier in the given AST module, extracts it, and
|
|
10
|
-
stores all module imports in the `LedgerOfImports`.
|
|
11
|
-
|
|
12
|
-
Parameters:
|
|
13
|
-
astModule: The AST module containing the function definition.
|
|
14
|
-
identifierFunctionDef: The name of the function to extract.
|
|
15
|
-
|
|
16
|
-
Returns:
|
|
17
|
-
ingredientsFunction: `IngredientsFunction` object containing the `ast.FunctionDef` and _all_ imports from the
|
|
18
|
-
source module.
|
|
19
|
-
|
|
20
|
-
Raises:
|
|
21
|
-
raiseIfNoneGitHubIssueNumber3: If the function definition is not found.
|
|
22
|
-
"""
|
|
23
|
-
astFunctionDef = extractFunctionDef(astModule, identifierFunctionDef)
|
|
24
|
-
if not astFunctionDef: raise raiseIfNoneGitHubIssueNumber3
|
|
25
|
-
return IngredientsFunction(astFunctionDef, LedgerOfImports(astModule))
|
|
26
|
-
|
|
27
|
-
def extractClassDef(module: ast.AST, identifier: ast_Identifier) -> ast.ClassDef | None:
|
|
28
|
-
"""
|
|
29
|
-
Extract a class definition with a specific name from an AST module.
|
|
30
|
-
|
|
31
|
-
This function searches through an AST module for a class definition that matches the provided identifier and returns
|
|
32
|
-
it if found.
|
|
33
|
-
|
|
34
|
-
Parameters:
|
|
35
|
-
module: The AST module to search within.
|
|
36
|
-
identifier: The name of the class to find.
|
|
37
|
-
|
|
38
|
-
Returns:
|
|
39
|
-
astClassDef|None: The matching class definition AST node, or `None` if not found.
|
|
40
|
-
"""
|
|
41
|
-
return NodeTourist(ifThis.isClassDef_Identifier(identifier), Then.extractIt).captureLastMatch(module)
|
|
42
|
-
|
|
43
|
-
def extractFunctionDef(module: ast.AST, identifier: ast_Identifier) -> ast.FunctionDef | None:
|
|
44
|
-
"""
|
|
45
|
-
Extract a function definition with a specific name from an AST module.
|
|
46
|
-
|
|
47
|
-
This function searches through an AST module for a function definition that matches the provided identifier and
|
|
48
|
-
returns it if found.
|
|
49
|
-
|
|
50
|
-
Parameters:
|
|
51
|
-
module: The AST module to search within.
|
|
52
|
-
identifier: The name of the function to find.
|
|
53
|
-
|
|
54
|
-
Returns:
|
|
55
|
-
astFunctionDef|None: The matching function definition AST node, or `None` if not found.
|
|
56
|
-
"""
|
|
57
|
-
return NodeTourist(ifThis.isFunctionDef_Identifier(identifier), Then.extractIt).captureLastMatch(module)
|
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
AST Node Predicate and Access Utilities for Pattern Matching and Traversal
|
|
3
|
-
|
|
4
|
-
This module provides utilities for accessing and matching AST nodes in a consistent way. It contains three primary
|
|
5
|
-
classes:
|
|
6
|
-
|
|
7
|
-
1. DOT: Provides consistent accessor methods for AST node attributes across different node types, simplifying the access
|
|
8
|
-
to node properties.
|
|
9
|
-
|
|
10
|
-
2. be: Offers type-guard functions that verify AST node types, enabling safe type narrowing for static type checking and
|
|
11
|
-
improving code safety.
|
|
12
|
-
|
|
13
|
-
3. ifThis: Contains predicate functions for matching AST nodes based on various criteria, enabling precise targeting of
|
|
14
|
-
nodes for analysis or transformation.
|
|
15
|
-
|
|
16
|
-
These utilities form the foundation of the pattern-matching component in the AST manipulation framework, working in
|
|
17
|
-
conjunction with the NodeChanger and NodeTourist classes to enable precise and targeted code transformations. Together,
|
|
18
|
-
they implement a declarative approach to AST manipulation that separates node identification (ifThis), type verification
|
|
19
|
-
(be), and data access (DOT).
|
|
20
|
-
"""
|
|
21
|
-
|
|
22
|
-
from collections.abc import Callable
|
|
23
|
-
from mapFolding.someAssemblyRequired import (
|
|
24
|
-
ast_Identifier,
|
|
25
|
-
astClassHasDOTbody,
|
|
26
|
-
astClassHasDOTbody_expr,
|
|
27
|
-
astClassHasDOTbodyList_stmt,
|
|
28
|
-
astClassHasDOTnameNotName,
|
|
29
|
-
astClassHasDOTnameNotNameAlways,
|
|
30
|
-
astClassHasDOTnameNotNameOptionally,
|
|
31
|
-
astClassHasDOTtarget,
|
|
32
|
-
astClassHasDOTtarget_expr,
|
|
33
|
-
astClassHasDOTtargetAttributeNameSubscript,
|
|
34
|
-
astClassHasDOTvalue,
|
|
35
|
-
astClassHasDOTvalue_expr,
|
|
36
|
-
astClassHasDOTvalue_exprNone,
|
|
37
|
-
ImaCallToName,
|
|
38
|
-
)
|
|
39
|
-
from typing import Any, overload, TypeGuard
|
|
40
|
-
import ast
|
|
41
|
-
|
|
42
|
-
class DOT:
|
|
43
|
-
"""
|
|
44
|
-
Access attributes and sub-nodes of AST elements via consistent accessor methods.
|
|
45
|
-
|
|
46
|
-
The DOT class provides static methods to access specific attributes of different types of AST nodes in a consistent
|
|
47
|
-
way. This simplifies attribute access across various node types and improves code readability by abstracting the
|
|
48
|
-
underlying AST structure details.
|
|
49
|
-
|
|
50
|
-
DOT is designed for safe, read-only access to node properties, unlike the grab class which is designed for modifying
|
|
51
|
-
node attributes.
|
|
52
|
-
"""
|
|
53
|
-
@staticmethod
|
|
54
|
-
@overload
|
|
55
|
-
def annotation(node: ast.AnnAssign) -> ast.expr:...
|
|
56
|
-
@staticmethod
|
|
57
|
-
@overload
|
|
58
|
-
def annotation(node: ast.arg) -> ast.expr | None:...
|
|
59
|
-
@staticmethod
|
|
60
|
-
def annotation(node: ast.AnnAssign | ast.arg) -> ast.expr | None:
|
|
61
|
-
return node.annotation
|
|
62
|
-
|
|
63
|
-
@staticmethod
|
|
64
|
-
@overload
|
|
65
|
-
def arg(node: ast.arg) -> ast_Identifier:...
|
|
66
|
-
@staticmethod
|
|
67
|
-
@overload
|
|
68
|
-
def arg(node: ast.keyword) -> ast_Identifier | None:...
|
|
69
|
-
@staticmethod
|
|
70
|
-
def arg(node: ast.arg | ast.keyword) -> ast_Identifier | None:
|
|
71
|
-
return node.arg
|
|
72
|
-
|
|
73
|
-
@staticmethod
|
|
74
|
-
def attr(node: ast.Attribute) -> ast_Identifier:
|
|
75
|
-
return node.attr
|
|
76
|
-
|
|
77
|
-
@staticmethod
|
|
78
|
-
@overload
|
|
79
|
-
def body(node: astClassHasDOTbodyList_stmt) -> list[ast.stmt]:...
|
|
80
|
-
@staticmethod
|
|
81
|
-
@overload
|
|
82
|
-
def body(node: astClassHasDOTbody_expr) -> ast.expr:...
|
|
83
|
-
@staticmethod
|
|
84
|
-
def body(node: astClassHasDOTbody) -> ast.expr | list[ast.stmt]:
|
|
85
|
-
return node.body
|
|
86
|
-
|
|
87
|
-
@staticmethod
|
|
88
|
-
@overload
|
|
89
|
-
def func(node: ImaCallToName) -> ast.Name:...
|
|
90
|
-
@staticmethod
|
|
91
|
-
@overload
|
|
92
|
-
def func(node: ast.Call) -> ast.expr:...
|
|
93
|
-
@staticmethod
|
|
94
|
-
def func(node: ast.Call | ImaCallToName) -> ast.expr | ast.Name:
|
|
95
|
-
return node.func
|
|
96
|
-
|
|
97
|
-
@staticmethod
|
|
98
|
-
def id(node: ast.Name) -> ast_Identifier:
|
|
99
|
-
return node.id
|
|
100
|
-
|
|
101
|
-
@staticmethod
|
|
102
|
-
@overload
|
|
103
|
-
def name(node: astClassHasDOTnameNotNameAlways) -> ast_Identifier:...
|
|
104
|
-
@staticmethod
|
|
105
|
-
@overload
|
|
106
|
-
def name(node: astClassHasDOTnameNotNameOptionally) -> ast_Identifier | None:...
|
|
107
|
-
@staticmethod
|
|
108
|
-
def name(node: astClassHasDOTnameNotName) -> ast_Identifier | None:
|
|
109
|
-
return node.name
|
|
110
|
-
|
|
111
|
-
@staticmethod
|
|
112
|
-
@overload
|
|
113
|
-
def target(node: ast.NamedExpr) -> ast.Name:...
|
|
114
|
-
@staticmethod
|
|
115
|
-
@overload
|
|
116
|
-
def target(node: astClassHasDOTtarget_expr) -> ast.expr:...
|
|
117
|
-
@staticmethod
|
|
118
|
-
@overload
|
|
119
|
-
def target(node: astClassHasDOTtargetAttributeNameSubscript) -> ast.Attribute | ast.Name | ast.Subscript:...
|
|
120
|
-
@staticmethod
|
|
121
|
-
def target(node: astClassHasDOTtarget) -> ast.Attribute | ast.expr | ast.Name | ast.Subscript:
|
|
122
|
-
return node.target
|
|
123
|
-
|
|
124
|
-
@staticmethod
|
|
125
|
-
@overload
|
|
126
|
-
def value(node: ast.Constant) -> Any:...
|
|
127
|
-
@staticmethod
|
|
128
|
-
@overload
|
|
129
|
-
def value(node: ast.MatchSingleton) -> bool | None:...
|
|
130
|
-
@staticmethod
|
|
131
|
-
@overload
|
|
132
|
-
def value(node: astClassHasDOTvalue_expr) -> ast.expr:...
|
|
133
|
-
@staticmethod
|
|
134
|
-
@overload
|
|
135
|
-
def value(node: astClassHasDOTvalue_exprNone) -> ast.expr | None:...
|
|
136
|
-
@staticmethod
|
|
137
|
-
def value(node: astClassHasDOTvalue) -> Any | ast.expr | bool | None:
|
|
138
|
-
return node.value
|
|
139
|
-
|
|
140
|
-
class be:
|
|
141
|
-
"""
|
|
142
|
-
Provide type-guard functions for safely verifying AST node types during manipulation.
|
|
143
|
-
|
|
144
|
-
The be class contains static methods that perform runtime type verification of AST nodes, returning TypeGuard
|
|
145
|
-
results that enable static type checkers to narrow node types in conditional branches. These type-guards:
|
|
146
|
-
|
|
147
|
-
1. Improve code safety by preventing operations on incompatible node types.
|
|
148
|
-
2. Enable IDE tooling to provide better autocompletion and error detection.
|
|
149
|
-
3. Document expected node types in a way that's enforced by the type system.
|
|
150
|
-
4. Support pattern-matching workflows where node types must be verified before access.
|
|
151
|
-
|
|
152
|
-
When used with conditional statements, these type-guards allow for precise, type-safe manipulation of AST nodes
|
|
153
|
-
while maintaining full static type checking capabilities, even in complex transformation scenarios.
|
|
154
|
-
"""
|
|
155
|
-
@staticmethod
|
|
156
|
-
def AnnAssign(node: ast.AST) -> TypeGuard[ast.AnnAssign]:
|
|
157
|
-
return isinstance(node, ast.AnnAssign)
|
|
158
|
-
|
|
159
|
-
@staticmethod
|
|
160
|
-
def arg(node: ast.AST) -> TypeGuard[ast.arg]:
|
|
161
|
-
return isinstance(node, ast.arg)
|
|
162
|
-
|
|
163
|
-
@staticmethod
|
|
164
|
-
def Assign(node: ast.AST) -> TypeGuard[ast.Assign]:
|
|
165
|
-
return isinstance(node, ast.Assign)
|
|
166
|
-
|
|
167
|
-
@staticmethod
|
|
168
|
-
def Attribute(node: ast.AST) -> TypeGuard[ast.Attribute]:
|
|
169
|
-
return isinstance(node, ast.Attribute)
|
|
170
|
-
|
|
171
|
-
@staticmethod
|
|
172
|
-
def AugAssign(node: ast.AST) -> TypeGuard[ast.AugAssign]:
|
|
173
|
-
return isinstance(node, ast.AugAssign)
|
|
174
|
-
|
|
175
|
-
@staticmethod
|
|
176
|
-
def Call(node: ast.AST) -> TypeGuard[ast.Call]:
|
|
177
|
-
return isinstance(node, ast.Call)
|
|
178
|
-
|
|
179
|
-
@staticmethod
|
|
180
|
-
def ClassDef(node: ast.AST) -> TypeGuard[ast.ClassDef]:
|
|
181
|
-
return isinstance(node, ast.ClassDef)
|
|
182
|
-
|
|
183
|
-
@staticmethod
|
|
184
|
-
def Compare(node: ast.AST) -> TypeGuard[ast.Compare]:
|
|
185
|
-
return isinstance(node, ast.Compare)
|
|
186
|
-
|
|
187
|
-
@staticmethod
|
|
188
|
-
def Constant(node: ast.AST) -> TypeGuard[ast.Constant]:
|
|
189
|
-
return isinstance(node, ast.Constant)
|
|
190
|
-
|
|
191
|
-
@staticmethod
|
|
192
|
-
def FunctionDef(node: ast.AST) -> TypeGuard[ast.FunctionDef]:
|
|
193
|
-
return isinstance(node, ast.FunctionDef)
|
|
194
|
-
|
|
195
|
-
@staticmethod
|
|
196
|
-
def keyword(node: ast.AST) -> TypeGuard[ast.keyword]:
|
|
197
|
-
return isinstance(node, ast.keyword)
|
|
198
|
-
|
|
199
|
-
@staticmethod
|
|
200
|
-
def If(node: ast.AST) -> TypeGuard[ast.If]:
|
|
201
|
-
return isinstance(node, ast.If)
|
|
202
|
-
|
|
203
|
-
@staticmethod
|
|
204
|
-
def Gt(node: ast.AST) -> TypeGuard[ast.Gt]:
|
|
205
|
-
return isinstance(node, ast.Gt)
|
|
206
|
-
|
|
207
|
-
@staticmethod
|
|
208
|
-
def LtE(node: ast.AST) -> TypeGuard[ast.LtE]:
|
|
209
|
-
return isinstance(node, ast.LtE)
|
|
210
|
-
|
|
211
|
-
@staticmethod
|
|
212
|
-
def Name(node: ast.AST) -> TypeGuard[ast.Name]:
|
|
213
|
-
return isinstance(node, ast.Name)
|
|
214
|
-
|
|
215
|
-
@staticmethod
|
|
216
|
-
def Not(node: ast.AST) -> TypeGuard[ast.Not]:
|
|
217
|
-
return isinstance(node, ast.Not)
|
|
218
|
-
|
|
219
|
-
@staticmethod
|
|
220
|
-
def Return(node: ast.AST) -> TypeGuard[ast.Return]:
|
|
221
|
-
return isinstance(node, ast.Return)
|
|
222
|
-
|
|
223
|
-
@staticmethod
|
|
224
|
-
def Starred(node: ast.AST) -> TypeGuard[ast.Starred]:
|
|
225
|
-
return isinstance(node, ast.Starred)
|
|
226
|
-
|
|
227
|
-
@staticmethod
|
|
228
|
-
def Subscript(node: ast.AST) -> TypeGuard[ast.Subscript]:
|
|
229
|
-
return isinstance(node, ast.Subscript)
|
|
230
|
-
|
|
231
|
-
@staticmethod
|
|
232
|
-
def Tuple(node: ast.AST) -> TypeGuard[ast.Tuple]:
|
|
233
|
-
return isinstance(node, ast.Tuple)
|
|
234
|
-
|
|
235
|
-
@staticmethod
|
|
236
|
-
def UnaryOp(node: ast.AST) -> TypeGuard[ast.UnaryOp]:
|
|
237
|
-
return isinstance(node, ast.UnaryOp)
|
|
238
|
-
|
|
239
|
-
@staticmethod
|
|
240
|
-
def While(node: ast.AST) -> TypeGuard[ast.While]:
|
|
241
|
-
return isinstance(node, ast.While)
|
|
242
|
-
|
|
243
|
-
class ifThis:
|
|
244
|
-
"""
|
|
245
|
-
Provide predicate functions for matching and filtering AST nodes based on various criteria.
|
|
246
|
-
|
|
247
|
-
The ifThis class contains static methods that generate predicate functions used to test whether AST nodes match
|
|
248
|
-
specific criteria. These predicates can be used with NodeChanger and NodeTourist to identify and process specific
|
|
249
|
-
patterns in the AST.
|
|
250
|
-
|
|
251
|
-
The class provides predicates for matching various node types, attributes, identifiers, and structural patterns,
|
|
252
|
-
enabling precise targeting of AST elements for analysis or transformation.
|
|
253
|
-
"""
|
|
254
|
-
@staticmethod
|
|
255
|
-
def _Identifier(identifier: ast_Identifier) -> Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]:
|
|
256
|
-
return lambda node: node == identifier
|
|
257
|
-
@staticmethod
|
|
258
|
-
def _nested_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool]:
|
|
259
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool:
|
|
260
|
-
return ifThis.isName_Identifier(identifier)(node) or ifThis.isAttribute_Identifier(identifier)(node) or ifThis.isSubscript_Identifier(identifier)(node) or ifThis.isStarred_Identifier(identifier)(node)
|
|
261
|
-
return workhorse
|
|
262
|
-
|
|
263
|
-
@staticmethod
|
|
264
|
-
def is_arg_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg] | bool]:
|
|
265
|
-
"""see also `isArgument_Identifier`"""
|
|
266
|
-
return lambda node: be.arg(node) and ifThis._Identifier(identifier)(DOT.arg(node))
|
|
267
|
-
@staticmethod
|
|
268
|
-
def is_keyword_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.keyword] | bool]:
|
|
269
|
-
"""see also `isArgument_Identifier`"""
|
|
270
|
-
return lambda node: be.keyword(node) and ifThis._Identifier(identifier)(DOT.arg(node))
|
|
271
|
-
|
|
272
|
-
@staticmethod
|
|
273
|
-
def isAnnAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
|
|
274
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.AnnAssign] | bool:
|
|
275
|
-
return be.AnnAssign(node) and targetPredicate(DOT.target(node))
|
|
276
|
-
return workhorse
|
|
277
|
-
|
|
278
|
-
@staticmethod
|
|
279
|
-
def isArgument_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg | ast.keyword] | bool]:
|
|
280
|
-
return lambda node: (be.arg(node) or be.keyword(node)) and ifThis._Identifier(identifier)(DOT.arg(node))
|
|
281
|
-
|
|
282
|
-
@staticmethod
|
|
283
|
-
def isAssignAndTargets0Is(targets0Predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
|
|
284
|
-
"""node is Assign and node.targets[0] matches `targets0Predicate`."""
|
|
285
|
-
return lambda node: be.Assign(node) and targets0Predicate(node.targets[0])
|
|
286
|
-
@staticmethod
|
|
287
|
-
def isAssignAndValueIs(valuePredicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
|
|
288
|
-
"""node is ast.Assign and node.value matches `valuePredicate`. """
|
|
289
|
-
return lambda node: be.Assign(node) and valuePredicate(DOT.value(node))
|
|
290
|
-
|
|
291
|
-
@staticmethod
|
|
292
|
-
def isAttribute_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
|
|
293
|
-
"""node is `ast.Attribute` and the top-level `ast.Name` is `identifier`"""
|
|
294
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute]:
|
|
295
|
-
return be.Attribute(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
|
|
296
|
-
return workhorse
|
|
297
|
-
@staticmethod
|
|
298
|
-
def isAttributeName(node: ast.AST) -> TypeGuard[ast.Attribute]:
|
|
299
|
-
""" Displayed as Name.attribute."""
|
|
300
|
-
return be.Attribute(node) and be.Name(DOT.value(node))
|
|
301
|
-
@staticmethod
|
|
302
|
-
def isAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
|
|
303
|
-
return lambda node: ifThis.isAttributeName(node) and ifThis.isName_Identifier(namespace)(DOT.value(node)) and ifThis._Identifier(identifier)(DOT.attr(node))
|
|
304
|
-
|
|
305
|
-
@staticmethod
|
|
306
|
-
def isAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Compare] | bool]:
|
|
307
|
-
return lambda node: (be.Compare(node)
|
|
308
|
-
and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.left)
|
|
309
|
-
and be.Gt(node.ops[0])
|
|
310
|
-
and ifThis.isConstant_value(0)(node.comparators[0]))
|
|
311
|
-
|
|
312
|
-
@staticmethod
|
|
313
|
-
def isUnaryNotAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.UnaryOp] | bool]:
|
|
314
|
-
return lambda node: (be.UnaryOp(node)
|
|
315
|
-
and be.Not(node.op)
|
|
316
|
-
and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.operand))
|
|
317
|
-
|
|
318
|
-
@staticmethod
|
|
319
|
-
def isIfUnaryNotAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.If] | bool]:
|
|
320
|
-
return lambda node: (be.If(node)
|
|
321
|
-
and ifThis.isUnaryNotAttributeNamespace_Identifier(namespace, identifier)(node.test))
|
|
322
|
-
|
|
323
|
-
@staticmethod
|
|
324
|
-
def isIfAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.If] | bool]:
|
|
325
|
-
return lambda node: (be.If(node)
|
|
326
|
-
and ifThis.isAttributeNamespace_IdentifierGreaterThan0(namespace, identifier)(node.test))
|
|
327
|
-
|
|
328
|
-
@staticmethod
|
|
329
|
-
def isWhileAttributeNamespace_IdentifierGreaterThan0(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.While] | bool]:
|
|
330
|
-
return lambda node: (be.While(node)
|
|
331
|
-
and ifThis.isAttributeNamespace_IdentifierGreaterThan0(namespace, identifier)(node.test))
|
|
332
|
-
|
|
333
|
-
@staticmethod
|
|
334
|
-
def isAttributeNamespace_IdentifierLessThanOrEqual(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Compare] | bool]:
|
|
335
|
-
return lambda node: (be.Compare(node)
|
|
336
|
-
and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(node.left)
|
|
337
|
-
and be.LtE(node.ops[0]))
|
|
338
|
-
|
|
339
|
-
@staticmethod
|
|
340
|
-
def isAugAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AugAssign] | bool]:
|
|
341
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.AugAssign] | bool:
|
|
342
|
-
return be.AugAssign(node) and targetPredicate(DOT.target(node))
|
|
343
|
-
return workhorse
|
|
344
|
-
|
|
345
|
-
@staticmethod
|
|
346
|
-
def isCall_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ImaCallToName] | bool]:
|
|
347
|
-
def workhorse(node: ast.AST) -> TypeGuard[ImaCallToName] | bool:
|
|
348
|
-
return ifThis.isCallToName(node) and ifThis._Identifier(identifier)(DOT.id(DOT.func(node)))
|
|
349
|
-
return workhorse
|
|
350
|
-
|
|
351
|
-
@staticmethod
|
|
352
|
-
def isCallAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
|
|
353
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.Call] | bool:
|
|
354
|
-
return be.Call(node) and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(DOT.func(node))
|
|
355
|
-
return workhorse
|
|
356
|
-
@staticmethod
|
|
357
|
-
def isCallToName(node: ast.AST) -> TypeGuard[ImaCallToName]:
|
|
358
|
-
return be.Call(node) and be.Name(DOT.func(node))
|
|
359
|
-
|
|
360
|
-
@staticmethod
|
|
361
|
-
def isClassDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.ClassDef] | bool]:
|
|
362
|
-
return lambda node: be.ClassDef(node) and ifThis._Identifier(identifier)(DOT.name(node))
|
|
363
|
-
|
|
364
|
-
@staticmethod
|
|
365
|
-
def isConstant_value(value: Any) -> Callable[[ast.AST], TypeGuard[ast.Constant] | bool]:
|
|
366
|
-
return lambda node: be.Constant(node) and DOT.value(node) == value
|
|
367
|
-
|
|
368
|
-
@staticmethod
|
|
369
|
-
def isFunctionDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.FunctionDef] | bool]:
|
|
370
|
-
return lambda node: be.FunctionDef(node) and ifThis._Identifier(identifier)(DOT.name(node))
|
|
371
|
-
|
|
372
|
-
@staticmethod
|
|
373
|
-
def isName_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Name] | bool]:
|
|
374
|
-
return lambda node: be.Name(node) and ifThis._Identifier(identifier)(DOT.id(node))
|
|
375
|
-
|
|
376
|
-
@staticmethod
|
|
377
|
-
def isStarred_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Starred] | bool]:
|
|
378
|
-
"""node is `ast.Starred` and the top-level `ast.Name` is `identifier`"""
|
|
379
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.Starred]:
|
|
380
|
-
return be.Starred(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
|
|
381
|
-
return workhorse
|
|
382
|
-
@staticmethod
|
|
383
|
-
def isSubscript_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Subscript] | bool]:
|
|
384
|
-
"""node is `ast.Subscript` and the top-level `ast.Name` is `identifier`"""
|
|
385
|
-
def workhorse(node: ast.AST) -> TypeGuard[ast.Subscript]:
|
|
386
|
-
return be.Subscript(node) and ifThis._nested_Identifier(identifier)(DOT.value(node))
|
|
387
|
-
return workhorse
|
|
388
|
-
|
|
389
|
-
@staticmethod
|
|
390
|
-
def matchesMeButNotAnyDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
|
|
391
|
-
return lambda node: predicate(node) and ifThis.matchesNoDescendant(predicate)(node)
|
|
392
|
-
@staticmethod
|
|
393
|
-
def matchesNoDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
|
|
394
|
-
def workhorse(node: ast.AST) -> bool:
|
|
395
|
-
for descendant in ast.walk(node):
|
|
396
|
-
if descendant is not node and predicate(descendant):
|
|
397
|
-
return False
|
|
398
|
-
return True
|
|
399
|
-
return workhorse
|
|
400
|
-
|
|
401
|
-
@staticmethod
|
|
402
|
-
def Z0Z_unparseIs(astAST: ast.AST) -> Callable[[ast.AST], bool]:
|
|
403
|
-
def workhorse(node: ast.AST) -> bool: return ast.unparse(node) == ast.unparse(astAST)
|
|
404
|
-
return workhorse
|