Skip to content

Commit 5e5bfc0

Browse files
committed
toolz, FTW!
1 parent 5523c8e commit 5e5bfc0

8 files changed

Lines changed: 2861 additions & 2782 deletions

File tree

astToolkit/_astTypes.py

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,37 @@
66
intORstrORtype_params: typing_TypeAlias = Any
77
intORtype_params: typing_TypeAlias = Any
88
yourPythonIsOld: typing_TypeAlias = Any
9-
if sys.version_info >= (3, 11):
10-
from ast import TryStar as astDOTTryStar
11-
else:
12-
astDOTTryStar: typing_TypeAlias = yourPythonIsOld
13-
if sys.version_info >= (3, 12):
14-
from ast import ParamSpec as astDOTParamSpec, type_param as astDOTtype_param, TypeAlias as astDOTTypeAlias, TypeVar as astDOTTypeVar, TypeVarTuple as astDOTTypeVarTuple
15-
else:
16-
astDOTParamSpec: typing_TypeAlias = yourPythonIsOld
17-
astDOTtype_param: typing_TypeAlias = yourPythonIsOld
18-
astDOTTypeAlias: typing_TypeAlias = yourPythonIsOld
19-
astDOTTypeVar: typing_TypeAlias = yourPythonIsOld
20-
astDOTTypeVarTuple: typing_TypeAlias = yourPythonIsOld
21-
hasDOTannotation_expr: typing_TypeAlias = ast.AnnAssign
22-
hasDOTannotation_exprOrNone: typing_TypeAlias = ast.arg
23-
hasDOTannotation: typing_TypeAlias = hasDOTannotation_expr | hasDOTannotation_exprOrNone
24-
hasDOTarg_Identifier: typing_TypeAlias = ast.arg
25-
hasDOTarg_IdentifierOrNone: typing_TypeAlias = ast.keyword
26-
hasDOTarg: typing_TypeAlias = hasDOTarg_Identifier | hasDOTarg_IdentifierOrNone
27-
hasDOTargs_arguments: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef | ast.Lambda
28-
hasDOTargs_list_expr: typing_TypeAlias = ast.Call
29-
hasDOTargs_list_arg: typing_TypeAlias = ast.arguments
30-
hasDOTargs: typing_TypeAlias = hasDOTargs_arguments | hasDOTargs_list_expr | hasDOTargs_list_arg
319
hasDOTargtypes: typing_TypeAlias = ast.FunctionType
3210
hasDOTasname: typing_TypeAlias = ast.alias
3311
hasDOTattr: typing_TypeAlias = ast.Attribute
3412
hasDOTbases: typing_TypeAlias = ast.ClassDef
35-
hasDOTbody_list_stmt: typing_TypeAlias = ast.Module | ast.Interactive | ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef | ast.For | ast.AsyncFor | ast.While | ast.If | ast.With | ast.AsyncWith | ast.Try | ast.ExceptHandler | astDOTTryStar | ast.match_case
36-
hasDOTbody_expr: typing_TypeAlias = ast.Expression | ast.Lambda | ast.IfExp
37-
hasDOTbody: typing_TypeAlias = hasDOTbody_list_stmt | hasDOTbody_expr
38-
hasDOTbound: typing_TypeAlias = astDOTTypeVar
13+
if sys.version_info >= (3, 12):
14+
hasDOTbound: typing_TypeAlias = ast.TypeVar
3915
hasDOTcases: typing_TypeAlias = ast.Match
4016
hasDOTcause: typing_TypeAlias = ast.Raise
4117
hasDOTcls: typing_TypeAlias = ast.MatchClass
4218
hasDOTcomparators: typing_TypeAlias = ast.Compare
4319
hasDOTcontext_expr: typing_TypeAlias = ast.withitem
4420
hasDOTconversion: typing_TypeAlias = ast.FormattedValue
45-
hasDOTctx: typing_TypeAlias = ast.Attribute | ast.Subscript | ast.Starred | ast.Name | ast.List | ast.Tuple
46-
hasDOTdecorator_list: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef
47-
hasDOTdefault_value: typing_TypeAlias = astDOTTypeVar | astDOTParamSpec | astDOTTypeVarTuple
21+
hasDOTctx: typing_TypeAlias = ast.Attribute | ast.List | ast.Name | ast.Starred | ast.Subscript | ast.Tuple
22+
hasDOTdecorator_list: typing_TypeAlias = ast.AsyncFunctionDef | ast.ClassDef | ast.FunctionDef
23+
if sys.version_info >= (3, 13):
24+
hasDOTdefault_value: typing_TypeAlias = ast.ParamSpec | ast.TypeVar | ast.TypeVarTuple
4825
hasDOTdefaults: typing_TypeAlias = ast.arguments
49-
hasDOTelt: typing_TypeAlias = ast.ListComp | ast.SetComp | ast.GeneratorExp
50-
hasDOTelts: typing_TypeAlias = ast.Set | ast.List | ast.Tuple
26+
hasDOTelt: typing_TypeAlias = ast.GeneratorExp | ast.ListComp | ast.SetComp
27+
hasDOTelts: typing_TypeAlias = ast.List | ast.Set | ast.Tuple
5128
hasDOTexc: typing_TypeAlias = ast.Raise
52-
hasDOTfinalbody: typing_TypeAlias = ast.Try | astDOTTryStar
5329
hasDOTformat_spec: typing_TypeAlias = ast.FormattedValue
5430
hasDOTfunc: typing_TypeAlias = ast.Call
55-
hasDOTgenerators: typing_TypeAlias = ast.ListComp | ast.SetComp | ast.DictComp | ast.GeneratorExp
31+
hasDOTgenerators: typing_TypeAlias = ast.DictComp | ast.GeneratorExp | ast.ListComp | ast.SetComp
5632
hasDOTguard: typing_TypeAlias = ast.match_case
57-
hasDOThandlers: typing_TypeAlias = ast.Try | astDOTTryStar
5833
hasDOTid: typing_TypeAlias = ast.Name
5934
hasDOTifs: typing_TypeAlias = ast.comprehension
6035
hasDOTis_async: typing_TypeAlias = ast.comprehension
61-
hasDOTitems: typing_TypeAlias = ast.With | ast.AsyncWith
62-
hasDOTiter: typing_TypeAlias = ast.For | ast.AsyncFor | ast.comprehension
36+
hasDOTitems: typing_TypeAlias = ast.AsyncWith | ast.With
37+
hasDOTiter: typing_TypeAlias = ast.AsyncFor | ast.comprehension | ast.For
6338
hasDOTkey: typing_TypeAlias = ast.DictComp
64-
hasDOTkeys_list_exprOrNone: typing_TypeAlias = ast.Dict
65-
hasDOTkeys_list_expr: typing_TypeAlias = ast.MatchMapping
66-
hasDOTkeys: typing_TypeAlias = hasDOTkeys_list_exprOrNone | hasDOTkeys_list_expr
67-
hasDOTkeywords: typing_TypeAlias = ast.ClassDef | ast.Call
39+
hasDOTkeywords: typing_TypeAlias = ast.Call | ast.ClassDef
6840
hasDOTkind: typing_TypeAlias = ast.Constant
6941
hasDOTkw_defaults: typing_TypeAlias = ast.arguments
7042
hasDOTkwarg: typing_TypeAlias = ast.arguments
@@ -77,54 +49,25 @@
7749
hasDOTlower: typing_TypeAlias = ast.Slice
7850
hasDOTmodule: typing_TypeAlias = ast.ImportFrom
7951
hasDOTmsg: typing_TypeAlias = ast.Assert
80-
hasDOTname_Identifier: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef | astDOTTypeVar | astDOTParamSpec | astDOTTypeVarTuple
81-
hasDOTname_IdentifierOrNone: typing_TypeAlias = ast.ExceptHandler | ast.MatchStar | ast.MatchAs
82-
hasDOTname_str: typing_TypeAlias = ast.alias
83-
hasDOTname_Name: typing_TypeAlias = astDOTTypeAlias
84-
hasDOTname: typing_TypeAlias = hasDOTname_Identifier | hasDOTname_IdentifierOrNone | hasDOTname_str | hasDOTname_Name
85-
hasDOTnames_list_alias: typing_TypeAlias = ast.Import | ast.ImportFrom
86-
hasDOTnames_list_Identifier: typing_TypeAlias = ast.Global | ast.Nonlocal
87-
hasDOTnames: typing_TypeAlias = hasDOTnames_list_alias | hasDOTnames_list_Identifier
88-
hasDOTop_operator: typing_TypeAlias = ast.AugAssign | ast.BinOp
89-
hasDOTop_boolop: typing_TypeAlias = ast.BoolOp
90-
hasDOTop_unaryop: typing_TypeAlias = ast.UnaryOp
91-
hasDOTop: typing_TypeAlias = hasDOTop_operator | hasDOTop_boolop | hasDOTop_unaryop
9252
hasDOToperand: typing_TypeAlias = ast.UnaryOp
9353
hasDOTops: typing_TypeAlias = ast.Compare
9454
hasDOToptional_vars: typing_TypeAlias = ast.withitem
95-
hasDOTorelse_list_stmt: typing_TypeAlias = ast.For | ast.AsyncFor | ast.While | ast.If | ast.Try | astDOTTryStar
96-
hasDOTorelse_expr: typing_TypeAlias = ast.IfExp
97-
hasDOTorelse: typing_TypeAlias = hasDOTorelse_list_stmt | hasDOTorelse_expr
98-
hasDOTpattern_Pattern: typing_TypeAlias = ast.match_case
99-
hasDOTpattern_patternOrNone: typing_TypeAlias = ast.MatchAs
100-
hasDOTpattern: typing_TypeAlias = hasDOTpattern_Pattern | hasDOTpattern_patternOrNone
101-
hasDOTpatterns: typing_TypeAlias = ast.MatchSequence | ast.MatchMapping | ast.MatchClass | ast.MatchOr
55+
hasDOTpatterns: typing_TypeAlias = ast.MatchClass | ast.MatchMapping | ast.MatchOr | ast.MatchSequence
10256
hasDOTposonlyargs: typing_TypeAlias = ast.arguments
10357
hasDOTrest: typing_TypeAlias = ast.MatchMapping
104-
hasDOTreturns_expr: typing_TypeAlias = ast.FunctionType
105-
hasDOTreturns_exprOrNone: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef
106-
hasDOTreturns: typing_TypeAlias = hasDOTreturns_expr | hasDOTreturns_exprOrNone
10758
hasDOTright: typing_TypeAlias = ast.BinOp
10859
hasDOTsimple: typing_TypeAlias = ast.AnnAssign
10960
hasDOTslice: typing_TypeAlias = ast.Subscript
11061
hasDOTstep: typing_TypeAlias = ast.Slice
11162
hasDOTsubject: typing_TypeAlias = ast.Match
11263
hasDOTtag: typing_TypeAlias = ast.TypeIgnore
113-
hasDOTtarget_NameOrAttributeOrSubscript: typing_TypeAlias = ast.AugAssign | ast.AnnAssign
114-
hasDOTtarget_expr: typing_TypeAlias = ast.For | ast.AsyncFor | ast.comprehension
115-
hasDOTtarget_Name: typing_TypeAlias = ast.NamedExpr
116-
hasDOTtarget: typing_TypeAlias = hasDOTtarget_NameOrAttributeOrSubscript | hasDOTtarget_expr | hasDOTtarget_Name
117-
hasDOTtargets: typing_TypeAlias = ast.Delete | ast.Assign
118-
hasDOTtest: typing_TypeAlias = ast.While | ast.If | ast.Assert | ast.IfExp
64+
hasDOTtargets: typing_TypeAlias = ast.Assign | ast.Delete
65+
hasDOTtest: typing_TypeAlias = ast.Assert | ast.If | ast.IfExp | ast.While
11966
hasDOTtype: typing_TypeAlias = ast.ExceptHandler
120-
hasDOTtype_comment: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef | ast.Assign | ast.For | ast.AsyncFor | ast.With | ast.AsyncWith | ast.arg
67+
hasDOTtype_comment: typing_TypeAlias = ast.arg | ast.Assign | ast.AsyncFor | ast.AsyncFunctionDef | ast.AsyncWith | ast.For | ast.FunctionDef | ast.With
12168
hasDOTtype_ignores: typing_TypeAlias = ast.Module
122-
hasDOTtype_params: typing_TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef | astDOTTypeAlias
69+
if sys.version_info >= (3, 12):
70+
hasDOTtype_params: typing_TypeAlias = ast.AsyncFunctionDef | ast.ClassDef | ast.FunctionDef | ast.TypeAlias
12371
hasDOTupper: typing_TypeAlias = ast.Slice
124-
hasDOTvalue_exprOrNone: typing_TypeAlias = ast.Return | ast.AnnAssign | ast.Yield
125-
hasDOTvalue_expr: typing_TypeAlias = ast.Assign | ast.AugAssign | ast.Expr | ast.NamedExpr | ast.DictComp | ast.Await | ast.YieldFrom | ast.FormattedValue | ast.Attribute | ast.Subscript | ast.Starred | ast.keyword | astDOTTypeAlias | ast.MatchValue
126-
hasDOTvalue_Any: typing_TypeAlias = ast.Constant
127-
hasDOTvalue_LiteralTrueFalseOrNone: typing_TypeAlias = ast.MatchSingleton
128-
hasDOTvalue: typing_TypeAlias = hasDOTvalue_exprOrNone | hasDOTvalue_expr | hasDOTvalue_Any | hasDOTvalue_LiteralTrueFalseOrNone
12972
hasDOTvalues: typing_TypeAlias = ast.BoolOp | ast.Dict | ast.JoinedStr
13073
hasDOTvararg: typing_TypeAlias = ast.arguments

toolFactory/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from toolFactory.datacenter import (
2424
getElementsBe as getElementsBe,
25+
getElementsMake as getElementsMake,
2526
getElementsTypeAlias as getElementsTypeAlias,
2627
)
27-
28-
from toolFactory.astFactory import makeTools as makeTools

toolFactory/astFactory.py

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
from collections.abc import Iterable
21
from pathlib import PurePosixPath
32
from string import ascii_letters
4-
from toolFactory import ast_Identifier, fileExtension, pathPackage, str_nameDOTname, sys_version_infoTarget
3+
from toolFactory import fileExtension, pathPackage, str_nameDOTname, sys_version_infoTarget
54
from toolFactory.Z0Z_hardcoded import keywordArgumentsIdentifier, listASTClassesPostPythonVersionMinimum, moduleIdentifierPrefix
65
from toolFactory._snippets import astName_overload, astName_staticmethod, astName_typing_TypeAlias
76
from toolFactory.factory_annex import (
87
FunctionDefMake_Attribute,
98
handmadeMethodsGrab,
109
listHandmadeTypeAlias_astTypes,
11-
listPylanceErrors,
12-
MakeImportFunctionDef,
10+
FunctionDefMake_Import,
1311
)
1412
from toolFactory.docstrings import ClassDefDocstringDOT, ClassDefDocstringGrab, ClassDefDocstringMake, docstringWarning
1513
from typing import cast, TypedDict
@@ -54,13 +52,13 @@ class Name(expr):
5452

5553
class AnnotationsAndDefs(TypedDict):
5654
astAnnotation: ast.expr
57-
listClassDefIdentifier: list[ast_Identifier | str_nameDOTname]
55+
listClassDefIdentifier: list[str | str_nameDOTname]
5856

5957
class MakeDictionaryOf_astClassAnnotations(ast.NodeVisitor):
6058
def __init__(self, astAST: ast.AST) -> None:
6159
super().__init__()
6260
self.astAST = astAST
63-
self.dictionarySubstitutions: dict[ast_Identifier, ast.Attribute | ast.Name] = {
61+
self.dictionarySubstitutions: dict[str, ast.Attribute | ast.Name] = {
6462
'_Identifier': ast.Name('ast_Identifier'),
6563
'_Pattern': ast.Attribute(value=ast.Name('ast'), attr='pattern'),
6664
'_Slice': ast.Name('ast_expr_Slice'),
@@ -74,29 +72,12 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
7472
NameOrAttribute = ast.Attribute(value=ast.Name('ast'), attr=node.name)
7573
self.dictionarySubstitutions[node.name] = NameOrAttribute
7674

77-
def getDictionary(self) -> dict[ast_Identifier, ast.Attribute | ast.Name]:
75+
def getDictionary(self) -> dict[str, ast.Attribute | ast.Name]:
7876
self.visit(self.astAST)
7977
return self.dictionarySubstitutions
8078

8179
class Prepend_ast2astClasses(ast.NodeTransformer):
82-
"""The _effect_ of this `NodeTransformer` is to replace a naked `Jabberwocky` identifier with the specific
83-
`ast.Jabberwocky` identifier.
84-
85-
The explanation, however, sounds like "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo."
86-
87-
Initialize `ast.NodeTransformer` with mapping from subclass `ast._Identifier` of class `AST`, which is implemented
88-
in C, imported from module `_ast`, and defined in stub `ast.pyi`, to subclass `ast.Attribute`, rendered as
89-
"ast._Identifier" or subclass `ast.Name` for `AST` subclasses implemented later than Python version 3.10, rendered
90-
as "astDOT_Identifier" and defined as `typing.TypeAlias` or `typing.Any` depending on the runtime Python version in
91-
"__init__".
92-
93-
Call method `visit` of `ast.NodeTransformer` with class `AST` or `AST` subclass parameter. `ast.NodeTransformer`
94-
will call `visit_Name` to visit each node descendant class `AST` or `AST` subclass parameter of class `ast.Name`.
95-
The class `ast.Name` descendant node identifier is `node`. If method `visit_Name` matches subclass `ast._Identifier`
96-
`node.id` to a mapping key, it returns the subclass `ast.Attribute` or subclass `ast.Name` mapping value. Otherwise,
97-
`visit_Name` returns subclass `ast.Name` `node`.
98-
"""
99-
def __init__(self, dictionarySubstitutions: dict[ast_Identifier, ast.Attribute | ast.Name]) -> None:
80+
def __init__(self, dictionarySubstitutions: dict[str, ast.Attribute | ast.Name]) -> None:
10081
super().__init__()
10182
self.dictionarySubstitutions = dictionarySubstitutions
10283

@@ -106,7 +87,7 @@ def visit_Name(self, node: ast.Name) -> ast.Attribute | ast.Name:
10687
return node
10788

10889
def makeTools(astStubFile: ast.AST) -> None:
109-
def writeModule(astModule: ast.Module, moduleIdentifier: ast_Identifier) -> None:
90+
def writeModule(astModule: ast.Module, moduleIdentifier: str) -> None:
11091
ast.fix_missing_locations(astModule)
11192
pythonSource: str = ast.unparse(astModule)
11293
if 'Grab' in moduleIdentifier or 'DOT' in moduleIdentifier or 'ClassIsAndAttribute' in moduleIdentifier:
@@ -148,9 +129,9 @@ def writeModule(astModule: ast.Module, moduleIdentifier: ast_Identifier) -> None
148129
ClassDefMake = ast.ClassDef(name='Make', bases=[], keywords=[], body=[], decorator_list=[])
149130
ClassDefGrab = ast.ClassDef(name='Grab', bases=[], keywords=[], body=[], decorator_list=[])
150131

151-
dictionaryOf_astDOTclass: dict[ast_Identifier, ast.Attribute | ast.Name] = MakeDictionaryOf_astClassAnnotations(astStubFile).getDictionary()
132+
dictionaryOf_astDOTclass: dict[str, ast.Attribute | ast.Name] = MakeDictionaryOf_astClassAnnotations(astStubFile).getDictionary()
152133

153-
attributeIdentifier2Str4TypeAlias2astAnnotationAndListClassDefIdentifier: dict[ast_Identifier, dict[str, AnnotationsAndDefs]] = {}
134+
attributeIdentifier2Str4TypeAlias2astAnnotationAndListClassDefIdentifier: dict[str, dict[str, AnnotationsAndDefs]] = {}
154135

155136
# NOTE Convert each ast.ClassDef into `TypeAlias` and methods in `Be`, `DOT`, `Grab`, and `Make`.
156137
for node in ast.walk(astStubFile):
@@ -167,7 +148,7 @@ def writeModule(astModule: ast.Module, moduleIdentifier: ast_Identifier) -> None
167148
del node # NOTE this is necessary because AI assistants don't always follow instructions.
168149

169150
# Create ast "fragments" before you need them.
170-
ClassDefIdentifier: ast_Identifier = astDOTClassDef.name
151+
ClassDefIdentifier: str = astDOTClassDef.name
171152
ClassDef_astNameOrAttribute: ast.Attribute | ast.Name = dictionaryOf_astDOTclass[ClassDefIdentifier]
172153
# Reset these identifiers in case they were changed
173154
keywordArguments_ast_arg: ast.arg | None = ast.arg(keywordArgumentsIdentifier, ast.Name('int'))
@@ -181,9 +162,9 @@ def writeModule(astModule: ast.Module, moduleIdentifier: ast_Identifier) -> None
181162

182163
# Start: cope with different arguments for Python versions. ==============================================================
183164
# NOTE: I would love suggestions to improve this section.
184-
list_astDOTClassDefAttributeIdentifier: list[ast_Identifier] = []
185-
list__match_args__: list[list[ast_Identifier]] = []
186-
dictAttributes: dict[tuple[int, int], list[ast_Identifier]] = {}
165+
list_astDOTClassDefAttributeIdentifier: list[str] = []
166+
list__match_args__: list[list[str]] = []
167+
dictAttributes: dict[tuple[int, int], list[str]] = {}
187168
for subnode in ast.walk(astDOTClassDef):
188169
list_astDOTClassDefAttributeIdentifier = []
189170
if (isinstance(subnode, ast.If) and isinstance(subnode.test, ast.Compare)
@@ -265,7 +246,7 @@ def writeModule(astModule: ast.Module, moduleIdentifier: ast_Identifier) -> None
265246
case 'Import':
266247
if cast(ast.FunctionDef, ClassDefMake.body[-1]).name == ClassDefIdentifier:
267248
ClassDefMake.body.pop(-1)
268-
ClassDefMake.body.append(MakeImportFunctionDef)
249+
ClassDefMake.body.append(FunctionDefMake_Import)
269250
continue
270251
case _:
271252
pass
@@ -397,11 +378,11 @@ def list2Sequence():
397378
, type_ignores=[]
398379
)
399380

400-
listAttributeIdentifier: list[ast_Identifier] = list(attributeIdentifier2Str4TypeAlias2astAnnotationAndListClassDefIdentifier.keys())
381+
listAttributeIdentifier: list[str] = list(attributeIdentifier2Str4TypeAlias2astAnnotationAndListClassDefIdentifier.keys())
401382
listAttributeIdentifier.sort(key=lambda attributeIdentifier: attributeIdentifier.lower())
402383

403384
for attributeIdentifier in listAttributeIdentifier:
404-
hasDOTIdentifier: ast_Identifier = 'hasDOT' + attributeIdentifier
385+
hasDOTIdentifier: str = 'hasDOT' + attributeIdentifier
405386
hasDOTName_Store: ast.Name = ast.Name(hasDOTIdentifier, ast.Store())
406387
hasDOTName_Load: ast.Name = ast.Name(hasDOTIdentifier)
407388
list_hasDOTNameTypeAliasAnnotations: list[ast.Name] = []

0 commit comments

Comments
 (0)