Skip to content

Further simplify the implementations of the TypeVarLikes #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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