@@ -1371,18 +1371,21 @@ class _DefaultMixin:
1371
1371
__init__ = _set_default
1372
1372
1373
1373
1374
+ # Classes using this metaclass must provide a _backported_typevarlike ClassVar
1374
1375
class _TypeVarLikeMeta (type ):
1375
1376
def __instancecheck__ (cls , __instance : Any ) -> bool :
1376
1377
return isinstance (__instance , cls ._backported_typevarlike )
1377
1378
1378
1379
1379
1380
# Add default and infer_variance parameters from PEP 696 and 695
1380
- class _TypeVarMeta (_TypeVarLikeMeta ):
1381
+ class TypeVar (metaclass = _TypeVarLikeMeta ):
1382
+ """Type variable."""
1383
+
1381
1384
_backported_typevarlike = typing .TypeVar
1382
1385
1383
- def __call__ ( self , name , * constraints , bound = None ,
1384
- covariant = False , contravariant = False ,
1385
- default = _marker , infer_variance = False ):
1386
+ def __new__ ( cls , name , * constraints , bound = None ,
1387
+ covariant = False , contravariant = False ,
1388
+ default = _marker , infer_variance = False ):
1386
1389
if hasattr (typing , "TypeAliasType" ):
1387
1390
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
1388
1391
typevar = typing .TypeVar (name , * constraints , bound = bound ,
@@ -1398,10 +1401,6 @@ def __call__(self, name, *constraints, bound=None,
1398
1401
_set_module (typevar )
1399
1402
return typevar
1400
1403
1401
-
1402
- class TypeVar (metaclass = _TypeVarMeta ):
1403
- """Type variable."""
1404
-
1405
1404
def __init_subclass__ (cls ) -> None :
1406
1405
raise TypeError (f"type '{ __name__ } .TypeVar' is not an acceptable base type" )
1407
1406
@@ -1472,12 +1471,14 @@ def __eq__(self, other):
1472
1471
if hasattr (typing , 'ParamSpec' ):
1473
1472
1474
1473
# Add default parameter - PEP 696
1475
- class _ParamSpecMeta (_TypeVarLikeMeta ):
1474
+ class ParamSpec (metaclass = _TypeVarLikeMeta ):
1475
+ """Parameter specification."""
1476
+
1476
1477
_backported_typevarlike = typing .ParamSpec
1477
1478
1478
- def __call__ ( self , name , * , bound = None ,
1479
- covariant = False , contravariant = False ,
1480
- infer_variance = False , default = _marker ):
1479
+ def __new__ ( cls , name , * , bound = None ,
1480
+ covariant = False , contravariant = False ,
1481
+ infer_variance = False , default = _marker ):
1481
1482
if hasattr (typing , "TypeAliasType" ):
1482
1483
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
1483
1484
paramspec = typing .ParamSpec (name , bound = bound ,
@@ -1494,9 +1495,6 @@ def __call__(self, name, *, bound=None,
1494
1495
_set_module (paramspec )
1495
1496
return paramspec
1496
1497
1497
- class ParamSpec (metaclass = _ParamSpecMeta ):
1498
- """Parameter specification."""
1499
-
1500
1498
def __init_subclass__ (cls ) -> None :
1501
1499
raise TypeError (f"type '{ __name__ } .ParamSpec' is not an acceptable base type" )
1502
1500
@@ -2105,18 +2103,17 @@ def _is_unpack(obj):
2105
2103
if hasattr (typing , "TypeVarTuple" ): # 3.11+
2106
2104
2107
2105
# Add default parameter - PEP 696
2108
- class _TypeVarTupleMeta (_TypeVarLikeMeta ):
2106
+ class TypeVarTuple (metaclass = _TypeVarLikeMeta ):
2107
+ """Type variable tuple."""
2108
+
2109
2109
_backported_typevarlike = typing .TypeVarTuple
2110
2110
2111
- def __call__ ( self , name , * , default = _marker ):
2111
+ def __new__ ( cls , name , * , default = _marker ):
2112
2112
tvt = typing .TypeVarTuple (name )
2113
2113
_set_default (tvt , default )
2114
2114
_set_module (tvt )
2115
2115
return tvt
2116
2116
2117
- class TypeVarTuple (metaclass = _TypeVarTupleMeta ):
2118
- """Type variable tuple."""
2119
-
2120
2117
def __init_subclass__ (self , * args , ** kwds ):
2121
2118
raise TypeError ("Cannot subclass special typing classes" )
2122
2119
0 commit comments