@@ -4110,9 +4110,7 @@ def process_typevar_parameters(
41104110 tv_arg = self .get_typevarlike_argument (
41114111 "TypeVar" , param_name , param_value , context , allow_unbound_tvars = True
41124112 )
4113- if tv_arg is None :
4114- return None
4115- default = tv_arg
4113+ default = tv_arg or AnyType (TypeOfAny .from_error )
41164114 elif param_name == "values" :
41174115 # Probably using obsolete syntax with values=(...). Explain the current syntax.
41184116 self .fail ('TypeVar "values" argument not supported' , context )
@@ -4149,6 +4147,7 @@ def get_typevarlike_argument(
41494147 * ,
41504148 allow_unbound_tvars : bool = False ,
41514149 allow_param_spec_literals : bool = False ,
4150+ report_invalid_typevar_arg : bool = True ,
41524151 ) -> ProperType | None :
41534152 try :
41544153 # We want to use our custom error message below, so we suppress
@@ -4169,7 +4168,7 @@ def get_typevarlike_argument(
41694168 # ...
41704169 analyzed = PlaceholderType (None , [], context .line )
41714170 typ = get_proper_type (analyzed )
4172- if isinstance (typ , AnyType ) and typ .is_from_error :
4171+ if report_invalid_typevar_arg and isinstance (typ , AnyType ) and typ .is_from_error :
41734172 self .fail (
41744173 message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
41754174 param_value ,
@@ -4178,10 +4177,11 @@ def get_typevarlike_argument(
41784177 # using the AnyType as the upper bound.
41794178 return typ
41804179 except TypeTranslationError :
4181- self .fail (
4182- message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4183- param_value ,
4184- )
4180+ if report_invalid_typevar_arg :
4181+ self .fail (
4182+ message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4183+ param_value ,
4184+ )
41854185 return None
41864186
41874187 def extract_typevarlike_name (self , s : AssignmentStmt , call : CallExpr ) -> str | None :
@@ -4232,20 +4232,23 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
42324232 s ,
42334233 allow_unbound_tvars = True ,
42344234 allow_param_spec_literals = True ,
4235+ report_invalid_typevar_arg = False ,
42354236 )
4236- if tv_arg is None :
4237- return False
4238- default = tv_arg
4237+ default = tv_arg or AnyType (TypeOfAny .from_error )
42394238 if isinstance (tv_arg , Parameters ):
42404239 for i , arg_type in enumerate (tv_arg .arg_types ):
42414240 typ = get_proper_type (arg_type )
42424241 if isinstance (typ , AnyType ) and typ .is_from_error :
42434242 self .fail (
42444243 f"Argument { i } of ParamSpec default must be a type" , param_value
42454244 )
4246- elif not isinstance (default , (AnyType , UnboundType )):
4245+ elif (
4246+ isinstance (default , AnyType )
4247+ and default .is_from_error
4248+ or not isinstance (default , (AnyType , UnboundType ))
4249+ ):
42474250 self .fail (
4248- "The default argument to ParamSpec must be a tuple expression, ellipsis, or a ParamSpec" ,
4251+ "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec" ,
42494252 param_value ,
42504253 )
42514254 default = AnyType (TypeOfAny .from_error )
@@ -4296,11 +4299,14 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
42964299 ):
42974300 if param_name == "default" :
42984301 tv_arg = self .get_typevarlike_argument (
4299- "TypeVarTuple" , param_name , param_value , s , allow_unbound_tvars = True
4302+ "TypeVarTuple" ,
4303+ param_name ,
4304+ param_value ,
4305+ s ,
4306+ allow_unbound_tvars = True ,
4307+ report_invalid_typevar_arg = False ,
43004308 )
4301- if tv_arg is None :
4302- return False
4303- default = tv_arg
4309+ default = tv_arg or AnyType (TypeOfAny .from_error )
43044310 if not isinstance (default , UnpackType ):
43054311 self .fail (
43064312 "The default argument to TypeVarTuple must be an Unpacked tuple" ,
0 commit comments