2121 'ParamSpecKwargs' ,
2222 'Self' ,
2323 'Type' ,
24+ 'TypeVar' ,
2425 'TypeVarTuple' ,
2526 'Unpack' ,
2627
@@ -1147,6 +1148,43 @@ def __repr__(self):
11471148 above.""" )
11481149
11491150
1151+ class _DefaultMixin :
1152+ """Mixin for TypeVarLike defaults."""
1153+
1154+ __slots__ = ()
1155+
1156+ def __init__ (self , default ):
1157+ if isinstance (default , (tuple , list )):
1158+ self .__default__ = tuple ((typing ._type_check (d , "Default must be a type" )
1159+ for d in default ))
1160+ elif default :
1161+ self .__default__ = typing ._type_check (default , "Default must be a type" )
1162+ else :
1163+ self .__default__ = None
1164+
1165+
1166+ # Add default Parameter - PEP 696
1167+ class TypeVar (typing .TypeVar , _DefaultMixin , _root = True ):
1168+ """Type variable."""
1169+
1170+ __module__ = 'typing'
1171+
1172+ def __init__ (self , name , * constraints , bound = None ,
1173+ covariant = False , contravariant = False ,
1174+ default = None ):
1175+ super ().__init__ (name , * constraints , bound = bound , covariant = covariant ,
1176+ contravariant = contravariant )
1177+ _DefaultMixin .__init__ (self , default )
1178+
1179+ # for pickling:
1180+ try :
1181+ def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1182+ except (AttributeError , ValueError ):
1183+ def_mod = None
1184+ if def_mod != 'typing_extensions' :
1185+ self .__module__ = def_mod
1186+
1187+
11501188# Python 3.10+ has PEP 612
11511189if hasattr (typing , 'ParamSpecArgs' ):
11521190 ParamSpecArgs = typing .ParamSpecArgs
@@ -1211,12 +1249,32 @@ def __eq__(self, other):
12111249
12121250# 3.10+
12131251if hasattr (typing , 'ParamSpec' ):
1214- ParamSpec = typing .ParamSpec
1252+
1253+ # Add default Parameter - PEP 696
1254+ class ParamSpec (typing .ParamSpec , _DefaultMixin , _root = True ):
1255+ """Parameter specification variable."""
1256+
1257+ __module__ = 'typing'
1258+
1259+ def __init__ (self , name , * , bound = None , covariant = False , contravariant = False ,
1260+ default = None ):
1261+ super ().__init__ (name , bound = bound , covariant = covariant ,
1262+ contravariant = contravariant )
1263+ _DefaultMixin .__init__ (self , default )
1264+
1265+ # for pickling:
1266+ try :
1267+ def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1268+ except (AttributeError , ValueError ):
1269+ def_mod = None
1270+ if def_mod != 'typing_extensions' :
1271+ self .__module__ = def_mod
1272+
12151273# 3.7-3.9
12161274else :
12171275
12181276 # Inherits from list as a workaround for Callable checks in Python < 3.9.2.
1219- class ParamSpec (list ):
1277+ class ParamSpec (list , _DefaultMixin ):
12201278 """Parameter specification variable.
12211279
12221280 Usage::
@@ -1274,7 +1332,8 @@ def args(self):
12741332 def kwargs (self ):
12751333 return ParamSpecKwargs (self )
12761334
1277- def __init__ (self , name , * , bound = None , covariant = False , contravariant = False ):
1335+ def __init__ (self , name , * , bound = None , covariant = False , contravariant = False ,
1336+ default = None ):
12781337 super ().__init__ ([self ])
12791338 self .__name__ = name
12801339 self .__covariant__ = bool (covariant )
@@ -1283,6 +1342,7 @@ def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
12831342 self .__bound__ = typing ._type_check (bound , 'Bound must be a type.' )
12841343 else :
12851344 self .__bound__ = None
1345+ _DefaultMixin .__init__ (self , default )
12861346
12871347 # for pickling:
12881348 try :
@@ -1784,9 +1844,25 @@ def _is_unpack(obj):
17841844
17851845
17861846if hasattr (typing , "TypeVarTuple" ): # 3.11+
1787- TypeVarTuple = typing .TypeVarTuple
1847+
1848+ # Add default Parameter - PEP 696
1849+ class TypeVarTuple (typing .TypeVarTuple , _DefaultMixin , _root = True ):
1850+ """Type variable tuple."""
1851+
1852+ def __init__ (self , name , * , default = None ):
1853+ super ().__init__ (name )
1854+ _DefaultMixin .__init__ (self , default )
1855+
1856+ # for pickling:
1857+ try :
1858+ def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1859+ except (AttributeError , ValueError ):
1860+ def_mod = None
1861+ if def_mod != 'typing_extensions' :
1862+ self .__module__ = def_mod
1863+
17881864else :
1789- class TypeVarTuple :
1865+ class TypeVarTuple ( _DefaultMixin ) :
17901866 """Type variable tuple.
17911867
17921868 Usage::
@@ -1836,8 +1912,9 @@ def get_shape(self) -> Tuple[*Ts]:
18361912 def __iter__ (self ):
18371913 yield self .__unpacked__
18381914
1839- def __init__ (self , name ):
1915+ def __init__ (self , name , * , default = None ):
18401916 self .__name__ = name
1917+ _DefaultMixin .__init__ (self , default )
18411918
18421919 # for pickling:
18431920 try :
0 commit comments