@@ -759,6 +759,7 @@ def runtime_checkable(cls):
759
759
SupportsInt = typing .SupportsInt
760
760
SupportsFloat = typing .SupportsFloat
761
761
SupportsComplex = typing .SupportsComplex
762
+ SupportsBytes = typing .SupportsBytes
762
763
SupportsIndex = typing .SupportsIndex
763
764
SupportsAbs = typing .SupportsAbs
764
765
SupportsRound = typing .SupportsRound
@@ -1343,39 +1344,53 @@ def __repr__(self):
1343
1344
above.""" )
1344
1345
1345
1346
1347
+ def _set_default (type_param , default ):
1348
+ if isinstance (default , (tuple , list )):
1349
+ type_param .__default__ = tuple ((typing ._type_check (d , "Default must be a type" )
1350
+ for d in default ))
1351
+ elif default != _marker :
1352
+ type_param .__default__ = typing ._type_check (default , "Default must be a type" )
1353
+ else :
1354
+ type_param .__default__ = None
1355
+
1356
+
1346
1357
class _DefaultMixin :
1347
1358
"""Mixin for TypeVarLike defaults."""
1348
1359
1349
1360
__slots__ = ()
1350
-
1351
- def __init__ (self , default ):
1352
- if isinstance (default , (tuple , list )):
1353
- self .__default__ = tuple ((typing ._type_check (d , "Default must be a type" )
1354
- for d in default ))
1355
- elif default != _marker :
1356
- self .__default__ = typing ._type_check (default , "Default must be a type" )
1357
- else :
1358
- self .__default__ = None
1361
+ __init__ = _set_default
1359
1362
1360
1363
1361
1364
# Add default and infer_variance parameters from PEP 696 and 695
1362
- class TypeVar (typing .TypeVar , _DefaultMixin , _root = True ):
1363
- """Type variable."""
1364
-
1365
- __module__ = 'typing'
1366
-
1367
- def __init__ (self , name , * constraints , bound = None ,
1365
+ class _TypeVarMeta (type ):
1366
+ def __call__ (self , name , * constraints , bound = None ,
1368
1367
covariant = False , contravariant = False ,
1369
1368
default = _marker , infer_variance = False ):
1370
- super ().__init__ (name , * constraints , bound = bound , covariant = covariant ,
1371
- contravariant = contravariant )
1372
- _DefaultMixin .__init__ (self , default )
1373
- self .__infer_variance__ = infer_variance
1369
+ if hasattr (typing , "TypeAliasType" ):
1370
+ # PEP 695 implemented, can pass infer_variance to typing.TypeVar
1371
+ typevar = typing .TypeVar (name , * constraints , bound = bound ,
1372
+ covariant = covariant , contravariant = contravariant ,
1373
+ infer_variance = infer_variance )
1374
+ else :
1375
+ typevar = typing .TypeVar (name , * constraints , bound = bound ,
1376
+ covariant = covariant , contravariant = contravariant )
1377
+ typevar .__infer_variance__ = infer_variance
1378
+ _set_default (typevar , default )
1374
1379
1375
1380
# for pickling:
1376
1381
def_mod = _caller ()
1377
1382
if def_mod != 'typing_extensions' :
1378
- self .__module__ = def_mod
1383
+ typevar .__module__ = def_mod
1384
+ return typevar
1385
+
1386
+ def __instancecheck__ (self , __instance : Any ) -> bool :
1387
+ return isinstance (__instance , typing .TypeVar )
1388
+
1389
+
1390
+ class TypeVar (metaclass = _TypeVarMeta ):
1391
+ """Type variable."""
1392
+
1393
+ __module__ = 'typing'
1379
1394
1380
1395
1381
1396
# Python 3.10+ has PEP 612
@@ -1443,22 +1458,28 @@ def __eq__(self, other):
1443
1458
# 3.10+
1444
1459
if hasattr (typing , 'ParamSpec' ):
1445
1460
1446
- # Add default Parameter - PEP 696
1447
- class ParamSpec (typing .ParamSpec , _DefaultMixin , _root = True ):
1448
- """Parameter specification variable."""
1449
-
1450
- __module__ = 'typing'
1451
-
1452
- def __init__ (self , name , * , bound = None , covariant = False , contravariant = False ,
1461
+ # Add default parameter - PEP 696
1462
+ class _ParamSpecMeta (type ):
1463
+ def __call__ (self , name , * , bound = None ,
1464
+ covariant = False , contravariant = False ,
1453
1465
default = _marker ):
1454
- super (). __init__ (name , bound = bound , covariant = covariant ,
1455
- contravariant = contravariant )
1456
- _DefaultMixin . __init__ ( self , default )
1466
+ paramspec = typing . ParamSpec (name , bound = bound ,
1467
+ covariant = covariant , contravariant = contravariant )
1468
+ _set_default ( paramspec , default )
1457
1469
1458
1470
# for pickling:
1459
1471
def_mod = _caller ()
1460
1472
if def_mod != 'typing_extensions' :
1461
- self .__module__ = def_mod
1473
+ paramspec .__module__ = def_mod
1474
+ return paramspec
1475
+
1476
+ def __instancecheck__ (self , __instance : Any ) -> bool :
1477
+ return isinstance (__instance , typing .ParamSpec )
1478
+
1479
+ class ParamSpec (metaclass = _ParamSpecMeta ):
1480
+ """Parameter specification."""
1481
+
1482
+ __module__ = 'typing'
1462
1483
1463
1484
# 3.7-3.9
1464
1485
else :
@@ -2061,18 +2082,28 @@ def _is_unpack(obj):
2061
2082
2062
2083
if hasattr (typing , "TypeVarTuple" ): # 3.11+
2063
2084
2064
- # Add default Parameter - PEP 696
2065
- class TypeVarTuple (typing .TypeVarTuple , _DefaultMixin , _root = True ):
2066
- """Type variable tuple."""
2067
-
2068
- def __init__ (self , name , * , default = _marker ):
2069
- super ().__init__ (name )
2070
- _DefaultMixin .__init__ (self , default )
2085
+ # Add default parameter - PEP 696
2086
+ class _TypeVarTupleMeta (type ):
2087
+ def __call__ (self , name , * , default = _marker ):
2088
+ tvt = typing .TypeVarTuple (name )
2089
+ _set_default (tvt , default )
2071
2090
2072
2091
# for pickling:
2073
2092
def_mod = _caller ()
2074
2093
if def_mod != 'typing_extensions' :
2075
- self .__module__ = def_mod
2094
+ tvt .__module__ = def_mod
2095
+ return tvt
2096
+
2097
+ def __instancecheck__ (self , __instance : Any ) -> bool :
2098
+ return isinstance (__instance , typing .TypeVarTuple )
2099
+
2100
+ class TypeVarTuple (metaclass = _TypeVarTupleMeta ):
2101
+ """Type variable tuple."""
2102
+
2103
+ __module__ = 'typing'
2104
+
2105
+ def __init_subclass__ (self , * args , ** kwds ):
2106
+ raise TypeError ("Cannot subclass special typing classes" )
2076
2107
2077
2108
else :
2078
2109
class TypeVarTuple (_DefaultMixin ):
0 commit comments