@@ -2262,21 +2262,7 @@ class Foo(Bar, Generic[T]): ...
22622262 # grained incremental mode.
22632263 defn .removed_base_type_exprs .append (defn .base_type_exprs [i ])
22642264 del base_type_exprs [i ]
2265- tvar_defs : list [TypeVarLikeType ] = []
2266- last_tvar_name_with_default : str | None = None
2267- for name , tvar_expr in declared_tvars :
2268- tvar_expr .default = tvar_expr .default .accept (
2269- TypeVarDefaultTranslator (self , tvar_expr .name , context )
2270- )
2271- tvar_def = self .tvar_scope .bind_new (name , tvar_expr )
2272- if last_tvar_name_with_default is not None and not tvar_def .has_default ():
2273- self .msg .tvar_without_default_type (
2274- tvar_def .name , last_tvar_name_with_default , context
2275- )
2276- tvar_def .default = AnyType (TypeOfAny .from_error )
2277- elif tvar_def .has_default ():
2278- last_tvar_name_with_default = tvar_def .name
2279- tvar_defs .append (tvar_def )
2265+ tvar_defs = self .tvar_defs_from_tvars (declared_tvars , context )
22802266 return base_type_exprs , tvar_defs , is_protocol
22812267
22822268 def analyze_class_typevar_declaration (self , base : Type ) -> tuple [TypeVarLikeList , bool ] | None :
@@ -2377,6 +2363,26 @@ def get_all_bases_tvars(
23772363 tvars .extend (base_tvars )
23782364 return remove_dups (tvars )
23792365
2366+ def tvar_defs_from_tvars (
2367+ self , tvars : TypeVarLikeList , context : Context
2368+ ) -> list [TypeVarLikeType ]:
2369+ tvar_defs : list [TypeVarLikeType ] = []
2370+ last_tvar_name_with_default : str | None = None
2371+ for name , tvar_expr in tvars :
2372+ tvar_expr .default = tvar_expr .default .accept (
2373+ TypeVarDefaultTranslator (self , tvar_expr .name , context )
2374+ )
2375+ tvar_def = self .tvar_scope .bind_new (name , tvar_expr )
2376+ if last_tvar_name_with_default is not None and not tvar_def .has_default ():
2377+ self .msg .tvar_without_default_type (
2378+ tvar_def .name , last_tvar_name_with_default , context
2379+ )
2380+ tvar_def .default = AnyType (TypeOfAny .from_error )
2381+ elif tvar_def .has_default ():
2382+ last_tvar_name_with_default = tvar_def .name
2383+ tvar_defs .append (tvar_def )
2384+ return tvar_defs
2385+
23802386 def get_and_bind_all_tvars (self , type_exprs : list [Expression ]) -> list [TypeVarLikeType ]:
23812387 """Return all type variable references in item type expressions.
23822388
@@ -3852,21 +3858,8 @@ def analyze_alias(
38523858 tvar_defs : list [TypeVarLikeType ] = []
38533859 namespace = self .qualified_name (name )
38543860 alias_type_vars = found_type_vars if declared_type_vars is None else declared_type_vars
3855- last_tvar_name_with_default : str | None = None
38563861 with self .tvar_scope_frame (self .tvar_scope .class_frame (namespace )):
3857- for name , tvar_expr in alias_type_vars :
3858- tvar_expr .default = tvar_expr .default .accept (
3859- TypeVarDefaultTranslator (self , tvar_expr .name , typ )
3860- )
3861- tvar_def = self .tvar_scope .bind_new (name , tvar_expr )
3862- if last_tvar_name_with_default is not None and not tvar_def .has_default ():
3863- self .msg .tvar_without_default_type (
3864- tvar_def .name , last_tvar_name_with_default , typ
3865- )
3866- tvar_def .default = AnyType (TypeOfAny .from_error )
3867- elif tvar_def .has_default ():
3868- last_tvar_name_with_default = tvar_def .name
3869- tvar_defs .append (tvar_def )
3862+ tvar_defs = self .tvar_defs_from_tvars (alias_type_vars , typ )
38703863
38713864 if python_3_12_type_alias :
38723865 with self .allow_unbound_tvars_set ():
0 commit comments