Skip to content

Commit

Permalink
Further simplify the implementations of the TypeVarLikes (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed May 22, 2023
1 parent 773090f commit 8054a29
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,18 +1371,21 @@ class _DefaultMixin:
__init__ = _set_default


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


# Add default and infer_variance parameters from PEP 696 and 695
class _TypeVarMeta(_TypeVarLikeMeta):
class TypeVar(metaclass=_TypeVarLikeMeta):
"""Type variable."""

_backported_typevarlike = typing.TypeVar

def __call__(self, name, *constraints, bound=None,
covariant=False, contravariant=False,
default=_marker, infer_variance=False):
def __new__(cls, name, *constraints, bound=None,
covariant=False, contravariant=False,
default=_marker, infer_variance=False):
if hasattr(typing, "TypeAliasType"):
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
typevar = typing.TypeVar(name, *constraints, bound=bound,
Expand All @@ -1398,10 +1401,6 @@ def __call__(self, name, *constraints, bound=None,
_set_module(typevar)
return typevar


class TypeVar(metaclass=_TypeVarMeta):
"""Type variable."""

def __init_subclass__(cls) -> None:
raise TypeError(f"type '{__name__}.TypeVar' is not an acceptable base type")

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

# Add default parameter - PEP 696
class _ParamSpecMeta(_TypeVarLikeMeta):
class ParamSpec(metaclass=_TypeVarLikeMeta):
"""Parameter specification."""

_backported_typevarlike = typing.ParamSpec

def __call__(self, name, *, bound=None,
covariant=False, contravariant=False,
infer_variance=False, default=_marker):
def __new__(cls, name, *, bound=None,
covariant=False, contravariant=False,
infer_variance=False, default=_marker):
if hasattr(typing, "TypeAliasType"):
# PEP 695 implemented, can pass infer_variance to typing.TypeVar
paramspec = typing.ParamSpec(name, bound=bound,
Expand All @@ -1494,9 +1495,6 @@ def __call__(self, name, *, bound=None,
_set_module(paramspec)
return paramspec

class ParamSpec(metaclass=_ParamSpecMeta):
"""Parameter specification."""

def __init_subclass__(cls) -> None:
raise TypeError(f"type '{__name__}.ParamSpec' is not an acceptable base type")

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

# Add default parameter - PEP 696
class _TypeVarTupleMeta(_TypeVarLikeMeta):
class TypeVarTuple(metaclass=_TypeVarLikeMeta):
"""Type variable tuple."""

_backported_typevarlike = typing.TypeVarTuple

def __call__(self, name, *, default=_marker):
def __new__(cls, name, *, default=_marker):
tvt = typing.TypeVarTuple(name)
_set_default(tvt, default)
_set_module(tvt)
return tvt

class TypeVarTuple(metaclass=_TypeVarTupleMeta):
"""Type variable tuple."""

def __init_subclass__(self, *args, **kwds):
raise TypeError("Cannot subclass special typing classes")

Expand Down

0 comments on commit 8054a29

Please sign in to comment.