summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-05-16 03:36:23 (GMT)
committerGitHub <noreply@github.com>2023-05-16 03:36:23 (GMT)
commit24d8b88420b81fc60aeb0cbcacef1e72d633824a (patch)
tree1b06e157ddc7d1066fd41a28d2c27270ccf2e278 /Lib/typing.py
parentfdafdc235e74f2f4fedc1f745bf8b90141daa162 (diff)
downloadcpython-24d8b88420b81fc60aeb0cbcacef1e72d633824a.zip
cpython-24d8b88420b81fc60aeb0cbcacef1e72d633824a.tar.gz
cpython-24d8b88420b81fc60aeb0cbcacef1e72d633824a.tar.bz2
gh-103763: Implement PEP 695 (#103764)
This implements PEP 695, Type Parameter Syntax. It adds support for: - Generic functions (def func[T](): ...) - Generic classes (class X[T](): ...) - Type aliases (type X = ...) - New scoping when the new syntax is used within a class body - Compiler and interpreter changes to support the new syntax and scoping rules Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Eric Traut <eric@traut.com> Co-authored-by: Larry Hastings <larry@hastings.org> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py591
1 files changed, 170 insertions, 421 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 61aed09..8d132e2 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -23,6 +23,7 @@ from abc import abstractmethod, ABCMeta
import collections
from collections import defaultdict
import collections.abc
+import copyreg
import contextlib
import functools
import operator
@@ -32,12 +33,16 @@ import types
import warnings
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
-
-try:
- from _typing import _idfunc
-except ImportError:
- def _idfunc(_, x):
- return x
+from _typing import (
+ _idfunc,
+ TypeVar,
+ ParamSpec,
+ TypeVarTuple,
+ ParamSpecArgs,
+ ParamSpecKwargs,
+ TypeAliasType,
+ Generic,
+)
# Please keep __all__ alphabetized within each category.
__all__ = [
@@ -149,6 +154,7 @@ __all__ = [
'TYPE_CHECKING',
'TypeAlias',
'TypeGuard',
+ 'TypeAliasType',
'Unpack',
]
@@ -695,6 +701,15 @@ def Union(self, parameters):
return _UnionGenericAlias(self, parameters, name="Optional")
return _UnionGenericAlias(self, parameters)
+def _make_union(left, right):
+ """Used from the C implementation of TypeVar.
+
+ TypeVar.__or__ calls this instead of returning types.UnionType
+ because we want to allow unions between TypeVars and strings
+ (forward references.)
+ """
+ return Union[left, right]
+
@_SpecialForm
def Optional(self, parameters):
"""Optional type.
@@ -926,333 +941,162 @@ class _PickleUsingNameMixin:
return self.__name__
-class _BoundVarianceMixin:
- """Mixin giving __init__ bound and variance arguments.
-
- This is used by TypeVar and ParamSpec, which both employ the notions of
- a type 'bound' (restricting type arguments to be a subtype of some
- specified type) and type 'variance' (determining subtype relations between
- generic types).
- """
- def __init__(self, bound, covariant, contravariant):
- """Used to setup TypeVars and ParamSpec's bound, covariant and
- contravariant attributes.
- """
- if covariant and contravariant:
- raise ValueError("Bivariant types are not supported.")
- self.__covariant__ = bool(covariant)
- self.__contravariant__ = bool(contravariant)
- if bound:
- self.__bound__ = _type_check(bound, "Bound must be a type.")
- else:
- self.__bound__ = None
-
- def __or__(self, right):
- return Union[self, right]
-
- def __ror__(self, left):
- return Union[left, self]
-
- def __repr__(self):
- if self.__covariant__:
- prefix = '+'
- elif self.__contravariant__:
- prefix = '-'
- else:
- prefix = '~'
- return prefix + self.__name__
-
- def __mro_entries__(self, bases):
- raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
-
-
-class TypeVar(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
- _root=True):
- """Type variable.
-
- Usage::
-
- T = TypeVar('T') # Can be anything
- A = TypeVar('A', str, bytes) # Must be str or bytes
-
- Type variables exist primarily for the benefit of static type
- checkers. They serve as the parameters for generic types as well
- as for generic function definitions. See class Generic for more
- information on generic types. Generic functions work as follows:
-
- def repeat(x: T, n: int) -> List[T]:
- '''Return a list containing n references to x.'''
- return [x]*n
-
- def longest(x: A, y: A) -> A:
- '''Return the longest of two strings.'''
- return x if len(x) >= len(y) else y
-
- The latter example's signature is essentially the overloading
- of (str, str) -> str and (bytes, bytes) -> bytes. Also note
- that if the arguments are instances of some subclass of str,
- the return type is still plain str.
-
- At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
-
- Type variables defined with covariant=True or contravariant=True
- can be used to declare covariant or contravariant generic types.
- See PEP 484 for more details. By default generic types are invariant
- in all type variables.
-
- Type variables can be introspected. e.g.:
-
- T.__name__ == 'T'
- T.__constraints__ == ()
- T.__covariant__ == False
- T.__contravariant__ = False
- A.__constraints__ == (str, bytes)
-
- Note that only type variables defined in global scope can be pickled.
- """
-
- def __init__(self, name, *constraints, bound=None,
- covariant=False, contravariant=False):
- self.__name__ = name
- super().__init__(bound, covariant, contravariant)
- if constraints and bound is not None:
- raise TypeError("Constraints cannot be combined with bound=...")
- if constraints and len(constraints) == 1:
- raise TypeError("A single constraint is not allowed")
- msg = "TypeVar(name, constraint, ...): constraints must be types."
- self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
- def_mod = _caller()
- if def_mod != 'typing':
- self.__module__ = def_mod
+def _typevar_subst(self, arg):
+ msg = "Parameters to generic types must be types."
+ arg = _type_check(arg, msg, is_argument=True)
+ if ((isinstance(arg, _GenericAlias) and arg.__origin__ is Unpack) or
+ (isinstance(arg, GenericAlias) and getattr(arg, '__unpacked__', False))):
+ raise TypeError(f"{arg} is not valid as type argument")
+ return arg
- def __typing_subst__(self, arg):
- msg = "Parameters to generic types must be types."
- arg = _type_check(arg, msg, is_argument=True)
- if ((isinstance(arg, _GenericAlias) and arg.__origin__ is Unpack) or
- (isinstance(arg, GenericAlias) and getattr(arg, '__unpacked__', False))):
- raise TypeError(f"{arg} is not valid as type argument")
- return arg
+def _typevartuple_prepare_subst(self, alias, args):
+ params = alias.__parameters__
+ typevartuple_index = params.index(self)
+ for param in params[typevartuple_index + 1:]:
+ if isinstance(param, TypeVarTuple):
+ raise TypeError(f"More than one TypeVarTuple parameter in {alias}")
+
+ alen = len(args)
+ plen = len(params)
+ left = typevartuple_index
+ right = plen - typevartuple_index - 1
+ var_tuple_index = None
+ fillarg = None
+ for k, arg in enumerate(args):
+ if not isinstance(arg, type):
+ subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
+ if subargs and len(subargs) == 2 and subargs[-1] is ...:
+ if var_tuple_index is not None:
+ raise TypeError("More than one unpacked arbitrary-length tuple argument")
+ var_tuple_index = k
+ fillarg = subargs[0]
+ if var_tuple_index is not None:
+ left = min(left, var_tuple_index)
+ right = min(right, alen - var_tuple_index - 1)
+ elif left + right > alen:
+ raise TypeError(f"Too few arguments for {alias};"
+ f" actual {alen}, expected at least {plen-1}")
-class TypeVarTuple(_Final, _Immutable, _PickleUsingNameMixin, _root=True):
- """Type variable tuple.
+ return (
+ *args[:left],
+ *([fillarg]*(typevartuple_index - left)),
+ tuple(args[left: alen - right]),
+ *([fillarg]*(plen - right - left - typevartuple_index - 1)),
+ *args[alen - right:],
+ )
- Usage:
- Ts = TypeVarTuple('Ts') # Can be given any name
+def _paramspec_subst(self, arg):
+ if isinstance(arg, (list, tuple)):
+ arg = tuple(_type_check(a, "Expected a type.") for a in arg)
+ elif not _is_param_expr(arg):
+ raise TypeError(f"Expected a list of types, an ellipsis, "
+ f"ParamSpec, or Concatenate. Got {arg}")
+ return arg
- Just as a TypeVar (type variable) is a placeholder for a single type,
- a TypeVarTuple is a placeholder for an *arbitrary* number of types. For
- example, if we define a generic class using a TypeVarTuple:
- class C(Generic[*Ts]): ...
+def _paramspec_prepare_subst(self, alias, args):
+ params = alias.__parameters__
+ i = params.index(self)
+ if i >= len(args):
+ raise TypeError(f"Too few arguments for {alias}")
+ # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
+ if len(params) == 1 and not _is_param_expr(args[0]):
+ assert i == 0
+ args = (args,)
+ # Convert lists to tuples to help other libraries cache the results.
+ elif isinstance(args[i], list):
+ args = (*args[:i], tuple(args[i]), *args[i+1:])
+ return args
- Then we can parameterize that class with an arbitrary number of type
- arguments:
- C[int] # Fine
- C[int, str] # Also fine
- C[()] # Even this is fine
+@_tp_cache
+def _generic_class_getitem(cls, params):
+ """Parameterizes a generic class.
- For more details, see PEP 646.
+ At least, parameterizing a generic class is the *main* thing this method
+ does. For example, for some generic class `Foo`, this is called when we
+ do `Foo[int]` - there, with `cls=Foo` and `params=int`.
- Note that only TypeVarTuples defined in global scope can be pickled.
+ However, note that this method is also called when defining generic
+ classes in the first place with `class Foo(Generic[T]): ...`.
"""
+ if not isinstance(params, tuple):
+ params = (params,)
- def __init__(self, name):
- self.__name__ = name
-
- # Used for pickling.
- def_mod = _caller()
- if def_mod != 'typing':
- self.__module__ = def_mod
-
- def __iter__(self):
- yield Unpack[self]
-
- def __repr__(self):
- return self.__name__
+ params = tuple(_type_convert(p) for p in params)
+ is_generic_or_protocol = cls in (Generic, Protocol)
- def __typing_subst__(self, arg):
- raise TypeError("Substitution of bare TypeVarTuple is not supported")
+ if is_generic_or_protocol:
+ # Generic and Protocol can only be subscripted with unique type variables.
+ if not params:
+ raise TypeError(
+ f"Parameter list to {cls.__qualname__}[...] cannot be empty"
+ )
+ if not all(_is_typevar_like(p) for p in params):
+ raise TypeError(
+ f"Parameters to {cls.__name__}[...] must all be type variables "
+ f"or parameter specification variables.")
+ if len(set(params)) != len(params):
+ raise TypeError(
+ f"Parameters to {cls.__name__}[...] must all be unique")
+ else:
+ # Subscripting a regular Generic subclass.
+ for param in cls.__parameters__:
+ prepare = getattr(param, '__typing_prepare_subst__', None)
+ if prepare is not None:
+ params = prepare(cls, params)
+ _check_generic(cls, params, len(cls.__parameters__))
- def __typing_prepare_subst__(self, alias, args):
- params = alias.__parameters__
- typevartuple_index = params.index(self)
- for param in params[typevartuple_index + 1:]:
+ new_args = []
+ for param, new_arg in zip(cls.__parameters__, params):
if isinstance(param, TypeVarTuple):
- raise TypeError(f"More than one TypeVarTuple parameter in {alias}")
-
- alen = len(args)
- plen = len(params)
- left = typevartuple_index
- right = plen - typevartuple_index - 1
- var_tuple_index = None
- fillarg = None
- for k, arg in enumerate(args):
- if not isinstance(arg, type):
- subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
- if subargs and len(subargs) == 2 and subargs[-1] is ...:
- if var_tuple_index is not None:
- raise TypeError("More than one unpacked arbitrary-length tuple argument")
- var_tuple_index = k
- fillarg = subargs[0]
- if var_tuple_index is not None:
- left = min(left, var_tuple_index)
- right = min(right, alen - var_tuple_index - 1)
- elif left + right > alen:
- raise TypeError(f"Too few arguments for {alias};"
- f" actual {alen}, expected at least {plen-1}")
-
- return (
- *args[:left],
- *([fillarg]*(typevartuple_index - left)),
- tuple(args[left: alen - right]),
- *([fillarg]*(plen - right - left - typevartuple_index - 1)),
- *args[alen - right:],
- )
-
- def __mro_entries__(self, bases):
- raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
-
-
-class ParamSpecArgs(_Final, _Immutable, _root=True):
- """The args for a ParamSpec object.
-
- Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
-
- ParamSpecArgs objects have a reference back to their ParamSpec:
-
- P.args.__origin__ is P
-
- This type is meant for runtime introspection and has no special meaning to
- static type checkers.
- """
- def __init__(self, origin):
- self.__origin__ = origin
-
- def __repr__(self):
- return f"{self.__origin__.__name__}.args"
-
- def __eq__(self, other):
- if not isinstance(other, ParamSpecArgs):
- return NotImplemented
- return self.__origin__ == other.__origin__
-
- def __mro_entries__(self, bases):
- raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
-
-
-class ParamSpecKwargs(_Final, _Immutable, _root=True):
- """The kwargs for a ParamSpec object.
-
- Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
-
- ParamSpecKwargs objects have a reference back to their ParamSpec:
-
- P.kwargs.__origin__ is P
-
- This type is meant for runtime introspection and has no special meaning to
- static type checkers.
- """
- def __init__(self, origin):
- self.__origin__ = origin
-
- def __repr__(self):
- return f"{self.__origin__.__name__}.kwargs"
-
- def __eq__(self, other):
- if not isinstance(other, ParamSpecKwargs):
- return NotImplemented
- return self.__origin__ == other.__origin__
-
- def __mro_entries__(self, bases):
- raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
-
-
-class ParamSpec(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
- _root=True):
- """Parameter specification variable.
-
- Usage::
-
- P = ParamSpec('P')
-
- Parameter specification variables exist primarily for the benefit of static
- type checkers. They are used to forward the parameter types of one
- callable to another callable, a pattern commonly found in higher order
- functions and decorators. They are only valid when used in ``Concatenate``,
- or as the first argument to ``Callable``, or as parameters for user-defined
- Generics. See class Generic for more information on generic types. An
- example for annotating a decorator::
-
- T = TypeVar('T')
- P = ParamSpec('P')
-
- def add_logging(f: Callable[P, T]) -> Callable[P, T]:
- '''A type-safe decorator to add logging to a function.'''
- def inner(*args: P.args, **kwargs: P.kwargs) -> T:
- logging.info(f'{f.__name__} was called')
- return f(*args, **kwargs)
- return inner
-
- @add_logging
- def add_two(x: float, y: float) -> float:
- '''Add two numbers together.'''
- return x + y
-
- Parameter specification variables defined with covariant=True or
- contravariant=True can be used to declare covariant or contravariant
- generic types. These keyword arguments are valid, but their actual semantics
- are yet to be decided. See PEP 612 for details.
-
- Parameter specification variables can be introspected. e.g.:
-
- P.__name__ == 'P'
- P.__bound__ == None
- P.__covariant__ == False
- P.__contravariant__ == False
-
- Note that only parameter specification variables defined in global scope can
- be pickled.
- """
+ new_args.extend(new_arg)
+ else:
+ new_args.append(new_arg)
+ params = tuple(new_args)
- @property
- def args(self):
- return ParamSpecArgs(self)
+ return _GenericAlias(cls, params)
- @property
- def kwargs(self):
- return ParamSpecKwargs(self)
-
- def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
- self.__name__ = name
- super().__init__(bound, covariant, contravariant)
- def_mod = _caller()
- if def_mod != 'typing':
- self.__module__ = def_mod
- def __typing_subst__(self, arg):
- if isinstance(arg, (list, tuple)):
- arg = tuple(_type_check(a, "Expected a type.") for a in arg)
- elif not _is_param_expr(arg):
- raise TypeError(f"Expected a list of types, an ellipsis, "
- f"ParamSpec, or Concatenate. Got {arg}")
- return arg
+def _generic_init_subclass(cls, *args, **kwargs):
+ super(Generic, cls).__init_subclass__(*args, **kwargs)
+ tvars = []
+ if '__orig_bases__' in cls.__dict__:
+ error = Generic in cls.__orig_bases__
+ else:
+ error = (Generic in cls.__bases__ and
+ cls.__name__ != 'Protocol' and
+ type(cls) != _TypedDictMeta)
+ if error:
+ raise TypeError("Cannot inherit from plain Generic")
+ if '__orig_bases__' in cls.__dict__:
+ tvars = _collect_parameters(cls.__orig_bases__)
+ # Look for Generic[T1, ..., Tn].
+ # If found, tvars must be a subset of it.
+ # If not found, tvars is it.
+ # Also check for and reject plain Generic,
+ # and reject multiple Generic[...].
+ gvars = None
+ for base in cls.__orig_bases__:
+ if (isinstance(base, _GenericAlias) and
+ base.__origin__ is Generic):
+ if gvars is not None:
+ raise TypeError(
+ "Cannot inherit from Generic[...] multiple times.")
+ gvars = base.__parameters__
+ if gvars is not None:
+ tvarset = set(tvars)
+ gvarset = set(gvars)
+ if not tvarset <= gvarset:
+ s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
+ s_args = ', '.join(str(g) for g in gvars)
+ raise TypeError(f"Some type variables ({s_vars}) are"
+ f" not listed in Generic[{s_args}]")
+ tvars = gvars
+ cls.__parameters__ = tuple(tvars)
- def __typing_prepare_subst__(self, alias, args):
- params = alias.__parameters__
- i = params.index(self)
- if i >= len(args):
- raise TypeError(f"Too few arguments for {alias}")
- # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
- if len(params) == 1 and not _is_param_expr(args[0]):
- assert i == 0
- args = (args,)
- # Convert lists to tuples to help other libraries cache the results.
- elif isinstance(args[i], list):
- args = (*args[:i], tuple(args[i]), *args[i+1:])
- return args
def _is_dunder(attr):
return attr.startswith('__') and attr.endswith('__')
@@ -1812,113 +1656,6 @@ class _UnpackGenericAlias(_GenericAlias, _root=True):
return isinstance(self.__args__[0], TypeVarTuple)
-class Generic:
- """Abstract base class for generic types.
-
- A generic type is typically declared by inheriting from
- this class parameterized with one or more type variables.
- For example, a generic mapping type might be defined as::
-
- class Mapping(Generic[KT, VT]):
- def __getitem__(self, key: KT) -> VT:
- ...
- # Etc.
-
- This class can then be used as follows::
-
- def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
- try:
- return mapping[key]
- except KeyError:
- return default
- """
- __slots__ = ()
- _is_protocol = False
-
- @_tp_cache
- def __class_getitem__(cls, params):
- """Parameterizes a generic class.
-
- At least, parameterizing a generic class is the *main* thing this method
- does. For example, for some generic class `Foo`, this is called when we
- do `Foo[int]` - there, with `cls=Foo` and `params=int`.
-
- However, note that this method is also called when defining generic
- classes in the first place with `class Foo(Generic[T]): ...`.
- """
- if not isinstance(params, tuple):
- params = (params,)
-
- params = tuple(_type_convert(p) for p in params)
- if cls in (Generic, Protocol):
- # Generic and Protocol can only be subscripted with unique type variables.
- if not params:
- raise TypeError(
- f"Parameter list to {cls.__qualname__}[...] cannot be empty"
- )
- if not all(_is_typevar_like(p) for p in params):
- raise TypeError(
- f"Parameters to {cls.__name__}[...] must all be type variables "
- f"or parameter specification variables.")
- if len(set(params)) != len(params):
- raise TypeError(
- f"Parameters to {cls.__name__}[...] must all be unique")
- else:
- # Subscripting a regular Generic subclass.
- for param in cls.__parameters__:
- prepare = getattr(param, '__typing_prepare_subst__', None)
- if prepare is not None:
- params = prepare(cls, params)
- _check_generic(cls, params, len(cls.__parameters__))
-
- new_args = []
- for param, new_arg in zip(cls.__parameters__, params):
- if isinstance(param, TypeVarTuple):
- new_args.extend(new_arg)
- else:
- new_args.append(new_arg)
- params = tuple(new_args)
-
- return _GenericAlias(cls, params)
-
- def __init_subclass__(cls, *args, **kwargs):
- super().__init_subclass__(*args, **kwargs)
- tvars = []
- if '__orig_bases__' in cls.__dict__:
- error = Generic in cls.__orig_bases__
- else:
- error = (Generic in cls.__bases__ and
- cls.__name__ != 'Protocol' and
- type(cls) != _TypedDictMeta)
- if error:
- raise TypeError("Cannot inherit from plain Generic")
- if '__orig_bases__' in cls.__dict__:
- tvars = _collect_parameters(cls.__orig_bases__)
- # Look for Generic[T1, ..., Tn].
- # If found, tvars must be a subset of it.
- # If not found, tvars is it.
- # Also check for and reject plain Generic,
- # and reject multiple Generic[...].
- gvars = None
- for base in cls.__orig_bases__:
- if (isinstance(base, _GenericAlias) and
- base.__origin__ is Generic):
- if gvars is not None:
- raise TypeError(
- "Cannot inherit from Generic[...] multiple times.")
- gvars = base.__parameters__
- if gvars is not None:
- tvarset = set(tvars)
- gvarset = set(gvars)
- if not tvarset <= gvarset:
- s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
- s_args = ', '.join(str(g) for g in gvars)
- raise TypeError(f"Some type variables ({s_vars}) are"
- f" not listed in Generic[{s_args}]")
- tvars = gvars
- cls.__parameters__ = tuple(tvars)
-
-
class _TypingEllipsis:
"""Internal placeholder for ... (ellipsis)."""
@@ -2024,6 +1761,18 @@ def _lazy_load_getattr_static():
_cleanups.append(_lazy_load_getattr_static.cache_clear)
+def _pickle_psargs(psargs):
+ return ParamSpecArgs, (psargs.__origin__,)
+
+copyreg.pickle(ParamSpecArgs, _pickle_psargs)
+
+def _pickle_pskwargs(pskwargs):
+ return ParamSpecKwargs, (pskwargs.__origin__,)
+
+copyreg.pickle(ParamSpecKwargs, _pickle_pskwargs)
+
+del _pickle_psargs, _pickle_pskwargs
+
class _ProtocolMeta(ABCMeta):
# This metaclass is really unfortunate and exists only because of
@@ -2943,7 +2692,7 @@ class NamedTupleMeta(type):
module=ns['__module__'])
nm_tpl.__bases__ = bases
if Generic in bases:
- class_getitem = Generic.__class_getitem__.__func__
+ class_getitem = _generic_class_getitem
nm_tpl.__class_getitem__ = classmethod(class_getitem)
# update from user namespace without overriding special namedtuple attributes
for key in ns: