@@ -4114,9 +4114,7 @@ def process_typevar_parameters(
41144114 tv_arg = self .get_typevarlike_argument (
41154115 "TypeVar" , param_name , param_value , context , allow_unbound_tvars = True
41164116 )
4117- if tv_arg is None :
4118- return None
4119- default = tv_arg
4117+ default = tv_arg or AnyType (TypeOfAny .from_error )
41204118 elif param_name == "values" :
41214119 # Probably using obsolete syntax with values=(...). Explain the current syntax.
41224120 self .fail ('TypeVar "values" argument not supported' , context )
@@ -4153,6 +4151,7 @@ def get_typevarlike_argument(
41534151 * ,
41544152 allow_unbound_tvars : bool = False ,
41554153 allow_param_spec_literals : bool = False ,
4154+ report_invalid_typevar_arg : bool = True ,
41564155 ) -> ProperType | None :
41574156 try :
41584157 # We want to use our custom error message below, so we suppress
@@ -4173,7 +4172,7 @@ def get_typevarlike_argument(
41734172 # ...
41744173 analyzed = PlaceholderType (None , [], context .line )
41754174 typ = get_proper_type (analyzed )
4176- if isinstance (typ , AnyType ) and typ .is_from_error :
4175+ if report_invalid_typevar_arg and isinstance (typ , AnyType ) and typ .is_from_error :
41774176 self .fail (
41784177 message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
41794178 param_value ,
@@ -4182,10 +4181,11 @@ def get_typevarlike_argument(
41824181 # using the AnyType as the upper bound.
41834182 return typ
41844183 except TypeTranslationError :
4185- self .fail (
4186- message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4187- param_value ,
4188- )
4184+ if report_invalid_typevar_arg :
4185+ self .fail (
4186+ message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format (typevarlike_name , param_name ),
4187+ param_value ,
4188+ )
41894189 return None
41904190
41914191 def extract_typevarlike_name (self , s : AssignmentStmt , call : CallExpr ) -> str | None :
@@ -4236,20 +4236,23 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
42364236 s ,
42374237 allow_unbound_tvars = True ,
42384238 allow_param_spec_literals = True ,
4239+ report_invalid_typevar_arg = False ,
42394240 )
4240- if tv_arg is None :
4241- return False
4242- default = tv_arg
4241+ default = tv_arg or AnyType (TypeOfAny .from_error )
42434242 if isinstance (tv_arg , Parameters ):
42444243 for i , arg_type in enumerate (tv_arg .arg_types ):
42454244 typ = get_proper_type (arg_type )
42464245 if isinstance (typ , AnyType ) and typ .is_from_error :
42474246 self .fail (
42484247 f"Argument { i } of ParamSpec default must be a type" , param_value
42494248 )
4250- elif not isinstance (default , (AnyType , UnboundType )):
4249+ elif (
4250+ isinstance (default , AnyType )
4251+ and default .is_from_error
4252+ or not isinstance (default , (AnyType , UnboundType ))
4253+ ):
42514254 self .fail (
4252- "The default argument to ParamSpec must be a tuple expression, ellipsis, or a ParamSpec" ,
4255+ "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec" ,
42534256 param_value ,
42544257 )
42554258 default = AnyType (TypeOfAny .from_error )
@@ -4306,11 +4309,14 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
43064309 ):
43074310 if param_name == "default" :
43084311 tv_arg = self .get_typevarlike_argument (
4309- "TypeVarTuple" , param_name , param_value , s , allow_unbound_tvars = True
4312+ "TypeVarTuple" ,
4313+ param_name ,
4314+ param_value ,
4315+ s ,
4316+ allow_unbound_tvars = True ,
4317+ report_invalid_typevar_arg = False ,
43104318 )
4311- if tv_arg is None :
4312- return False
4313- default = tv_arg
4319+ default = tv_arg or AnyType (TypeOfAny .from_error )
43144320 if not isinstance (default , UnpackType ):
43154321 self .fail (
43164322 "The default argument to TypeVarTuple must be an Unpacked tuple" ,
0 commit comments