@@ -920,13 +920,20 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
920920 continue
921921 if (
922922 isinstance (lvalue , NameExpr )
923- and not self .is_private_name (lvalue .name )
924- # it is never an alias with explicit annotation
925- and not o .unanalyzed_type
926923 and self .is_alias_expression (o .rvalue )
924+ and not self .is_private_name (lvalue .name )
927925 ):
928- self .process_typealias (lvalue , o .rvalue )
929- continue
926+ is_explicit_type_alias = (
927+ o .unanalyzed_type and getattr (o .type , "name" , None ) == "TypeAlias"
928+ )
929+ if is_explicit_type_alias :
930+ self .process_typealias (lvalue , o .rvalue , is_explicit_type_alias = True )
931+ continue
932+
933+ if not o .unanalyzed_type :
934+ self .process_typealias (lvalue , o .rvalue )
935+ continue
936+
930937 if isinstance (lvalue , (TupleExpr , ListExpr )):
931938 items = lvalue .items
932939 if isinstance (o .unanalyzed_type , TupleType ): # type: ignore[misc]
@@ -1139,9 +1146,15 @@ def is_alias_expression(self, expr: Expression, top_level: bool = True) -> bool:
11391146 else :
11401147 return False
11411148
1142- def process_typealias (self , lvalue : NameExpr , rvalue : Expression ) -> None :
1149+ def process_typealias (
1150+ self , lvalue : NameExpr , rvalue : Expression , is_explicit_type_alias : bool = False
1151+ ) -> None :
11431152 p = AliasPrinter (self )
1144- self .add (f"{ self ._indent } { lvalue .name } = { rvalue .accept (p )} \n " )
1153+ if is_explicit_type_alias :
1154+ self .import_tracker .require_name ("TypeAlias" )
1155+ self .add (f"{ self ._indent } { lvalue .name } : TypeAlias = { rvalue .accept (p )} \n " )
1156+ else :
1157+ self .add (f"{ self ._indent } { lvalue .name } = { rvalue .accept (p )} \n " )
11451158 self .record_name (lvalue .name )
11461159 self ._vars [- 1 ].append (lvalue .name )
11471160
@@ -1838,6 +1851,8 @@ def parse_options(args: list[str]) -> Options:
18381851 parser = argparse .ArgumentParser (
18391852 prog = "stubgen" , usage = HEADER , description = DESCRIPTION , fromfile_prefix_chars = "@"
18401853 )
1854+ if sys .version_info >= (3 , 14 ):
1855+ parser .color = True # Set as init arg in 3.14
18411856
18421857 parser .add_argument (
18431858 "--ignore-errors" ,
0 commit comments