@@ -1351,15 +1351,33 @@ def _set_default(type_param, default):
13511351 type_param .__default__ = None
13521352
13531353
1354+ def _set_module (typevarlike ):
1355+ # for pickling:
1356+ def_mod = _caller (depth = 3 )
1357+ if def_mod != 'typing_extensions' :
1358+ typevarlike .__module__ = def_mod
1359+
1360+
13541361class _DefaultMixin :
13551362 """Mixin for TypeVarLike defaults."""
13561363
13571364 __slots__ = ()
13581365 __init__ = _set_default
13591366
13601367
1368+ class _TypeVarLikeMeta (type ):
1369+ def __init__ (cls , * args , ** kwargs ):
1370+ super ().__init__ (* args , ** kwargs )
1371+ cls .__module__ = 'typing'
1372+
1373+ def __instancecheck__ (cls , __instance : Any ) -> bool :
1374+ return isinstance (__instance , cls ._backported_typevarlike )
1375+
1376+
13611377# Add default and infer_variance parameters from PEP 696 and 695
1362- class _TypeVarMeta (type ):
1378+ class _TypeVarMeta (_TypeVarLikeMeta ):
1379+ _backported_typevarlike = typing .TypeVar
1380+
13631381 def __call__ (self , name , * constraints , bound = None ,
13641382 covariant = False , contravariant = False ,
13651383 default = _marker , infer_variance = False ):
@@ -1375,22 +1393,13 @@ def __call__(self, name, *constraints, bound=None,
13751393 raise ValueError ("Variance cannot be specified with infer_variance." )
13761394 typevar .__infer_variance__ = infer_variance
13771395 _set_default (typevar , default )
1378-
1379- # for pickling:
1380- def_mod = _caller ()
1381- if def_mod != 'typing_extensions' :
1382- typevar .__module__ = def_mod
1396+ _set_module (typevar )
13831397 return typevar
13841398
1385- def __instancecheck__ (self , __instance : Any ) -> bool :
1386- return isinstance (__instance , typing .TypeVar )
1387-
13881399
13891400class TypeVar (metaclass = _TypeVarMeta ):
13901401 """Type variable."""
13911402
1392- __module__ = 'typing'
1393-
13941403 def __init_subclass__ (cls ) -> None :
13951404 raise TypeError (f"type '{ __name__ } .TypeVar' is not an acceptable base type" )
13961405
@@ -1461,28 +1470,21 @@ def __eq__(self, other):
14611470if hasattr (typing , 'ParamSpec' ):
14621471
14631472 # Add default parameter - PEP 696
1464- class _ParamSpecMeta (type ):
1473+ class _ParamSpecMeta (_TypeVarLikeMeta ):
1474+ _backported_typevarlike = typing .ParamSpec
1475+
14651476 def __call__ (self , name , * , bound = None ,
14661477 covariant = False , contravariant = False ,
14671478 default = _marker ):
14681479 paramspec = typing .ParamSpec (name , bound = bound ,
14691480 covariant = covariant , contravariant = contravariant )
14701481 _set_default (paramspec , default )
1471-
1472- # for pickling:
1473- def_mod = _caller ()
1474- if def_mod != 'typing_extensions' :
1475- paramspec .__module__ = def_mod
1482+ _set_module (paramspec )
14761483 return paramspec
14771484
1478- def __instancecheck__ (self , __instance : Any ) -> bool :
1479- return isinstance (__instance , typing .ParamSpec )
1480-
14811485 class ParamSpec (metaclass = _ParamSpecMeta ):
14821486 """Parameter specification."""
14831487
1484- __module__ = 'typing'
1485-
14861488 def __init_subclass__ (cls ) -> None :
14871489 raise TypeError (f"type '{ __name__ } .ParamSpec' is not an acceptable base type" )
14881490
@@ -2088,25 +2090,18 @@ def _is_unpack(obj):
20882090if hasattr (typing , "TypeVarTuple" ): # 3.11+
20892091
20902092 # Add default parameter - PEP 696
2091- class _TypeVarTupleMeta (type ):
2093+ class _TypeVarTupleMeta (_TypeVarLikeMeta ):
2094+ _backported_typevarlike = typing .TypeVarTuple
2095+
20922096 def __call__ (self , name , * , default = _marker ):
20932097 tvt = typing .TypeVarTuple (name )
20942098 _set_default (tvt , default )
2095-
2096- # for pickling:
2097- def_mod = _caller ()
2098- if def_mod != 'typing_extensions' :
2099- tvt .__module__ = def_mod
2099+ _set_module (tvt )
21002100 return tvt
21012101
2102- def __instancecheck__ (self , __instance : Any ) -> bool :
2103- return isinstance (__instance , typing .TypeVarTuple )
2104-
21052102 class TypeVarTuple (metaclass = _TypeVarTupleMeta ):
21062103 """Type variable tuple."""
21072104
2108- __module__ = 'typing'
2109-
21102105 def __init_subclass__ (self , * args , ** kwds ):
21112106 raise TypeError ("Cannot subclass special typing classes" )
21122107
0 commit comments