@@ -546,8 +546,13 @@ def __init__(
546546 # to create the set lazily.
547547 self .types_fixed : set [TypeInfo | TypeAlias ] | None = None
548548
549+ # Stack of type variables that have been removed from current class because they
550+ # cannot be bound unambiguously. This can happen if a (regular) type variable
551+ # with a default follows a type variable tuple.
552+ self .removed_type_vars : list [list [TypeVarType ]] = [[]]
553+
549554 # mypyc doesn't properly handle implementing an abstractproperty
550- # with a regular attribute so we make them properties
555+ # with a regular attribute, so we make them properties
551556 @property
552557 def type (self ) -> TypeInfo | None :
553558 return self ._type
@@ -1151,7 +1156,8 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type:
11511156 leading_type = fill_typevars (info )
11521157 if func .is_class or func .name == "__new__" :
11531158 leading_type = self .class_type (leading_type )
1154- func .type = replace_implicit_first_type (functype , leading_type )
1159+ if not has_placeholder (leading_type ):
1160+ func .type = replace_implicit_first_type (functype , leading_type )
11551161 elif has_self_type and isinstance (func .unanalyzed_type , CallableType ):
11561162 if not isinstance (get_proper_type (func .unanalyzed_type .arg_types [0 ]), AnyType ):
11571163 if self .is_expected_self_type (
@@ -1836,8 +1842,9 @@ def visit_class_def(self, defn: ClassDef) -> None:
18361842 if self .push_type_args (defn .type_args , defn ) is None :
18371843 self .mark_incomplete (defn .name , defn )
18381844 return
1839-
1845+ self . removed_type_vars . append ([])
18401846 self .analyze_class (defn )
1847+ self .removed_type_vars .pop ()
18411848 self .pop_type_args (defn .type_args )
18421849 self .incomplete_type_stack .pop ()
18431850
@@ -2084,7 +2091,21 @@ def check_type_alias_bases(self, bases: list[Expression]) -> None:
20842091 )
20852092
20862093 def setup_type_vars (self , defn : ClassDef , tvar_defs : list [TypeVarLikeType ]) -> None :
2087- defn .type_vars = tvar_defs
2094+ seen_tvt = False
2095+ valid_tvar_defs = []
2096+ for tv in tvar_defs :
2097+ if seen_tvt and isinstance (tv , TypeVarType ) and tv .has_default ():
2098+ self .fail (
2099+ message_registry .NO_DEFAULT_AFTER_TYPEVAR_TUPLE , defn , code = codes .TYPE_VAR
2100+ )
2101+ # Remove the ambiguous type variable, and record it, so that we can replace
2102+ # all its uses with Any.
2103+ self .removed_type_vars [- 1 ].append (tv )
2104+ continue
2105+ if isinstance (tv , TypeVarTupleType ):
2106+ seen_tvt = True
2107+ valid_tvar_defs .append (tv )
2108+ defn .type_vars = valid_tvar_defs
20882109 defn .info .type_vars = []
20892110 # we want to make sure any additional logic in add_type_vars gets run
20902111 defn .info .add_type_vars ()
@@ -4017,6 +4038,28 @@ def analyze_alias(
40174038 with self .allow_unbound_tvars_set ():
40184039 rvalue .accept (self )
40194040
4041+ new_tvar_defs = []
4042+ erase_tvar_defs = []
4043+ variadic = False
4044+ for td in tvar_defs :
4045+ if variadic and isinstance (td , TypeVarType ) and td .has_default ():
4046+ self .fail (
4047+ message_registry .NO_DEFAULT_AFTER_TYPEVAR_TUPLE ,
4048+ rvalue ,
4049+ code = codes .TYPE_VAR ,
4050+ )
4051+ # Remove the ambiguous type variable, and record it, so that we can
4052+ # replace all its uses with Any.
4053+ erase_tvar_defs .append (td )
4054+ continue
4055+ if isinstance (td , TypeVarTupleType ):
4056+ # There can be only one variadic variable at most,
4057+ # the error is reported elsewhere.
4058+ if variadic :
4059+ continue
4060+ variadic = True
4061+ new_tvar_defs .append (td )
4062+
40204063 analyzed , depends_on = analyze_type_alias (
40214064 typ ,
40224065 self ,
@@ -4029,20 +4072,11 @@ def analyze_alias(
40294072 in_dynamic_func = dynamic ,
40304073 global_scope = global_scope ,
40314074 allowed_alias_tvars = tvar_defs ,
4075+ erase_tvar_defs = erase_tvar_defs ,
40324076 alias_type_params_names = all_declared_type_params_names ,
40334077 python_3_12_type_alias = python_3_12_type_alias ,
40344078 )
40354079
4036- # There can be only one variadic variable at most, the error is reported elsewhere.
4037- new_tvar_defs = []
4038- variadic = False
4039- for td in tvar_defs :
4040- if isinstance (td , TypeVarTupleType ):
4041- if variadic :
4042- continue
4043- variadic = True
4044- new_tvar_defs .append (td )
4045-
40464080 indexed = bool (isinstance (typ , UnboundType ) and (typ .args or typ .empty_tuple_index ))
40474081 default_depends = {}
40484082 for _ , tv in alias_type_vars :
@@ -5771,7 +5805,10 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None:
57715805 res = make_any_non_unimported (res )
57725806 eager = self .is_func_scope ()
57735807 if isinstance (res , ProperType ) and isinstance (res , Instance ):
5774- fix_instance (res , self .fail , self .note , disallow_any = False , options = self .options )
5808+ if not validate_instance (res , self .fail , indexed ):
5809+ fix_instance (
5810+ res , self .fail , self .note , disallow_any = False , options = self .options
5811+ )
57755812 alias_node = TypeAlias (
57765813 res ,
57775814 self .qualified_name (s .name .name ),
@@ -6396,7 +6433,7 @@ def analyze_comp_for(self, expr: GeneratorExpr | DictionaryComprehension) -> Non
63966433 if i > 0 :
63976434 sequence .accept (self )
63986435 # Bind index variables.
6399- self .analyze_lvalue (index )
6436+ self .analyze_lvalue (index , is_index_var = True )
64006437 for cond in conditions :
64016438 cond .accept (self )
64026439
@@ -7789,6 +7826,7 @@ def type_analyzer(
77897826 prohibit_special_class_field_types = prohibit_special_class_field_types ,
77907827 allow_type_any = allow_type_any ,
77917828 analyzing_tvar_def = analyzing_tvar_def ,
7829+ erase_tvar_defs = self .removed_type_vars [- 1 ],
77927830 )
77937831 tpan .in_dynamic_func = bool (self .function_stack and self .function_stack [- 1 ].is_dynamic ())
77947832 tpan .global_scope = not self .type and not self .function_stack
0 commit comments