@@ -1808,7 +1808,26 @@ def analyze_type_param(
18081808 upper_bound = self .named_type ("builtins.tuple" , [self .object_type ()])
18091809 else :
18101810 upper_bound = self .object_type ()
1811- default = AnyType (TypeOfAny .from_omitted_generics )
1811+ if type_param .default :
1812+ default = self .anal_type (
1813+ type_param .default ,
1814+ allow_placeholder = True ,
1815+ allow_unbound_tvars = True ,
1816+ report_invalid_types = False ,
1817+ allow_param_spec_literals = type_param .kind == PARAM_SPEC_KIND ,
1818+ allow_tuple_literal = type_param .kind == PARAM_SPEC_KIND ,
1819+ allow_unpack = type_param .kind == TYPE_VAR_TUPLE_KIND ,
1820+ )
1821+ if default is None :
1822+ default = PlaceholderType (None , [], context .line )
1823+ elif type_param .kind == TYPE_VAR_KIND :
1824+ default = self .check_typevar_default (default , type_param .default )
1825+ elif type_param .kind == PARAM_SPEC_KIND :
1826+ default = self .check_paramspec_default (default , type_param .default )
1827+ elif type_param .kind == TYPE_VAR_TUPLE_KIND :
1828+ default = self .check_typevartuple_default (default , type_param .default )
1829+ else :
1830+ default = AnyType (TypeOfAny .from_omitted_generics )
18121831 if type_param .kind == TYPE_VAR_KIND :
18131832 values = []
18141833 if type_param .values :
@@ -4615,6 +4634,40 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
46154634 self .add_symbol (name , call .analyzed , s )
46164635 return True
46174636
4637+ def check_typevar_default (self , default : Type , context : Context ) -> Type :
4638+ typ = get_proper_type (default )
4639+ if isinstance (typ , AnyType ) and typ .is_from_error :
4640+ self .fail (
4641+ message_registry .TYPEVAR_ARG_MUST_BE_TYPE .format ("TypeVar" , "default" ), context
4642+ )
4643+ return default
4644+
4645+ def check_paramspec_default (self , default : Type , context : Context ) -> Type :
4646+ typ = get_proper_type (default )
4647+ if isinstance (typ , Parameters ):
4648+ for i , arg_type in enumerate (typ .arg_types ):
4649+ arg_ptype = get_proper_type (arg_type )
4650+ if isinstance (arg_ptype , AnyType ) and arg_ptype .is_from_error :
4651+ self .fail (f"Argument { i } of ParamSpec default must be a type" , context )
4652+ elif (
4653+ isinstance (typ , AnyType )
4654+ and typ .is_from_error
4655+ or not isinstance (typ , (AnyType , UnboundType ))
4656+ ):
4657+ self .fail (
4658+ "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec" ,
4659+ context ,
4660+ )
4661+ default = AnyType (TypeOfAny .from_error )
4662+ return default
4663+
4664+ def check_typevartuple_default (self , default : Type , context : Context ) -> Type :
4665+ typ = get_proper_type (default )
4666+ if not isinstance (typ , UnpackType ):
4667+ self .fail ("The default argument to TypeVarTuple must be an Unpacked tuple" , context )
4668+ default = AnyType (TypeOfAny .from_error )
4669+ return default
4670+
46184671 def check_typevarlike_name (self , call : CallExpr , name : str , context : Context ) -> bool :
46194672 """Checks that the name of a TypeVar or ParamSpec matches its variable."""
46204673 name = unmangle (name )
@@ -4822,23 +4875,7 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
48224875 report_invalid_typevar_arg = False ,
48234876 )
48244877 default = tv_arg or AnyType (TypeOfAny .from_error )
4825- if isinstance (tv_arg , Parameters ):
4826- for i , arg_type in enumerate (tv_arg .arg_types ):
4827- typ = get_proper_type (arg_type )
4828- if isinstance (typ , AnyType ) and typ .is_from_error :
4829- self .fail (
4830- f"Argument { i } of ParamSpec default must be a type" , param_value
4831- )
4832- elif (
4833- isinstance (default , AnyType )
4834- and default .is_from_error
4835- or not isinstance (default , (AnyType , UnboundType ))
4836- ):
4837- self .fail (
4838- "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec" ,
4839- param_value ,
4840- )
4841- default = AnyType (TypeOfAny .from_error )
4878+ default = self .check_paramspec_default (default , param_value )
48424879 else :
48434880 # ParamSpec is different from a regular TypeVar:
48444881 # arguments are not semantically valid. But, allowed in runtime.
@@ -4899,12 +4936,7 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
48994936 allow_unpack = True ,
49004937 )
49014938 default = tv_arg or AnyType (TypeOfAny .from_error )
4902- if not isinstance (default , UnpackType ):
4903- self .fail (
4904- "The default argument to TypeVarTuple must be an Unpacked tuple" ,
4905- param_value ,
4906- )
4907- default = AnyType (TypeOfAny .from_error )
4939+ default = self .check_typevartuple_default (default , param_value )
49084940 else :
49094941 self .fail (f'Unexpected keyword argument "{ param_name } " for "TypeVarTuple"' , s )
49104942
0 commit comments