@@ -4132,9 +4132,7 @@ def process_typevar_parameters(
41324132 tv_arg = self .get_typevarlike_argument (
41334133 "TypeVar" , param_name , param_value , context , allow_unbound_tvars = True
41344134 )
4135- if tv_arg is None :
4136- return None
4137- default = tv_arg
4135+ default = tv_arg or AnyType (TypeOfAny .from_error )
41384136 elif param_name == "values" :
41394137 # Probably using obsolete syntax with values=(...). Explain the current syntax.
41404138 self .fail ('TypeVar "values" argument not supported' , context )
@@ -4171,6 +4169,7 @@ def get_typevarlike_argument(
41714169 * ,
41724170 allow_unbound_tvars : bool = False ,
41734171 allow_param_spec_literals : bool = False ,
4172+ report_invalid_typevar_arg : bool = True ,
41744173 ) -> ProperType | None :
41754174 try :
41764175 # We want to use our custom error message below, so we suppress
@@ -4191,7 +4190,7 @@ def get_typevarlike_argument(
41914190 # ...
41924191 analyzed = PlaceholderType (None , [], context .line )
41934192 typ = get_proper_type (analyzed )
4194- if isinstance (typ , AnyType ) and typ .is_from_error :
4193+ if report_invalid_typevar_arg and isinstance (typ , AnyType ) and typ .is_from_error :
41954194 self .fail (
41964195 message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
41974196 param_value ,
@@ -4200,10 +4199,11 @@ def get_typevarlike_argument(
42004199 # using the AnyType as the upper bound.
42014200 return typ
42024201 except TypeTranslationError :
4203- self .fail (
4204- message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4205- param_value ,
4206- )
4202+ if report_invalid_typevar_arg :
4203+ self .fail (
4204+ message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4205+ param_value ,
4206+ )
42074207 return None
42084208
42094209 def extract_typevarlike_name (self , s : AssignmentStmt , call : CallExpr ) -> str | None :
@@ -4254,20 +4254,23 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
42544254 s ,
42554255 allow_unbound_tvars = True ,
42564256 allow_param_spec_literals = True ,
4257+ report_invalid_typevar_arg = False ,
42574258 )
4258- if tv_arg is None :
4259- return False
4260- default = tv_arg
4259+ default = tv_arg or AnyType (TypeOfAny .from_error )
42614260 if isinstance (tv_arg , Parameters ):
42624261 for i , arg_type in enumerate (tv_arg .arg_types ):
42634262 typ = get_proper_type (arg_type )
42644263 if isinstance (typ , AnyType ) and typ .is_from_error :
42654264 self .fail (
42664265 f"Argument { i } of ParamSpec default must be a type" , param_value
42674266 )
4268- elif not isinstance (default , (AnyType , UnboundType )):
4267+ elif (
4268+ isinstance (default , AnyType )
4269+ and default .is_from_error
4270+ or not isinstance (default , (AnyType , UnboundType ))
4271+ ):
42694272 self .fail (
4270- "The default argument to ParamSpec must be a tuple expression, ellipsis, or a ParamSpec" ,
4273+ "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec" ,
42714274 param_value ,
42724275 )
42734276 default = AnyType (TypeOfAny .from_error )
@@ -4324,11 +4327,14 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
43244327 ):
43254328 if param_name == "default" :
43264329 tv_arg = self .get_typevarlike_argument (
4327- "TypeVarTuple" , param_name , param_value , s , allow_unbound_tvars = True
4330+ "TypeVarTuple" ,
4331+ param_name ,
4332+ param_value ,
4333+ s ,
4334+ allow_unbound_tvars = True ,
4335+ report_invalid_typevar_arg = False ,
43284336 )
4329- if tv_arg is None :
4330- return False
4331- default = tv_arg
4337+ default = tv_arg or AnyType (TypeOfAny .from_error )
43324338 if not isinstance (default , UnpackType ):
43334339 self .fail (
43344340 "The default argument to TypeVarTuple must be an Unpacked tuple" ,
0 commit comments