Skip to content

Commit 8054a29

Browse files
authored
Further simplify the implementations of the TypeVarLikes (#176)
1 parent 773090f commit 8054a29

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/typing_extensions.py

+17-20
Original file line numberDiff line numberDiff line change
@@ -1371,18 +1371,21 @@ class _DefaultMixin:
13711371
__init__ = _set_default
13721372

13731373

1374+
# Classes using this metaclass must provide a _backported_typevarlike ClassVar
13741375
class _TypeVarLikeMeta(type):
13751376
def __instancecheck__(cls, __instance: Any) -> bool:
13761377
return isinstance(__instance, cls._backported_typevarlike)
13771378

13781379

13791380
# Add default and infer_variance parameters from PEP 696 and 695
1380-
class _TypeVarMeta(_TypeVarLikeMeta):
1381+
class TypeVar(metaclass=_TypeVarLikeMeta):
1382+
"""Type variable."""
1383+
13811384
_backported_typevarlike = typing.TypeVar
13821385

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):
13861389
if hasattr(typing, "TypeAliasType"):
13871390
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
13881391
typevar = typing.TypeVar(name, *constraints, bound=bound,
@@ -1398,10 +1401,6 @@ def __call__(self, name, *constraints, bound=None,
13981401
_set_module(typevar)
13991402
return typevar
14001403

1401-
1402-
class TypeVar(metaclass=_TypeVarMeta):
1403-
"""Type variable."""
1404-
14051404
def __init_subclass__(cls) -> None:
14061405
raise TypeError(f"type '{__name__}.TypeVar' is not an acceptable base type")
14071406

@@ -1472,12 +1471,14 @@ def __eq__(self, other):
14721471
if hasattr(typing, 'ParamSpec'):
14731472

14741473
# Add default parameter - PEP 696
1475-
class _ParamSpecMeta(_TypeVarLikeMeta):
1474+
class ParamSpec(metaclass=_TypeVarLikeMeta):
1475+
"""Parameter specification."""
1476+
14761477
_backported_typevarlike = typing.ParamSpec
14771478

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):
14811482
if hasattr(typing, "TypeAliasType"):
14821483
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
14831484
paramspec = typing.ParamSpec(name, bound=bound,
@@ -1494,9 +1495,6 @@ def __call__(self, name, *, bound=None,
14941495
_set_module(paramspec)
14951496
return paramspec
14961497

1497-
class ParamSpec(metaclass=_ParamSpecMeta):
1498-
"""Parameter specification."""
1499-
15001498
def __init_subclass__(cls) -> None:
15011499
raise TypeError(f"type '{__name__}.ParamSpec' is not an acceptable base type")
15021500

@@ -2105,18 +2103,17 @@ def _is_unpack(obj):
21052103
if hasattr(typing, "TypeVarTuple"): # 3.11+
21062104

21072105
# Add default parameter - PEP 696
2108-
class _TypeVarTupleMeta(_TypeVarLikeMeta):
2106+
class TypeVarTuple(metaclass=_TypeVarLikeMeta):
2107+
"""Type variable tuple."""
2108+
21092109
_backported_typevarlike = typing.TypeVarTuple
21102110

2111-
def __call__(self, name, *, default=_marker):
2111+
def __new__(cls, name, *, default=_marker):
21122112
tvt = typing.TypeVarTuple(name)
21132113
_set_default(tvt, default)
21142114
_set_module(tvt)
21152115
return tvt
21162116

2117-
class TypeVarTuple(metaclass=_TypeVarTupleMeta):
2118-
"""Type variable tuple."""
2119-
21202117
def __init_subclass__(self, *args, **kwds):
21212118
raise TypeError("Cannot subclass special typing classes")
21222119

0 commit comments

Comments
 (0)