1- # pyright: reportUnknownMemberType = false
2- # pyright: reportAttributeAccessIssue = false
31from collections import defaultdict
42from itertools import chain
53from pathlib import PurePosixPath
64from toolFactory import (
75 astName_overload ,
86 astName_staticmethod ,
97 astName_typing_TypeAlias ,
8+ BitOr ,
109 DictionaryAstExprType ,
1110 DictionaryClassDef ,
1211 DictionaryMatchArgs ,
2625 )
2726from toolFactory .datacenter import getElementsGrab
2827from toolFactory .factory_annex import (
28+ astAssign_EndPositionT ,
29+ astClassDef_Attributes ,
2930 FunctionDefGrab_andDoAllOf ,
3031 FunctionDefMake_Attribute ,
3132 FunctionDefMake_Import ,
32- listHandmadeTypeAlias_astTypes ,
33+ listHandmade_astTypes ,
3334)
3435from toolFactory .docstrings import ClassDefDocstringBe , ClassDefDocstringClassIsAndAttribute , ClassDefDocstringGrab , ClassDefDocstringMake , ClassDefDocstringDOT , docstringWarning
3536from typing import cast
@@ -161,7 +162,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
161162 astNameTypeAlias : ast .Name = ast .Name (formatTypeAliasSubcategory .format (hasDOTIdentifier = hasDOTIdentifier , TypeAliasSubcategory = TypeAliasSubcategory ))
162163 body : list [ast .stmt ] = [ast .Expr (ast .Constant (value = ...))]
163164 decorator_list = [astName_staticmethod , astName_overload ]
164- returns = ast .Subscript (ast .Name ('Callable' ), ast .Tuple ([ast .List ([ast .Attribute (ast .Name ('ast' ), attr = 'AST' )]), ast . BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )])]))
165+ returns = ast .Subscript (ast .Name ('Callable' ), ast .Tuple ([ast .List ([ast .Attribute (ast .Name ('ast' ), attr = 'AST' )]), BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )])]))
165166
166167 Z0Z_TypeWithoutNone = eval (dictionary_ast_exprType ['ast_exprType' ])
167168 annotation = ast .Subscript (ast .Name ('Callable' ), ast .Tuple ([ast .List ([Z0Z_TypeWithoutNone ]), ast .Name ('bool' )]))
@@ -176,7 +177,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
176177 attributeVersionMinorMinimum : int = min (dictionaryVersionsTypeAliasSubcategory .keys ())
177178 decorator_list : list [ast .expr ] = [astName_staticmethod ]
178179
179- annotation = cast ( ast . expr , ast . BitOr .join (dictionaryVersionsTypeAliasSubcategory [attributeVersionMinorMinimum ]) )
180+ annotation = BitOr .join (dictionaryVersionsTypeAliasSubcategory [attributeVersionMinorMinimum ])
180181 workhorseReturnValue : ast .BoolOp = ast .BoolOp (op = ast .And (), values = [ast .Call (ast .Name ('isinstance' ), args = [ast .Name ('node' ), ast .Name ('astClass' )], keywords = [])])
181182 for node in ast .walk (annotation ):
182183 if isinstance (node , ast .Subscript ) and isinstance (node .value , ast .Name ) and node .value .id == 'Sequence' and isinstance (node .slice , ast .BinOp ) and isinstance (node .slice .right , ast .Constant ) and node .slice .right .value is None :
@@ -192,7 +193,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
192193
193194 workhorseReturnValue .values .append (ast .Call (ast .Name ('attributeCondition' ), args = [ast .Attribute (ast .Name ('node' ), attribute )]))
194195
195- buffaloBuffalo_workhorse_returnsAnnotation = cast ( ast . expr , ast . BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )]) )
196+ buffaloBuffalo_workhorse_returnsAnnotation = BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )])
196197 body : list [ast .stmt ] = [ast .FunctionDef (name = 'workhorse' ,
197198 args = ast .arguments (args = [ast .arg ('node' , ast .Attribute (ast .Name ('ast' ), attr = 'AST' ))])
198199 , body = [ast .Return (workhorseReturnValue )]
@@ -207,7 +208,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
207208 attributeVersionMinorMinimum : int = dictionary_ast_exprType ['attributeVersionMinorMinimum' ]
208209 decorator_list = [astName_staticmethod ]
209210 if list_ast_exprType :
210- annotation = cast ( ast . expr , ast . BitOr .join (list_ast_exprType ) )
211+ annotation = BitOr .join (list_ast_exprType )
211212 else :
212213 Z0Z_TypeWithoutNone = eval (dictionary_ast_exprType ['ast_exprType' ])
213214 annotation = ast .Subscript (ast .Name ('Callable' ), ast .Tuple ([ast .List ([Z0Z_TypeWithoutNone ]), ast .Name ('bool' )]))
@@ -226,7 +227,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
226227 break
227228
228229 workhorseReturnValue .values .append (ast .Call (ast .Name ('attributeCondition' ), args = [ast .Attribute (ast .Name ('node' ), attribute )]))
229- buffaloBuffalo_workhorse_returnsAnnotation = cast ( ast . expr , ast . BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )]) )
230+ buffaloBuffalo_workhorse_returnsAnnotation = BitOr .join ([ast .Subscript (ast .Name ('TypeGuard' ), slice = astNameTypeAlias ), ast .Name ('bool' )])
230231 body : list [ast .stmt ] = [ast .FunctionDef (name = 'workhorse' ,
231232 args = ast .arguments (args = [ast .arg ('node' , ast .Attribute (ast .Name ('ast' ), attr = 'AST' ))])
232233 , body = [ast .Return (workhorseReturnValue )]
@@ -294,7 +295,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
294295 body : list [ast .stmt ] = [ast .Return (ast .Attribute (ast .Name ('node' ), attribute ))]
295296 decorator_list : list [ast .expr ] = [astName_staticmethod ]
296297 attributeVersionMinorMinimum : int = min (dictionaryVersionsTypeAliasSubcategory .keys ())
297- returns = cast ( ast . expr , ast . BitOr .join (dictionaryVersionsTypeAliasSubcategory [attributeVersionMinorMinimum ]) )
298+ returns = BitOr .join (dictionaryVersionsTypeAliasSubcategory [attributeVersionMinorMinimum ])
298299 del dictionaryVersionsTypeAliasSubcategory [attributeVersionMinorMinimum ]
299300 orelse = [create_ast_stmt ()]
300301
@@ -303,7 +304,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
303304 body : list [ast .stmt ] = [ast .Return (ast .Attribute (ast .Name ('node' ), attribute ))]
304305 decorator_list = [astName_staticmethod ]
305306 if list_ast_exprType :
306- returns = cast ( ast . expr , ast . BitOr .join (list_ast_exprType ) )
307+ returns = BitOr .join (list_ast_exprType )
307308 else :
308309 returns = cast (ast .Attribute , eval (dictionary_ast_exprType ['ast_exprType' ]))
309310 list4ClassDefBody .append (create_ast_stmt ())
@@ -328,7 +329,7 @@ def create_ast_stmt() -> ast.If | ast.FunctionDef:
328329 ast_exprType = eval (ast_exprTypeAsStr )
329330 list_ast_expr4annotation .append (ast .Subscript (ast .Name ('Callable' ), slice = ast .Tuple ([ast .List ([ast_exprType ]), ast_exprType ])))
330331
331- ast_expr4annotation = cast ( ast . expr , ast . BitOr .join (list_ast_expr4annotation ) )
332+ ast_expr4annotation = BitOr .join (list_ast_expr4annotation )
332333
333334 ast_stmt = ast .FunctionDef (attribute + 'Attribute'
334335 , args = ast .arguments (posonlyargs = [], args = [ast .arg ('action' , annotation = ast_expr4annotation )], vararg = None , kwonlyargs = [], kw_defaults = [], kwarg = None , defaults = [])
@@ -451,6 +452,7 @@ def unpackDictionaryAllMatch_argsVersions() -> ast.stmt:
451452 assert ast_stmt is not None , "Coding by brinkmanship!"
452453 return ast_stmt
453454
455+ list_aliasIdentifier : list [str ] = []
454456 list4ClassDefBody : list [ast .stmt ] = [ClassDefDocstringMake ]
455457 setKeywordArgumentsAnnotationTypeAlias : set [str ] = set ()
456458
@@ -465,6 +467,7 @@ def unpackDictionaryAllMatch_argsVersions() -> ast.stmt:
465467 if ClassDefIdentifier == 'Import' :
466468 ast_stmt = FunctionDefMake_Import
467469 list4ClassDefBody .append (ast_stmt )
470+ list_aliasIdentifier .append ('str_nameDOTname' )
468471 continue
469472 classAs_astAttribute = cast (ast .expr , eval (dictionaryClassDef ['classAs_astAttribute' ]))
470473 dictionaryAllClassVersions : dict [int , dict [int , DictionaryMatchArgs ]] = dictionaryClassDef ['classVersionMinorMinimum' ]
@@ -506,23 +509,31 @@ def unpackDictionaryAllMatch_argsVersions() -> ast.stmt:
506509 assert ast_stmt is not None , "Coding by brinkmanship!"
507510 list4ClassDefBody .append (ast_stmt )
508511
512+ # `.join` ===============
513+ # create FunctionDef operatorJoinMethod
514+ # create FunctionDef join@classmethod
515+ listOperatorIdentifiers : list [str ] = ['Add' , 'BitAnd' , 'BitOr' , 'BitXor' , 'Div' , 'FloorDiv' , 'LShift' , 'MatMult' , 'Mod' , 'Mult' , 'Pow' , 'RShift' , 'Sub' ,]
516+ # create loop to create ClassDef astToolkit.operatorIdentifier.bases[ast.operatorIdentifier]
517+ # Module-level operations ===============
509518 setKeywordArgumentsAnnotationTypeAlias .discard ('int' )
519+ list_aliasIdentifier = sorted (set ([* setKeywordArgumentsAnnotationTypeAlias , * list_aliasIdentifier ]), key = str .lower )
510520 list4ModuleBody : list [ast .stmt ] = [
511- ast .ImportFrom ('astToolkit' , [ast .alias (identifier ) for identifier in sorted ([* setKeywordArgumentsAnnotationTypeAlias , 'str_nameDOTname' ])], 0 )
512- , ast .ImportFrom ('typing' , [ast .alias ('Any' )], 0 )
521+ ast .ImportFrom ('astToolkit' , [ast .alias (identifier ) for identifier in list_aliasIdentifier ], 0 )
522+ , ast .ImportFrom ('collections.abc' , [ast .alias ('Callable' ), ast .alias ('Iterable' ), ast .alias ('Sequence' )], 0 )
523+ , ast .ImportFrom ('typing' , [ast .alias ('Any' ), ast .alias ('Unpack' )], 0 )
513524 , ast .Import ([ast .alias ('ast' )])
514525 , ast .Import ([ast .alias ('sys' )])
515526 ]
516527
517528 writeClass ('Make' , list4ClassDefBody , list4ModuleBody )
518529
519- def makeTypeAlias () -> None :
530+ def make_astTypes () -> None :
520531 def append_ast_stmtTypeAlias () -> None :
521532 ast_stmt = None
522533 if len (dictionaryVersions ) == 1 :
523534 # This branch is the simplest case: one TypeAlias for the attribute for all Python versions
524535 for versionMinor , listClassAs_astAttribute in dictionaryVersions .items ():
525- ast_stmt = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , cast ( ast . expr , ast . BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in listClassAs_astAttribute ]) ), 1 )
536+ ast_stmt = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in listClassAs_astAttribute ]), 1 )
526537 if versionMinor > pythonVersionMinorMinimum :
527538 ast_stmt = ast .If (ast .Compare (ast .Attribute (ast .Name ('sys' ), 'version_info' )
528539 , ops = [ast .GtE ()]
@@ -534,8 +545,8 @@ def append_ast_stmtTypeAlias() -> None:
534545 listVersionsMinor = sorted (dictionaryVersions .keys (), reverse = False )
535546 if len (listVersionsMinor ) > 2 :
536547 raise NotImplementedError
537- ast_stmtAtPythonMinimum = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , cast ( ast . expr , ast . BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in dictionaryVersions [min (listVersionsMinor )]]) ), 1 )
538- ast_stmtAbovePythonMinimum = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , cast ( ast . expr , ast . BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in sorted (chain (* dictionaryVersions .values ()), key = str .lower )]) ), 1 )
548+ ast_stmtAtPythonMinimum = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in dictionaryVersions [min (listVersionsMinor )]]), 1 )
549+ ast_stmtAbovePythonMinimum = ast .AnnAssign (astNameTypeAlias , astName_typing_TypeAlias , BitOr .join ([eval (classAs_astAttribute ) for classAs_astAttribute in sorted (chain (* dictionaryVersions .values ()), key = str .lower )]), 1 )
539550
540551 ast_stmt = ast .If (ast .Compare (ast .Attribute (ast .Name ('sys' ), 'version_info' )
541552 , ops = [ast .GtE ()]
@@ -551,7 +562,7 @@ def append_ast_stmtTypeAlias() -> None:
551562 , ast .ImportFrom ('typing' , [ast .alias ('Any' ), ast .alias ('TypeAlias' , 'typing_TypeAlias' )], 0 )
552563 , ast .Import ([ast .alias ('ast' )])
553564 , ast .Import ([ast .alias ('sys' )])
554- , * listHandmadeTypeAlias_astTypes
565+ , * listHandmade_astTypes
555566 ]
556567 , type_ignores = []
557568 )
@@ -590,4 +601,4 @@ def append_ast_stmtTypeAlias() -> None:
590601 makeToolDOT ()
591602 makeToolGrab ()
592603 makeToolMake ()
593- makeTypeAlias ()
604+ make_astTypes ()
0 commit comments