mapFolding 0.8.5__py3-none-any.whl → 0.9.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.
Files changed (33) hide show
  1. mapFolding/__init__.py +66 -18
  2. mapFolding/basecamp.py +32 -17
  3. mapFolding/beDRY.py +3 -3
  4. mapFolding/oeis.py +121 -25
  5. mapFolding/someAssemblyRequired/__init__.py +48 -27
  6. mapFolding/someAssemblyRequired/_theTypes.py +11 -15
  7. mapFolding/someAssemblyRequired/_tool_Make.py +40 -12
  8. mapFolding/someAssemblyRequired/_tool_Then.py +59 -25
  9. mapFolding/someAssemblyRequired/_toolboxAntecedents.py +151 -276
  10. mapFolding/someAssemblyRequired/_toolboxContainers.py +185 -51
  11. mapFolding/someAssemblyRequired/_toolboxPython.py +165 -44
  12. mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +141 -20
  13. mapFolding/someAssemblyRequired/toolboxNumba.py +93 -52
  14. mapFolding/someAssemblyRequired/transformationTools.py +228 -138
  15. mapFolding/syntheticModules/numbaCount_doTheNeedful.py +0 -1
  16. mapFolding/theSSOT.py +147 -55
  17. mapFolding/toolboxFilesystem.py +1 -1
  18. mapfolding-0.9.0.dist-info/METADATA +177 -0
  19. mapfolding-0.9.0.dist-info/RECORD +46 -0
  20. tests/__init__.py +44 -0
  21. tests/conftest.py +75 -7
  22. tests/test_computations.py +90 -9
  23. tests/test_filesystem.py +32 -33
  24. tests/test_other.py +0 -1
  25. tests/test_tasks.py +2 -2
  26. mapFolding/noHomeYet.py +0 -32
  27. mapFolding/someAssemblyRequired/newInliner.py +0 -22
  28. mapfolding-0.8.5.dist-info/METADATA +0 -190
  29. mapfolding-0.8.5.dist-info/RECORD +0 -48
  30. {mapfolding-0.8.5.dist-info → mapfolding-0.9.0.dist-info}/WHEEL +0 -0
  31. {mapfolding-0.8.5.dist-info → mapfolding-0.9.0.dist-info}/entry_points.txt +0 -0
  32. {mapfolding-0.8.5.dist-info → mapfolding-0.9.0.dist-info}/licenses/LICENSE +0 -0
  33. {mapfolding-0.8.5.dist-info → mapfolding-0.9.0.dist-info}/top_level.txt +0 -0
@@ -1,331 +1,226 @@
1
- from collections.abc import Callable, Container
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.
5
+ It contains two primary classes:
6
+
7
+ 1. DOT: Provides consistent accessor methods for AST node attributes across different
8
+ node types, simplifying the access to node properties.
9
+
10
+ 2. ifThis: Contains predicate functions for matching AST nodes based on various criteria,
11
+ enabling precise targeting of nodes for analysis or transformation.
12
+
13
+ These utilities form the foundation of the pattern-matching component in the AST
14
+ manipulation framework, working in conjunction with the NodeChanger and NodeTourist
15
+ classes to enable precise and targeted code transformations.
16
+ """
17
+
18
+ from collections.abc import Callable
2
19
  from mapFolding.someAssemblyRequired import (
3
- ast_expr_Slice,
4
20
  ast_Identifier,
5
- Ima_funcTypeUNEDITED,
6
21
  astClassHasDOTnameNotName,
7
- astClassOptionallyHasDOTnameNotName,
8
22
  astClassHasDOTtarget,
23
+ astClassHasDOTtargetAttributeNameSubscript,
24
+ astClassHasDOTtarget_expr,
9
25
  astClassHasDOTvalue,
10
- ImaAnnotationType,
11
- ImaAnnotationTypeVar,
12
- Ima_targetTypeUNEDITED,
13
- TypeCertified,
26
+ astClassHasDOTvalue_expr,
27
+ astClassOptionallyHasDOTnameNotName,
28
+ astClassHasDOTvalue_exprNone,
14
29
  )
15
- from typing import Any, cast, overload, TypeGuard
30
+ from typing import Any, overload, TypeGuard
16
31
  import ast
17
32
 
18
- class NotMyProblem(Exception):
19
- pass
20
-
21
33
  class DOT:
22
- @staticmethod
23
- @overload
24
- def annotation(node: ast.AnnAssign) -> ImaAnnotationType:...
25
- @staticmethod
26
- @overload
27
- def annotation(node: ast.arg) -> ImaAnnotationType | None:...
28
- @staticmethod
29
- def annotation(node: ast.AnnAssign | ast.arg) -> ImaAnnotationType | None:
30
- return cast(ImaAnnotationType, node.annotation)
31
- # fu = node.annotation
32
- # if fu is None:
33
- # return None
34
- # else:
35
- # fu = cast(ImaAnnotationType, node.annotation)
36
- # return fu
37
- @staticmethod
38
- def func(node: ast.Call) -> Ima_funcTypeUNEDITED | ast.expr:
39
- return node.func
40
- @staticmethod
41
- def id(node: ast.Name) -> ast_Identifier:
42
- return node.id
43
- @staticmethod
44
- def name(node: astClassHasDOTnameNotName | astClassOptionallyHasDOTnameNotName) -> ast_Identifier:
45
- if isinstance(node, astClassHasDOTnameNotName):
46
- return node.name
47
- try:
48
- identifier = node.name
49
- if identifier is None:
50
- raise NotMyProblem
51
- return identifier
52
- except AttributeError:
53
- raise NotMyProblem
34
+ """
35
+ Access attributes and sub-nodes of AST elements via consistent accessor methods.
54
36
 
55
- @staticmethod
56
- def nameOrNone(node: astClassHasDOTnameNotName | astClassOptionallyHasDOTnameNotName) -> ast_Identifier | None:
57
- if isinstance(node, astClassHasDOTnameNotName):
58
- return node.name
59
- try:
60
- return node.name
61
- except AttributeError:
62
- return None
37
+ The DOT class provides static methods to access specific attributes of different
38
+ types of AST nodes in a consistent way. This simplifies attribute access across
39
+ various node types and improves code readability by abstracting the underlying
40
+ AST structure details.
63
41
 
64
- class 又:
42
+ DOT is designed for safe, read-only access to node properties, unlike the grab
43
+ class which is designed for modifying node attributes.
44
+ """
65
45
  @staticmethod
66
46
  @overload
67
- def annotation(predicate: Callable[[ImaAnnotationType], ast.AST | ast_Identifier]) -> Callable[[ast.AnnAssign | ast.arg], ast.AST | ast_Identifier]:...
47
+ def annotation(node: ast.AnnAssign) -> ast.expr:...
68
48
  @staticmethod
69
49
  @overload
70
- def annotation(predicate: Callable[[ImaAnnotationType], TypeGuard[ImaAnnotationType] | bool]) -> Callable[[ast.AnnAssign | ast.arg], TypeGuard[ast.AnnAssign] | TypeGuard[ast.arg] | bool]:...
71
- @staticmethod
72
- def annotation(predicate: Callable[[ImaAnnotationType], TypeGuard[ImaAnnotationType] | ast.AST | ast_Identifier | bool]) -> Callable[[ast.AnnAssign | ast.arg], TypeGuard[ast.AnnAssign] | TypeGuard[ast.arg] | ast.AST | ast_Identifier | bool]:
73
- @overload
74
- def workhorse(node: ast.AnnAssign | ast.arg) -> ast.AST | ast_Identifier:...
75
- @overload
76
- def workhorse(node: ast.AnnAssign | ast.arg) -> TypeGuard[ast.AnnAssign] | TypeGuard[ast.arg] | bool:...
77
- def workhorse(node: ast.AnnAssign | ast.arg) -> TypeGuard[ast.AnnAssign] | TypeGuard[ast.arg] | ast.AST | ast_Identifier | bool:
78
- ImaAnnotation = node.annotation
79
- if ImaAnnotation is None: return False
80
- assert be.Attribute(ImaAnnotation) or be.Constant(ImaAnnotation) or be.Name(ImaAnnotation) or be.Subscript(ImaAnnotation)
81
- # assert be.Annotation(ImaAnnotation)
82
- return predicate(ImaAnnotation)
83
- return workhorse
50
+ def annotation(node: ast.arg) -> ast.expr | None:...
84
51
  @staticmethod
85
- @overload
86
- def arg(predicate: Callable[[ast_Identifier], ast.AST | ast_Identifier]) -> Callable[[ast.arg | ast.keyword], ast.AST | ast_Identifier]:...
52
+ def annotation(node: ast.AnnAssign | ast.arg) -> ast.expr | None:
53
+ return node.annotation
54
+
87
55
  @staticmethod
88
56
  @overload
89
- def arg(predicate: Callable[[ast_Identifier], TypeGuard[ast_Identifier] | bool]) -> Callable[[ast.arg | ast.keyword], TypeGuard[ast.arg] | TypeGuard[ast.keyword] | bool]:...
90
- @staticmethod
91
- def arg(predicate: Callable[[ast_Identifier], TypeGuard[ast_Identifier] | ast.AST | ast_Identifier | bool]) -> Callable[[ast.arg | ast.keyword], TypeGuard[ast.arg] | TypeGuard[ast.keyword] | ast.AST | ast_Identifier | bool]:
92
- @overload
93
- def workhorse(node: ast.arg | ast.keyword) -> ast.AST | ast_Identifier:...
94
- @overload
95
- def workhorse(node: ast.arg | ast.keyword) -> TypeGuard[ast.arg] | TypeGuard[ast.keyword] | bool:...
96
- def workhorse(node: ast.arg | ast.keyword) -> TypeGuard[ast.arg] | TypeGuard[ast.keyword] | ast.AST | ast_Identifier | bool:
97
- Ima_arg = node.arg
98
- if Ima_arg is None: return False
99
- return predicate(Ima_arg)
100
- return workhorse
101
- @staticmethod
102
- def asname(predicate: Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]) -> Callable[[ast.alias], TypeGuard[ast.alias] | bool]:
103
- return lambda node: predicate(node.asname)
104
- @staticmethod
105
- def attr(predicate: Callable[[ast_Identifier], TypeGuard[ast_Identifier] | bool]) -> Callable[[ast.Attribute], TypeGuard[ast.Attribute] | bool]:
106
- return lambda node: predicate(node.attr)
107
- @staticmethod
108
- def func(predicate: Callable[[ast.AST], TypeGuard[ast.AST] | bool]) -> Callable[[ast.Call], TypeGuard[ast.Call] | bool]:
109
- return lambda node: predicate(node.func)
57
+ def arg(node: ast.arg) -> ast_Identifier:...
110
58
  @staticmethod
111
- def id(predicate: Callable[[ast_Identifier], TypeGuard[ast_Identifier] | bool]) -> Callable[[ast.Name], TypeGuard[ast.Name] | bool]:
112
- return lambda node: predicate(node.id)
113
- @staticmethod
114
- def module(predicate: Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]) -> Callable[[ast.ImportFrom], TypeGuard[ast.ImportFrom] | bool]:
115
- return lambda node: predicate(node.module)
116
- @staticmethod
117
- def name(predicate: Callable[[ast_Identifier], TypeGuard[ast_Identifier] | bool]) -> Callable[[astClassHasDOTnameNotName], TypeGuard[astClassHasDOTnameNotName] | bool]:
118
- return lambda node: predicate(node.name)
119
- @staticmethod
120
- def slice(predicate: Callable[[ast_expr_Slice], TypeGuard[ast_expr_Slice] | bool]) -> Callable[[ast.Subscript], TypeGuard[ast.Subscript] | bool]:
121
- return lambda node: predicate(node.slice)
122
- @staticmethod
123
- def target(predicate: Callable[[ast.AST], TypeGuard[ast.AST] | bool]) -> Callable[[astClassHasDOTtarget], TypeGuard[astClassHasDOTtarget] | bool]:
124
- return lambda node: predicate(node.target)
59
+ @overload
60
+ def arg(node: ast.keyword) -> ast_Identifier | None:...
125
61
  @staticmethod
126
- def value(predicate: Callable[[ast.AST], TypeGuard[ast.AST] | bool]) -> Callable[[astClassHasDOTvalue], TypeGuard[astClassHasDOTvalue] | bool]:
127
- def workhorse(node: astClassHasDOTvalue) -> TypeGuard[astClassHasDOTvalue] | bool:
128
- ImaValue = node.value
129
- if ImaValue is None: return False
130
- return predicate(ImaValue)
131
- return workhorse
62
+ def arg(node: ast.arg | ast.keyword) -> ast_Identifier | None:
63
+ return node.arg
132
64
 
133
- class be:
134
- @staticmethod
135
- def _typeCertified(antecedent: type[TypeCertified]) -> Callable[[Any | None], TypeGuard[TypeCertified]]:
136
- def workhorse(node: Any | None) -> TypeGuard[TypeCertified]:
137
- return isinstance(node, antecedent)
138
- return workhorse
139
65
  @staticmethod
140
- def AnnAssign(node: TypeCertified) -> TypeGuard[ast.AnnAssign]: return be._typeCertified(ast.AnnAssign)(node) # pyright: ignore [reportInvalidTypeVarUse]
141
- # 'TypeVar "typeCertified" appears only once in generic function signature. Use "typeCertified" instead Pylance(reportInvalidTypeVarUse)"' HOW THE FUCK IS THAT INVALID WHEN IT IS WORKING PERFECTLY TO PASS THE TYPE INFORMATION--IN YOUR FUCKING STATIC TYPE CHECKER, PYLANCE!!!! Fuck you, and fuck your pretentious language.
66
+ def attr(node: ast.Attribute) -> ast_Identifier:
67
+ return node.attr
142
68
  @staticmethod
143
- def arg(node: TypeCertified) -> TypeGuard[ast.arg]: return be._typeCertified(ast.arg)(node) # pyright: ignore [reportInvalidTypeVarUse]
144
- @staticmethod
145
- def Assign(node: TypeCertified) -> TypeGuard[ast.Assign]: return be._typeCertified(ast.Assign)(node) # pyright: ignore [reportInvalidTypeVarUse]
146
- @staticmethod
147
- def Attribute(node: TypeCertified) -> TypeGuard[ast.Attribute]: return be._typeCertified(ast.Attribute)(node) # pyright: ignore [reportInvalidTypeVarUse]
148
- @staticmethod
149
- def AugAssign(node: TypeCertified) -> TypeGuard[ast.AugAssign]: return be._typeCertified(ast.AugAssign)(node) # pyright: ignore [reportInvalidTypeVarUse]
150
- @staticmethod
151
- def BoolOp(node: TypeCertified) -> TypeGuard[ast.BoolOp]: return be._typeCertified(ast.BoolOp)(node) # pyright: ignore [reportInvalidTypeVarUse]
152
- @staticmethod
153
- def Call(node: TypeCertified) -> TypeGuard[ast.Call]: return be._typeCertified(ast.Call)(node) # pyright: ignore [reportInvalidTypeVarUse]
154
- @staticmethod
155
- def ClassDef(node: TypeCertified) -> TypeGuard[ast.ClassDef]: return be._typeCertified(ast.ClassDef)(node) # pyright: ignore [reportInvalidTypeVarUse]
69
+ def func(node: ast.Call) -> ast.expr:
70
+ return node.func
156
71
  @staticmethod
157
- def Compare(node: TypeCertified) -> TypeGuard[ast.Compare]: return be._typeCertified(ast.Compare)(node) # pyright: ignore [reportInvalidTypeVarUse]
72
+ def id(node: ast.Name) -> ast_Identifier:
73
+ return node.id
74
+
158
75
  @staticmethod
159
- def Constant(node: TypeCertified) -> TypeGuard[ast.Constant]: return be._typeCertified(ast.Constant)(node) # pyright: ignore [reportInvalidTypeVarUse]
76
+ @overload
77
+ def name(node: astClassHasDOTnameNotName) -> ast_Identifier:...
160
78
  @staticmethod
161
- def Expr(node: TypeCertified) -> TypeGuard[ast.Expr]: return be._typeCertified(ast.Expr)(node) # pyright: ignore [reportInvalidTypeVarUse]
79
+ @overload
80
+ def name(node: astClassOptionallyHasDOTnameNotName) -> ast_Identifier | None:...
162
81
  @staticmethod
163
- def FunctionDef(node: TypeCertified) -> TypeGuard[ast.FunctionDef]: return be._typeCertified(ast.FunctionDef)(node) # pyright: ignore [reportInvalidTypeVarUse]
82
+ def name(node: astClassHasDOTnameNotName | astClassOptionallyHasDOTnameNotName) -> ast_Identifier | None:
83
+ return node.name
84
+
164
85
  @staticmethod
165
- def Import(node: TypeCertified) -> TypeGuard[ast.Import]: return be._typeCertified(ast.Import)(node) # pyright: ignore [reportInvalidTypeVarUse]
86
+ @overload
87
+ def target(node: ast.NamedExpr) -> ast.Name:...
166
88
  @staticmethod
167
- def ImportFrom(node: TypeCertified) -> TypeGuard[ast.ImportFrom]: return be._typeCertified(ast.ImportFrom)(node) # pyright: ignore [reportInvalidTypeVarUse]
89
+ @overload
90
+ def target(node: astClassHasDOTtarget_expr) -> ast.expr:...
168
91
  @staticmethod
169
- def keyword(node: TypeCertified) -> TypeGuard[ast.keyword]: return be._typeCertified(ast.keyword)(node) # pyright: ignore [reportInvalidTypeVarUse]
92
+ @overload
93
+ def target(node: astClassHasDOTtargetAttributeNameSubscript) -> ast.Attribute | ast.Name | ast.Subscript:...
170
94
  @staticmethod
171
- def Module(node: TypeCertified) -> TypeGuard[ast.Module]: return be._typeCertified(ast.Module)(node) # pyright: ignore [reportInvalidTypeVarUse]
95
+ def target(node: astClassHasDOTtarget) -> ast.Attribute | ast.expr | ast.Name | ast.Subscript:
96
+ return node.target
97
+
172
98
  @staticmethod
173
- def Name(node: TypeCertified) -> TypeGuard[ast.Name]: return be._typeCertified(ast.Name)(node) # pyright: ignore [reportInvalidTypeVarUse]
99
+ @overload
100
+ def value(node: ast.Constant) -> Any:...
174
101
  @staticmethod
175
- def Return(node: TypeCertified) -> TypeGuard[ast.Return]: return be._typeCertified(ast.Return)(node) # pyright: ignore [reportInvalidTypeVarUse]
102
+ @overload
103
+ def value(node: ast.MatchSingleton) -> bool | None:...
176
104
  @staticmethod
177
- def Starred(node: TypeCertified) -> TypeGuard[ast.Starred]: return be._typeCertified(ast.Starred)(node) # pyright: ignore [reportInvalidTypeVarUse]
105
+ @overload
106
+ def value(node: astClassHasDOTvalue_expr) -> ast.expr:...
178
107
  @staticmethod
179
- def Subscript(node: TypeCertified) -> TypeGuard[ast.Subscript]: return be._typeCertified(ast.Subscript)(node) # pyright: ignore [reportInvalidTypeVarUse]
108
+ @overload
109
+ def value(node: astClassHasDOTvalue_exprNone) -> ast.expr | None:...
180
110
  @staticmethod
181
- def UnaryOp(node: TypeCertified) -> TypeGuard[ast.UnaryOp]: return be._typeCertified(ast.UnaryOp)(node) # pyright: ignore [reportInvalidTypeVarUse]
111
+ def value(node: astClassHasDOTvalue) -> Any | ast.expr | bool | None:
112
+ return node.value
182
113
 
183
114
  class ifThis:
115
+ """
116
+ Provide predicate functions for matching and filtering AST nodes based on various criteria.
117
+
118
+ The ifThis class contains static methods that generate predicate functions used to test
119
+ whether AST nodes match specific criteria. These predicates can be used with NodeChanger
120
+ and NodeTourist to identify and process specific patterns in the AST.
121
+
122
+ The class provides predicates for matching various node types, attributes, identifiers,
123
+ and structural patterns, enabling precise targeting of AST elements for analysis or
124
+ transformation.
125
+ """
184
126
  @staticmethod
185
- def equals(this: Any) -> Callable[[Any], TypeGuard[Any] | bool]:
186
- return lambda node: node == this
187
- @staticmethod
188
- def isAssignAndTargets0Is(targets0Predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
189
- """node is Assign and node.targets[0] matches `targets0Predicate`."""
190
- return lambda node: be.Assign(node) and targets0Predicate(node.targets[0])
191
- @staticmethod
192
- def isAssignAndValueIs(valuePredicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
193
- """node is ast.Assign and node.value matches `valuePredicate`.
194
- Parameters:
195
- valuePredicate: Function that evaluates the value of the assignment
196
- Returns:
197
- predicate: matches assignments with values meeting the criteria
198
- """
199
- return lambda node: be.Assign(node) and 又.value(valuePredicate)(node)
200
- @staticmethod
201
- def isFunctionDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.FunctionDef] | bool]:
202
- return lambda node: be.FunctionDef(node) and 又.name(ifThis._Identifier(identifier))(node)
203
- @staticmethod
204
- def isArgument_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg | ast.keyword] | bool]:
205
- return lambda node: (be.arg(node) or be.keyword(node)) and 又.arg(ifThis._Identifier(identifier))(node)
127
+ def _Identifier(identifier: ast_Identifier) -> Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]:
128
+ return lambda node: node == identifier
206
129
  @staticmethod
207
- def is_keyword_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.keyword] | bool]:
208
- """see also `isArgument_Identifier`"""
209
- return lambda node: be.keyword(node) and 又.arg(ifThis._Identifier(identifier))(node)
130
+ def _nested_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool]:
131
+ def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool:
132
+ return ifThis.isName_Identifier(identifier)(node) or ifThis.isAttribute_Identifier(identifier)(node) or ifThis.isSubscript_Identifier(identifier)(node) or ifThis.isStarred_Identifier(identifier)(node)
133
+ return workhorse
134
+
210
135
  @staticmethod
211
136
  def is_arg_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg] | bool]:
212
137
  """see also `isArgument_Identifier`"""
213
- return lambda node: be.arg(node) and 又.arg(ifThis._Identifier(identifier))(node)
138
+ return lambda node: isinstance(node, ast.arg) and ifThis._Identifier(identifier)(DOT.arg(node))
214
139
  @staticmethod
215
- def isClassDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.ClassDef] | bool]:
216
- return lambda node: be.ClassDef(node) and 又.name(ifThis._Identifier(identifier))(node)
140
+ def is_keyword_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.keyword] | bool]:
141
+ """see also `isArgument_Identifier`"""
142
+ return lambda node: isinstance(node, ast.keyword) and ifThis._Identifier(identifier)(DOT.arg(node))
143
+
217
144
  @staticmethod
218
- def isAssignAndValueIsCall_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
219
- return lambda node: be.Assign(node) and 又.value(ifThis.isCall_Identifier(identifier))(node)
145
+ def isAnnAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
146
+ def workhorse(node: ast.AST) -> TypeGuard[ast.AnnAssign] | bool:
147
+ return isinstance(node, ast.AnnAssign) and targetPredicate(DOT.target(node))
148
+ return workhorse
149
+
220
150
  @staticmethod
221
- def isAssignAndValueIsCallAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
222
- return ifThis.isAssignAndValueIs(ifThis.isCallAttributeNamespace_Identifier(namespace, identifier))
151
+ def isArgument_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.arg | ast.keyword] | bool]:
152
+ return lambda node: (isinstance(node, ast.arg) or isinstance(node, ast.keyword)) and ifThis._Identifier(identifier)(DOT.arg(node))
153
+
223
154
  @staticmethod
224
- def is_keywordAndValueIsConstant(node: TypeCertified) -> TypeGuard[ast.keyword]: # pyright: ignore [reportInvalidTypeVarUse]
225
- return be.keyword(node) and 又.value(be.Constant)(node)
155
+ def isAssignAndTargets0Is(targets0Predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
156
+ """node is Assign and node.targets[0] matches `targets0Predicate`."""
157
+ return lambda node: isinstance(node, ast.Assign) and targets0Predicate(node.targets[0])
226
158
  @staticmethod
227
- def is_keyword_IdentifierEqualsConstantValue(identifier: ast_Identifier, ConstantValue: Any) -> Callable[[ast.AST], TypeGuard[ast.keyword] | bool]:
228
- return lambda node: ifThis.is_keyword_Identifier(identifier)(node) and ifThis.is_keywordAndValueIsConstant(node) and 又.value(ifThis.isConstantEquals(ConstantValue))(node)
159
+ def isAssignAndValueIs(valuePredicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], TypeGuard[ast.Assign] | bool]:
160
+ """node is ast.Assign and node.value matches `valuePredicate`. """
161
+ return lambda node: isinstance(node, ast.Assign) and valuePredicate(DOT.value(node))
162
+
229
163
  @staticmethod
230
- def isAnnAssign_targetIs(targetPredicate: Callable[[Ima_targetTypeUNEDITED], TypeGuard[Ima_targetTypeUNEDITED] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AnnAssign] | bool]:
231
- def workhorse(node: TypeCertified) -> TypeGuard[ast.AnnAssign] | bool: # pyright: ignore [reportInvalidTypeVarUse]
232
- return be.AnnAssign(node) and 又.target(targetPredicate)(node)
164
+ def isAttribute_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
165
+ """node is `ast.Attribute` and the top-level `ast.Name` is `identifier`"""
166
+ def workhorse(node: ast.AST) -> TypeGuard[ast.Attribute]:
167
+ return isinstance(node, ast.Attribute) and ifThis._nested_Identifier(identifier)(DOT.value(node))
233
168
  return workhorse
234
169
  @staticmethod
235
- def isAnnAssignAndAnnotationIsName(node: TypeCertified) -> TypeGuard[ast.AnnAssign] | bool: # pyright: ignore [reportInvalidTypeVarUse]
236
- return be.AnnAssign(node) and be.Name(DOT.annotation(node))
237
- # return be.AnnAssign(node) and 又.annotation(be.Name)(node)
170
+ def isAttributeName(node: ast.AST) -> TypeGuard[ast.Attribute]:
171
+ """ Displayed as Name.attribute."""
172
+ return isinstance(node, ast.Attribute) and isinstance(DOT.value(node), ast.Name)
173
+ @staticmethod
174
+ def isAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
175
+ return lambda node: ifThis.isAttributeName(node) and ifThis.isName_Identifier(namespace)(DOT.value(node)) and ifThis._Identifier(identifier)(DOT.attr(node))
176
+
238
177
  @staticmethod
239
- def isAugAssign_targetIs(targetPredicate: Callable[[Ima_targetTypeUNEDITED], TypeGuard[Ima_targetTypeUNEDITED] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AugAssign] | bool]:
240
- def workhorse(node: TypeCertified) -> TypeGuard[ast.AugAssign] | bool: # pyright: ignore [reportInvalidTypeVarUse]
241
- return be.AugAssign(node) and 又.target(targetPredicate)(node)
178
+ def isAugAssign_targetIs(targetPredicate: Callable[[ast.expr], TypeGuard[ast.expr] | bool]) -> Callable[[ast.AST], TypeGuard[ast.AugAssign] | bool]:
179
+ def workhorse(node: ast.AST) -> TypeGuard[ast.AugAssign] | bool:
180
+ return isinstance(node, ast.AugAssign) and targetPredicate(DOT.target(node))
242
181
  return workhorse
243
182
 
244
183
  @staticmethod
245
- def isAnyCompare(node: TypeCertified) -> TypeGuard[ast.BoolOp | ast.Compare]: # pyright: ignore [reportInvalidTypeVarUse]
246
- return be.Compare(node) or be.BoolOp(node)
184
+ def isCall_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
185
+ return lambda node: isinstance(node, ast.Call) and ifThis.isName_Identifier(identifier)(DOT.func(node))
247
186
  @staticmethod
248
- def isConstantEquals(value: Any) -> Callable[[ast.AST], TypeGuard[ast.Constant] | bool]:
249
- return lambda node: be.Constant(node) and 又.value(ifThis.equals(value))(node)
187
+ def isCallAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
188
+ return lambda node: isinstance(node, ast.Call) and ifThis.isAttributeNamespace_Identifier(namespace, identifier)(DOT.func(node))
250
189
  @staticmethod
251
- def isReturnAnyCompare(node: TypeCertified) -> TypeGuard[ast.Return] | bool: # pyright: ignore [reportInvalidTypeVarUse] # pyright: ignore [reportInvalidTypeVarUse]
252
- return be.Return(node) and 又.value(ifThis.isAnyCompare)(node)
190
+ def isCallToName(node: ast.AST) -> TypeGuard[ast.Call]:
191
+ return isinstance(node, ast.Call) and isinstance(DOT.func(node), ast.Name)
192
+
253
193
  @staticmethod
254
- def isReturnUnaryOp(node: TypeCertified) -> TypeGuard[ast.Return] | bool: # pyright: ignore [reportInvalidTypeVarUse]
255
- return be.Return(node) and 又.value(be.UnaryOp)(node)
194
+ def isClassDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.ClassDef] | bool]:
195
+ return lambda node: isinstance(node, ast.ClassDef) and ifThis._Identifier(identifier)(DOT.name(node))
256
196
 
257
- # ================================================================
258
- # Nested identifier
259
197
  @staticmethod
260
- def _nestedJunction_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool]:
261
- def workhorse(node: TypeCertified) -> TypeGuard[ast.Attribute | ast.Starred | ast.Subscript] | bool: # pyright: ignore [reportInvalidTypeVarUse]
262
- return ifThis.isName_Identifier(identifier)(node) or ifThis.isAttribute_Identifier(identifier)(node) or ifThis.isSubscript_Identifier(identifier)(node) or ifThis.isStarred_Identifier(identifier)(node)
263
- return workhorse
198
+ def isFunctionDef_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.FunctionDef] | bool]:
199
+ return lambda node: isinstance(node, ast.FunctionDef) and ifThis._Identifier(identifier)(DOT.name(node))
200
+
264
201
  @staticmethod
265
- def isAttribute_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
266
- """node is `ast.Attribute` and the top-level `ast.Name` is `identifier`"""
267
- def workhorse(node: TypeCertified) -> TypeGuard[ast.Attribute]: # pyright: ignore [reportInvalidTypeVarUse]
268
- return be.Attribute(node) and 又.value(ifThis._nestedJunction_Identifier(identifier))(node)
269
- return workhorse
202
+ def isName_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Name] | bool]:
203
+ return lambda node: isinstance(node, ast.Name) and ifThis._Identifier(identifier)(DOT.id(node))
204
+
270
205
  @staticmethod
271
206
  def isStarred_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Starred] | bool]:
272
207
  """node is `ast.Starred` and the top-level `ast.Name` is `identifier`"""
273
- def workhorse(node: TypeCertified) -> TypeGuard[ast.Starred]: # pyright: ignore [reportInvalidTypeVarUse]
274
- return be.Starred(node) and 又.value(ifThis._nestedJunction_Identifier(identifier))(node)
208
+ def workhorse(node: ast.AST) -> TypeGuard[ast.Starred]:
209
+ return isinstance(node, ast.Starred) and ifThis._nested_Identifier(identifier)(DOT.value(node))
275
210
  return workhorse
276
211
  @staticmethod
277
212
  def isSubscript_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Subscript] | bool]:
278
213
  """node is `ast.Subscript` and the top-level `ast.Name` is `identifier`"""
279
- def workhorse(node: TypeCertified) -> TypeGuard[ast.Subscript]: # pyright: ignore [reportInvalidTypeVarUse]
280
- return be.Subscript(node) and 又.value(ifThis._nestedJunction_Identifier(identifier))(node)
281
- return workhorse
282
- # ================================================================
283
-
284
- @staticmethod
285
- def Z0Z_unparseIs(astAST: ast.AST) -> Callable[[ast.AST], bool]:
286
- def workhorse(node: TypeCertified) -> bool: return ast.unparse(node) == ast.unparse(astAST) # pyright: ignore [reportInvalidTypeVarUse]
214
+ def workhorse(node: ast.AST) -> TypeGuard[ast.Subscript]:
215
+ return isinstance(node, ast.Subscript) and ifThis._nested_Identifier(identifier)(DOT.value(node))
287
216
  return workhorse
288
217
 
289
- # ================================================================
290
- # NOT used
291
- # TODO Does this work?
292
- @staticmethod
293
- def Z0Z_matchesAtLeast1Descendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
294
- """Create a predicate that returns True if any descendant of the node matches the given predicate."""
295
- return lambda node: not ifThis.matchesNoDescendant(predicate)(node)
296
- # ================================================================
297
- # MORE function inlining
298
- @staticmethod
299
- def onlyReturnAnyCompare(astFunctionDef: ast.AST) -> TypeGuard[ast.FunctionDef] | bool:
300
- return be.FunctionDef(astFunctionDef) and len(astFunctionDef.body) == 1 and ifThis.isReturnAnyCompare(astFunctionDef.body[0])
301
- # For function inlining
302
- @staticmethod
303
- def onlyReturnUnaryOp(astFunctionDef: ast.AST) -> TypeGuard[ast.FunctionDef] | bool:
304
- return be.FunctionDef(astFunctionDef) and len(astFunctionDef.body) == 1 and ifThis.isReturnUnaryOp(astFunctionDef.body[0])
305
- # ================================================================
306
- # These are used by other functions
307
- @staticmethod
308
- def isCallAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
309
- return lambda node: be.Call(node) and 又.func(ifThis.isAttributeNamespace_Identifier(namespace, identifier))(node)
310
- @staticmethod
311
- def isName_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Name] | bool]:
312
- return lambda node: be.Name(node) and 又.id(ifThis._Identifier(identifier))(node)
313
- @staticmethod
314
- def isCall_Identifier(identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
315
- return lambda node: be.Call(node) and 又.func(ifThis.isName_Identifier(identifier))(node)
316
- # ================================================================
317
- @staticmethod
318
- def CallDoesNotCallItself(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
319
- """If `namespace` is not applicable to your case, then call with `namespace=""`."""
320
- return lambda node: ifThis.matchesMeButNotAnyDescendant(ifThis.CallReallyIs(namespace, identifier))(node)
321
218
  @staticmethod
322
219
  def matchesMeButNotAnyDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
323
- """Create a predicate that returns True if the node matches but none of its descendants match the predicate."""
324
220
  return lambda node: predicate(node) and ifThis.matchesNoDescendant(predicate)(node)
325
221
  @staticmethod
326
222
  def matchesNoDescendant(predicate: Callable[[ast.AST], bool]) -> Callable[[ast.AST], bool]:
327
- """Create a predicate that returns True if no descendant of the node matches the given predicate."""
328
- def workhorse(node: TypeCertified) -> bool: # pyright: ignore [reportInvalidTypeVarUse]
223
+ def workhorse(node: ast.AST) -> bool:
329
224
  for descendant in ast.walk(node):
330
225
  if descendant is not node and predicate(descendant):
331
226
  return False
@@ -333,26 +228,6 @@ class ifThis:
333
228
  return workhorse
334
229
 
335
230
  @staticmethod
336
- def CallReallyIs(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
337
- return ifThis.isCall_Identifier(identifier) or ifThis.isCallAttributeNamespace_Identifier(namespace, identifier)
338
- @staticmethod
339
- def isAttributeNamespace_Identifier(namespace: ast_Identifier, identifier: ast_Identifier) -> Callable[[ast.AST], TypeGuard[ast.Attribute] | bool]:
340
- return lambda node: ifThis.isAttributeName(node) and 又.value(ifThis.isName_Identifier(namespace))(node) and 又.attr(ifThis._Identifier(identifier))(node)
341
- @staticmethod
342
- def _Identifier(identifier: ast_Identifier) -> Callable[[ast_Identifier | None], TypeGuard[ast_Identifier] | bool]:
343
- return lambda node: node == identifier
344
- @staticmethod
345
- def isAttributeName(node: TypeCertified) -> TypeGuard[ast.Attribute]: # pyright: ignore [reportInvalidTypeVarUse]
346
- """ Displayed as Name.attribute."""
347
- return be.Attribute(node) and 又.value(be.Name)(node)
348
-
349
- @staticmethod
350
- def isCallToName(node: TypeCertified) -> TypeGuard[ast.Call]: # pyright: ignore [reportInvalidTypeVarUse]
351
- return be.Call(node) and 又.func(be.Name)(node)
352
- @staticmethod
353
- def ast_IdentifierIn(container: Container[ast_Identifier]) -> Callable[[ast_Identifier], TypeGuard[ast_Identifier] | bool]:
354
- return lambda node: node in container
355
- # This bullshit is for the crappy function inliner I made.
356
- @staticmethod
357
- def CallDoesNotCallItselfAndNameDOTidIsIn(container: Container[ast_Identifier]) -> Callable[[ast.AST], TypeGuard[ast.Call] | bool]:
358
- return lambda node: ifThis.isCallToName(node) and 又.func(又.id(ifThis.ast_IdentifierIn(container)))(node) and ifThis.CallDoesNotCallItself("", node.func.id)(node) # type: ignore
231
+ def Z0Z_unparseIs(astAST: ast.AST) -> Callable[[ast.AST], bool]:
232
+ def workhorse(node: ast.AST) -> bool: return ast.unparse(node) == ast.unparse(astAST)
233
+ return workhorse