21
21
'ParamSpecKwargs' ,
22
22
'Self' ,
23
23
'Type' ,
24
+ 'TypeVar' ,
24
25
'TypeVarTuple' ,
25
26
'Unpack' ,
26
27
@@ -1147,6 +1148,43 @@ def __repr__(self):
1147
1148
above.""" )
1148
1149
1149
1150
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
+
1150
1188
# Python 3.10+ has PEP 612
1151
1189
if hasattr (typing , 'ParamSpecArgs' ):
1152
1190
ParamSpecArgs = typing .ParamSpecArgs
@@ -1211,12 +1249,32 @@ def __eq__(self, other):
1211
1249
1212
1250
# 3.10+
1213
1251
if 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
+
1215
1273
# 3.7-3.9
1216
1274
else :
1217
1275
1218
1276
# Inherits from list as a workaround for Callable checks in Python < 3.9.2.
1219
- class ParamSpec (list ):
1277
+ class ParamSpec (list , _DefaultMixin ):
1220
1278
"""Parameter specification variable.
1221
1279
1222
1280
Usage::
@@ -1274,7 +1332,8 @@ def args(self):
1274
1332
def kwargs (self ):
1275
1333
return ParamSpecKwargs (self )
1276
1334
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 ):
1278
1337
super ().__init__ ([self ])
1279
1338
self .__name__ = name
1280
1339
self .__covariant__ = bool (covariant )
@@ -1283,6 +1342,7 @@ def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
1283
1342
self .__bound__ = typing ._type_check (bound , 'Bound must be a type.' )
1284
1343
else :
1285
1344
self .__bound__ = None
1345
+ _DefaultMixin .__init__ (self , default )
1286
1346
1287
1347
# for pickling:
1288
1348
try :
@@ -1784,9 +1844,25 @@ def _is_unpack(obj):
1784
1844
1785
1845
1786
1846
if 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
+
1788
1864
else :
1789
- class TypeVarTuple :
1865
+ class TypeVarTuple ( _DefaultMixin ) :
1790
1866
"""Type variable tuple.
1791
1867
1792
1868
Usage::
@@ -1836,8 +1912,9 @@ def get_shape(self) -> Tuple[*Ts]:
1836
1912
def __iter__ (self ):
1837
1913
yield self .__unpacked__
1838
1914
1839
- def __init__ (self , name ):
1915
+ def __init__ (self , name , * , default = None ):
1840
1916
self .__name__ = name
1917
+ _DefaultMixin .__init__ (self , default )
1841
1918
1842
1919
# for pickling:
1843
1920
try :
0 commit comments