[3.11] gh-105286: Improve typing.py docstrings (#105287) (#105322)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Alex Waygood 2023-06-05 17:11:35 +01:00 committed by GitHub
parent 48957888d2
commit 93d9e990fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,22 +1,22 @@
""" """
The typing module: Support for gradual typing as defined by PEP 484. The typing module: Support for gradual typing as defined by PEP 484 and subsequent PEPs.
At large scale, the structure of the module is following: Any name not present in __all__ is an implementation detail
* Imports and exports, all public names should be explicitly added to __all__. that may be changed without notice. Use at your own risk!
* Internal helper functions: these should never be used in code outside this module.
* _SpecialForm and its instances (special forms): Among other things, the module includes the following:
Any, NoReturn, Never, ClassVar, Union, Optional, Concatenate, Unpack * Generic, Protocol, and internal machinery to support generic aliases.
* Classes whose instances can be type arguments in addition to types: All subscripted types like X[int], Union[int, str] are generic aliases.
ForwardRef, TypeVar and ParamSpec * Various "special forms" that have unique meanings in type annotations:
* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is NoReturn, Never, ClassVar, Self, Concatenate, Unpack, and others.
currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str], * Classes whose instances can be type arguments to generic classes and functions:
etc., are instances of either of these classes. TypeVar, ParamSpec, TypeVarTuple.
* The public counterpart of the generics API consists of two classes: Generic and Protocol. * Public helper functions: get_type_hints, overload, cast, final, and others.
* Public helper functions: get_type_hints, overload, cast, no_type_check, * Several protocols to support duck-typing:
no_type_check_decorator. SupportsFloat, SupportsIndex, SupportsAbs, and others.
* Generic aliases for collections.abc ABCs and few additional protocols.
* Special types: NewType, NamedTuple, TypedDict. * Special types: NewType, NamedTuple, TypedDict.
* Wrapper submodules for re and io related types. * Deprecated wrapper submodules for re and io related types.
* Deprecated aliases for builtin types and collections.abc ABCs.
""" """
from abc import abstractmethod, ABCMeta from abc import abstractmethod, ABCMeta
@ -171,7 +171,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
As a special case, accept None and return type(None) instead. Also wrap strings As a special case, accept None and return type(None) instead. Also wrap strings
into ForwardRef instances. Consider several corner cases, for example plain into ForwardRef instances. Consider several corner cases, for example plain
special forms like Union are not valid, while Union[int, str] is OK, etc. special forms like Union are not valid, while Union[int, str] is OK, etc.
The msg argument is a human-readable error message, e.g:: The msg argument is a human-readable error message, e.g.::
"Union[arg, ...]: arg should be a type." "Union[arg, ...]: arg should be a type."
@ -207,7 +207,7 @@ def _should_unflatten_callable_args(typ, args):
"""Internal helper for munging collections.abc.Callable's __args__. """Internal helper for munging collections.abc.Callable's __args__.
The canonical representation for a Callable's __args__ flattens the The canonical representation for a Callable's __args__ flattens the
argument types, see https://bugs.python.org/issue42195. For example: argument types, see https://bugs.python.org/issue42195. For example::
collections.abc.Callable[[int, int], str].__args__ == (int, int, str) collections.abc.Callable[[int, int], str].__args__ == (int, int, str)
collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str) collections.abc.Callable[ParamSpec, str].__args__ == (ParamSpec, str)
@ -244,9 +244,11 @@ def _type_repr(obj):
def _collect_parameters(args): def _collect_parameters(args):
"""Collect all type variables and parameter specifications in args """Collect all type variables and parameter specifications in args
in order of first appearance (lexicographic order). For example:: in order of first appearance (lexicographic order).
_collect_parameters((T, Callable[P, T])) == (T, P) For example::
assert _collect_parameters((T, Callable[P, T])) == (T, P)
""" """
parameters = [] parameters = []
for t in args: for t in args:
@ -272,6 +274,7 @@ def _collect_parameters(args):
def _check_generic(cls, parameters, elen): def _check_generic(cls, parameters, elen):
"""Check correct count for parameters of a generic cls (internal helper). """Check correct count for parameters of a generic cls (internal helper).
This gives a nice error message in case of count mismatch. This gives a nice error message in case of count mismatch.
""" """
if not elen: if not elen:
@ -306,8 +309,9 @@ def _deduplicate(params):
def _remove_dups_flatten(parameters): def _remove_dups_flatten(parameters):
"""An internal helper for Union creation and substitution: flatten Unions """Internal helper for Union creation and substitution.
among parameters, then remove duplicates.
Flatten Unions among parameters, then remove duplicates.
""" """
# Flatten out Union[Union[...], ...]. # Flatten out Union[Union[...], ...].
params = [] params = []
@ -321,7 +325,7 @@ def _remove_dups_flatten(parameters):
def _flatten_literal_params(parameters): def _flatten_literal_params(parameters):
"""An internal helper for Literal creation: flatten Literals among parameters""" """Internal helper for Literal creation: flatten Literals among parameters."""
params = [] params = []
for p in parameters: for p in parameters:
if isinstance(p, _LiteralGenericAlias): if isinstance(p, _LiteralGenericAlias):
@ -358,6 +362,7 @@ def inner(*args, **kwds):
def _eval_type(t, globalns, localns, recursive_guard=frozenset()): def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
"""Evaluate all forward references in the given type t. """Evaluate all forward references in the given type t.
For use of globalns and localns see the docstring for get_type_hints(). For use of globalns and localns see the docstring for get_type_hints().
recursive_guard is used to prevent infinite recursion with a recursive recursive_guard is used to prevent infinite recursion with a recursive
ForwardRef. ForwardRef.
@ -390,7 +395,7 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
class _Final: class _Final:
"""Mixin to prohibit subclassing""" """Mixin to prohibit subclassing."""
__slots__ = ('__weakref__',) __slots__ = ('__weakref__',)
@ -400,6 +405,7 @@ def __init_subclass__(cls, /, *args, **kwds):
class _Immutable: class _Immutable:
"""Mixin to indicate that object should not be copied.""" """Mixin to indicate that object should not be copied."""
__slots__ = () __slots__ = ()
def __copy__(self): def __copy__(self):
@ -412,8 +418,10 @@ def __deepcopy__(self, memo):
class _NotIterable: class _NotIterable:
"""Mixin to prevent iteration, without being compatible with Iterable. """Mixin to prevent iteration, without being compatible with Iterable.
That is, we could do: That is, we could do::
def __iter__(self): raise TypeError() def __iter__(self): raise TypeError()
But this would make users of this mixin duck type-compatible with But this would make users of this mixin duck type-compatible with
collections.abc.Iterable - isinstance(foo, Iterable) would be True. collections.abc.Iterable - isinstance(foo, Iterable) would be True.
@ -500,6 +508,7 @@ class Any(metaclass=_AnyMeta):
static type checkers. At runtime, Any should not be used with instance static type checkers. At runtime, Any should not be used with instance
checks. checks.
""" """
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
if cls is Any: if cls is Any:
raise TypeError("Any cannot be instantiated") raise TypeError("Any cannot be instantiated")
@ -509,18 +518,18 @@ def __new__(cls, *args, **kwargs):
@_SpecialForm @_SpecialForm
def NoReturn(self, parameters): def NoReturn(self, parameters):
"""Special type indicating functions that never return. """Special type indicating functions that never return.
Example:: Example::
from typing import NoReturn from typing import NoReturn
def stop() -> NoReturn: def stop() -> NoReturn:
raise Exception('no way') raise Exception('no way')
NoReturn can also be used as a bottom type, a type that NoReturn can also be used as a bottom type, a type that
has no values. Starting in Python 3.11, the Never type should has no values. Starting in Python 3.11, the Never type should
be used for this concept instead. Type checkers should treat the two be used for this concept instead. Type checkers should treat the two
equivalently. equivalently.
""" """
raise TypeError(f"{self} is not subscriptable") raise TypeError(f"{self} is not subscriptable")
@ -548,7 +557,6 @@ def int_or_str(arg: int | str) -> None:
print("It's a str") print("It's a str")
case _: case _:
never_call_me(arg) # ok, arg is of type Never never_call_me(arg) # ok, arg is of type Never
""" """
raise TypeError(f"{self} is not subscriptable") raise TypeError(f"{self} is not subscriptable")
@ -559,12 +567,12 @@ def Self(self, parameters):
Example:: Example::
from typing import Self from typing import Self
class Foo: class Foo:
def return_self(self) -> Self: def return_self(self) -> Self:
... ...
return self return self
This is especially useful for: This is especially useful for:
- classmethods that are used as alternative constructors - classmethods that are used as alternative constructors
@ -596,7 +604,6 @@ def caller(arbitrary_string: str, literal_string: LiteralString) -> None:
Only string literals and other LiteralStrings are compatible Only string literals and other LiteralStrings are compatible
with LiteralString. This provides a tool to help prevent with LiteralString. This provides a tool to help prevent
security issues such as SQL injection. security issues such as SQL injection.
""" """
raise TypeError(f"{self} is not subscriptable") raise TypeError(f"{self} is not subscriptable")
@ -609,9 +616,9 @@ def ClassVar(self, parameters):
attribute is intended to be used as a class variable and attribute is intended to be used as a class variable and
should not be set on instances of that class. Usage:: should not be set on instances of that class. Usage::
class Starship: class Starship:
stats: ClassVar[Dict[str, int]] = {} # class variable stats: ClassVar[Dict[str, int]] = {} # class variable
damage: int = 10 # instance variable damage: int = 10 # instance variable
ClassVar accepts only types and cannot be further subscribed. ClassVar accepts only types and cannot be further subscribed.
@ -626,16 +633,17 @@ def Final(self, parameters):
"""Special typing construct to indicate final names to type checkers. """Special typing construct to indicate final names to type checkers.
A final name cannot be re-assigned or overridden in a subclass. A final name cannot be re-assigned or overridden in a subclass.
For example:
MAX_SIZE: Final = 9000 For example::
MAX_SIZE += 1 # Error reported by type checker
class Connection: MAX_SIZE: Final = 9000
TIMEOUT: Final[int] = 10 MAX_SIZE += 1 # Error reported by type checker
class FastConnector(Connection): class Connection:
TIMEOUT = 1 # Error reported by type checker TIMEOUT: Final[int] = 10
class FastConnector(Connection):
TIMEOUT = 1 # Error reported by type checker
There is no runtime checking of these properties. There is no runtime checking of these properties.
""" """
@ -646,25 +654,29 @@ class FastConnector(Connection):
def Union(self, parameters): def Union(self, parameters):
"""Union type; Union[X, Y] means either X or Y. """Union type; Union[X, Y] means either X or Y.
To define a union, use e.g. Union[int, str]. Details: On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one. - The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by - None as an argument is a special case and is replaced by
type(None). type(None).
- Unions of unions are flattened, e.g.:: - Unions of unions are flattened, e.g.::
Union[Union[int, str], float] == Union[int, str, float] assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.:: - Unions of a single argument vanish, e.g.::
Union[int] == int # The constructor actually returns int assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.:: - Redundant arguments are skipped, e.g.::
Union[int, str, int] == Union[int, str] assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.:: - When comparing unions, the argument order is ignored, e.g.::
Union[int, str] == Union[str, int] assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union. - You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None]. - You can use Optional[X] as a shorthand for Union[X, None].
@ -684,10 +696,7 @@ def Union(self, parameters):
@_SpecialForm @_SpecialForm
def Optional(self, parameters): def Optional(self, parameters):
"""Optional type. """Optional[X] is equivalent to Union[X, None]."""
Optional[X] is equivalent to Union[X, None].
"""
arg = _type_check(parameters, f"{self} requires a single type.") arg = _type_check(parameters, f"{self} requires a single type.")
return Union[arg, type(None)] return Union[arg, type(None)]
@ -698,17 +707,17 @@ def Literal(self, *parameters):
This form can be used to indicate to type checkers that the corresponding This form can be used to indicate to type checkers that the corresponding
variable or function parameter has a value equivalent to the provided variable or function parameter has a value equivalent to the provided
literal (or one of several literals): literal (or one of several literals)::
def validate_simple(data: Any) -> Literal[True]: # always returns True def validate_simple(data: Any) -> Literal[True]: # always returns True
... ...
MODE = Literal['r', 'rb', 'w', 'wb'] MODE = Literal['r', 'rb', 'w', 'wb']
def open_helper(file: str, mode: MODE) -> str: def open_helper(file: str, mode: MODE) -> str:
... ...
open_helper('/some/path', 'r') # Passes type check open_helper('/some/path', 'r') # Passes type check
open_helper('/other/path', 'typo') # Error in type checker open_helper('/other/path', 'typo') # Error in type checker
Literal[...] cannot be subclassed. At runtime, an arbitrary value Literal[...] cannot be subclassed. At runtime, an arbitrary value
is allowed as type argument to Literal[...], but type checkers may is allowed as type argument to Literal[...], but type checkers may
@ -728,11 +737,11 @@ def open_helper(file: str, mode: MODE) -> str:
@_SpecialForm @_SpecialForm
def TypeAlias(self, parameters): def TypeAlias(self, parameters):
"""Special marker indicating that an assignment should """Special form for marking type aliases.
be recognized as a proper type alias definition by type
checkers.
For example:: Use TypeAlias to indicate that an assignment should
be recognized as a proper type alias definition by type
checkers. For example::
Predicate: TypeAlias = Callable[..., bool] Predicate: TypeAlias = Callable[..., bool]
@ -743,13 +752,15 @@ def TypeAlias(self, parameters):
@_SpecialForm @_SpecialForm
def Concatenate(self, parameters): def Concatenate(self, parameters):
"""Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a """Special form for annotating higher-order functions.
higher order function which adds, removes or transforms parameters of a
callable. ``Concatenate`` can be sed in conjunction with ``ParamSpec`` and
``Callable`` to represent a higher order function which adds, removes or
transforms the parameters of a callable.
For example:: For example::
Callable[Concatenate[int, P], int] Callable[Concatenate[int, P], int]
See PEP 612 for detailed information. See PEP 612 for detailed information.
""" """
@ -768,7 +779,9 @@ def Concatenate(self, parameters):
@_SpecialForm @_SpecialForm
def TypeGuard(self, parameters): def TypeGuard(self, parameters):
"""Special typing form used to annotate the return type of a user-defined """Special typing construct for marking user-defined type guard functions.
``TypeGuard`` can be used to annotate the return type of a user-defined
type guard function. ``TypeGuard`` only accepts a single type argument. type guard function. ``TypeGuard`` only accepts a single type argument.
At runtime, functions marked this way should return a boolean. At runtime, functions marked this way should return a boolean.
@ -791,14 +804,14 @@ def TypeGuard(self, parameters):
For example:: For example::
def is_str(val: Union[str, float]): def is_str(val: Union[str, float]):
# "isinstance" type guard # "isinstance" type guard
if isinstance(val, str): if isinstance(val, str):
# Type of ``val`` is narrowed to ``str`` # Type of ``val`` is narrowed to ``str``
... ...
else: else:
# Else, type of ``val`` is narrowed to ``float``. # Else, type of ``val`` is narrowed to ``float``.
... ...
Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
form of ``TypeA`` (it can even be a wider form) and this may lead to form of ``TypeA`` (it can even be a wider form) and this may lead to
@ -1226,14 +1239,15 @@ def _is_dunder(attr):
return attr.startswith('__') and attr.endswith('__') return attr.startswith('__') and attr.endswith('__')
class _BaseGenericAlias(_Final, _root=True): class _BaseGenericAlias(_Final, _root=True):
"""The central part of internal API. """The central part of the internal API.
This represents a generic version of type 'origin' with type arguments 'params'. This represents a generic version of type 'origin' with type arguments 'params'.
There are two kind of these aliases: user defined and special. The special ones There are two kind of these aliases: user defined and special. The special ones
are wrappers around builtin collections and ABCs in collections.abc. These must are wrappers around builtin collections and ABCs in collections.abc. These must
have 'name' always set. If 'inst' is False, then the alias can't be instantiated, have 'name' always set. If 'inst' is False, then the alias can't be instantiated;
this is used by e.g. typing.List and typing.Dict. this is used by e.g. typing.List and typing.Dict.
""" """
def __init__(self, origin, *, inst=True, name=None): def __init__(self, origin, *, inst=True, name=None):
self._inst = inst self._inst = inst
self._name = name self._name = name
@ -1675,7 +1689,6 @@ def _value_and_type_iter(parameters):
class _LiteralGenericAlias(_GenericAlias, _root=True): class _LiteralGenericAlias(_GenericAlias, _root=True):
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, _LiteralGenericAlias): if not isinstance(other, _LiteralGenericAlias):
return NotImplemented return NotImplemented
@ -1701,21 +1714,21 @@ def Unpack(self, parameters):
The type unpack operator takes the child types from some container type, The type unpack operator takes the child types from some container type,
such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
example: example::
# For some generic class `Foo`: # For some generic class `Foo`:
Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str] Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
Ts = TypeVarTuple('Ts') Ts = TypeVarTuple('Ts')
# Specifies that `Bar` is generic in an arbitrary number of types. # Specifies that `Bar` is generic in an arbitrary number of types.
# (Think of `Ts` as a tuple of an arbitrary number of individual # (Think of `Ts` as a tuple of an arbitrary number of individual
# `TypeVar`s, which the `Unpack` is 'pulling out' directly into the # `TypeVar`s, which the `Unpack` is 'pulling out' directly into the
# `Generic[]`.) # `Generic[]`.)
class Bar(Generic[Unpack[Ts]]): ... class Bar(Generic[Unpack[Ts]]): ...
Bar[int] # Valid Bar[int] # Valid
Bar[int, str] # Also valid Bar[int, str] # Also valid
From Python 3.11, this can also be done using the `*` operator: From Python 3.11, this can also be done using the `*` operator::
Foo[*tuple[int, str]] Foo[*tuple[int, str]]
class Bar(Generic[*Ts]): ... class Bar(Generic[*Ts]): ...
@ -1730,7 +1743,6 @@ class Bar(Generic[*Ts]): ...
class _UnpackGenericAlias(_GenericAlias, _root=True): class _UnpackGenericAlias(_GenericAlias, _root=True):
def __repr__(self): def __repr__(self):
# `Unpack` only takes one argument, so __args__ should contain only # `Unpack` only takes one argument, so __args__ should contain only
# a single item. # a single item.
@ -2015,6 +2027,7 @@ class GenProto(Protocol[T]):
def meth(self) -> T: def meth(self) -> T:
... ...
""" """
__slots__ = () __slots__ = ()
_is_protocol = True _is_protocol = True
_is_runtime_protocol = False _is_runtime_protocol = False
@ -2088,12 +2101,13 @@ class _AnnotatedAlias(_NotIterable, _GenericAlias, _root=True):
"""Runtime representation of an annotated type. """Runtime representation of an annotated type.
At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't' At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
with extra annotations. The alias behaves like a normal typing alias, with extra annotations. The alias behaves like a normal typing alias.
instantiating is the same as instantiating the underlying type, binding Instantiating is the same as instantiating the underlying type; binding
it to types is also the same. it to types is also the same.
The metadata itself is stored in a '__metadata__' attribute as a tuple. The metadata itself is stored in a '__metadata__' attribute as a tuple.
""" """
def __init__(self, origin, metadata): def __init__(self, origin, metadata):
if isinstance(origin, _AnnotatedAlias): if isinstance(origin, _AnnotatedAlias):
metadata = origin.__metadata__ + metadata metadata = origin.__metadata__ + metadata
@ -2133,7 +2147,7 @@ def __getattr__(self, attr):
class Annotated: class Annotated:
"""Add context specific metadata to a type. """Add context-specific metadata to a type.
Example: Annotated[int, runtime_check.Unsigned] indicates to the Example: Annotated[int, runtime_check.Unsigned] indicates to the
hypothetical runtime_check module that this type is an unsigned int. hypothetical runtime_check module that this type is an unsigned int.
@ -2147,24 +2161,24 @@ class Annotated:
- It's an error to call `Annotated` with less than two arguments. - It's an error to call `Annotated` with less than two arguments.
- Access the metadata via the ``__metadata__`` attribute:: - Access the metadata via the ``__metadata__`` attribute::
Annotated[int, '$'].__metadata__ == ('$',) assert Annotated[int, '$'].__metadata__ == ('$',)
- Nested Annotated are flattened:: - Nested Annotated are flattened::
Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3] assert Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
- Instantiating an annotated type is equivalent to instantiating the - Instantiating an annotated type is equivalent to instantiating the
underlying type:: underlying type::
Annotated[C, Ann1](5) == C(5) assert Annotated[C, Ann1](5) == C(5)
- Annotated can be used as a generic type alias:: - Annotated can be used as a generic type alias::
Optimized = Annotated[T, runtime.Optimize()] Optimized = Annotated[T, runtime.Optimize()]
Optimized[int] == Annotated[int, runtime.Optimize()] assert Optimized[int] == Annotated[int, runtime.Optimize()]
OptimizedList = Annotated[List[T], runtime.Optimize()] OptimizedList = Annotated[List[T], runtime.Optimize()]
OptimizedList[int] == Annotated[List[int], runtime.Optimize()] assert OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
- Annotated cannot be used with an unpacked TypeVarTuple:: - Annotated cannot be used with an unpacked TypeVarTuple::
@ -2210,6 +2224,7 @@ def runtime_checkable(cls):
Raise TypeError if applied to a non-protocol class. Raise TypeError if applied to a non-protocol class.
This allows a simple-minded structural check very similar to This allows a simple-minded structural check very similar to
one trick ponies in collections.abc such as Iterable. one trick ponies in collections.abc such as Iterable.
For example:: For example::
@runtime_checkable @runtime_checkable
@ -2251,7 +2266,6 @@ def assert_type(val, typ, /):
def greet(name: str) -> None: def greet(name: str) -> None:
assert_type(name, str) # ok assert_type(name, str) # ok
assert_type(name, int) # type checker error assert_type(name, int) # type checker error
""" """
return val return val
@ -2292,7 +2306,6 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
- If two dict arguments are passed, they specify globals and - If two dict arguments are passed, they specify globals and
locals, respectively. locals, respectively.
""" """
if getattr(obj, '__no_type_check__', None): if getattr(obj, '__no_type_check__', None):
return {} return {}
# Classes require a special treatment. # Classes require a special treatment.
@ -2362,8 +2375,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
def _strip_annotations(t): def _strip_annotations(t):
"""Strips the annotations from a given type. """Strip the annotations from a given type."""
"""
if isinstance(t, _AnnotatedAlias): if isinstance(t, _AnnotatedAlias):
return _strip_annotations(t.__origin__) return _strip_annotations(t.__origin__)
if hasattr(t, "__origin__") and t.__origin__ in (Required, NotRequired): if hasattr(t, "__origin__") and t.__origin__ in (Required, NotRequired):
@ -2391,16 +2403,16 @@ def get_origin(tp):
"""Get the unsubscripted version of a type. """Get the unsubscripted version of a type.
This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
and Annotated. Return None for unsupported types. Examples:: Annotated, and others. Return None for unsupported types. Examples::
get_origin(Literal[42]) is Literal assert get_origin(Literal[42]) is Literal
get_origin(int) is None assert get_origin(int) is None
get_origin(ClassVar[int]) is ClassVar assert get_origin(ClassVar[int]) is ClassVar
get_origin(Generic) is Generic assert get_origin(Generic) is Generic
get_origin(Generic[T]) is Generic assert get_origin(Generic[T]) is Generic
get_origin(Union[T, int]) is Union assert get_origin(Union[T, int]) is Union
get_origin(List[Tuple[T, T]][int]) == list assert get_origin(List[Tuple[T, T]][int]) is list
get_origin(P.args) is P assert get_origin(P.args) is P
""" """
if isinstance(tp, _AnnotatedAlias): if isinstance(tp, _AnnotatedAlias):
return Annotated return Annotated
@ -2418,12 +2430,14 @@ def get_args(tp):
"""Get type arguments with all substitutions performed. """Get type arguments with all substitutions performed.
For unions, basic simplifications used by Union constructor are performed. For unions, basic simplifications used by Union constructor are performed.
Examples:: Examples::
get_args(Dict[str, int]) == (str, int)
get_args(int) == () assert get_args(Dict[str, int]) == (str, int)
get_args(Union[int, Union[T, int], str][int]) == (int, str) assert get_args(int) == ()
get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) assert get_args(Union[int, Union[T, int], str][int]) == (int, str)
get_args(Callable[[], T][int]) == ([], int) assert get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
assert get_args(Callable[[], T][int]) == ([], int)
""" """
if isinstance(tp, _AnnotatedAlias): if isinstance(tp, _AnnotatedAlias):
return (tp.__origin__,) + tp.__metadata__ return (tp.__origin__,) + tp.__metadata__
@ -2438,14 +2452,15 @@ def get_args(tp):
def is_typeddict(tp): def is_typeddict(tp):
"""Check if an annotation is a TypedDict class """Check if an annotation is a TypedDict class.
For example:: For example::
class Film(TypedDict): class Film(TypedDict):
title: str title: str
year: int year: int
is_typeddict(Film) # => True is_typeddict(Film) # => True
is_typeddict(Union[list, str]) # => False is_typeddict(Union[list, str]) # => False
""" """
return isinstance(tp, _TypedDictMeta) return isinstance(tp, _TypedDictMeta)
@ -2472,7 +2487,6 @@ def int_or_str(arg: int | str) -> None:
reachable, it will emit an error. reachable, it will emit an error.
At runtime, this throws an exception when called. At runtime, this throws an exception when called.
""" """
value = repr(arg) value = repr(arg)
if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH: if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH:
@ -2522,7 +2536,6 @@ def no_type_check_decorator(decorator):
This wraps the decorator with something that wraps the decorated This wraps the decorator with something that wraps the decorated
function in @no_type_check. function in @no_type_check.
""" """
@functools.wraps(decorator) @functools.wraps(decorator)
def wrapped_decorator(*args, **kwds): def wrapped_decorator(*args, **kwds):
func = decorator(*args, **kwds) func = decorator(*args, **kwds)
@ -2549,27 +2562,27 @@ def overload(func):
"""Decorator for overloaded functions/methods. """Decorator for overloaded functions/methods.
In a stub file, place two or more stub definitions for the same In a stub file, place two or more stub definitions for the same
function in a row, each decorated with @overload. For example: function in a row, each decorated with @overload. For example::
@overload @overload
def utf8(value: None) -> None: ... def utf8(value: None) -> None: ...
@overload @overload
def utf8(value: bytes) -> bytes: ... def utf8(value: bytes) -> bytes: ...
@overload @overload
def utf8(value: str) -> bytes: ... def utf8(value: str) -> bytes: ...
In a non-stub file (i.e. a regular .py file), do the same but In a non-stub file (i.e. a regular .py file), do the same but
follow it with an implementation. The implementation should *not* follow it with an implementation. The implementation should *not*
be decorated with @overload. For example: be decorated with @overload. For example::
@overload @overload
def utf8(value: None) -> None: ... def utf8(value: None) -> None: ...
@overload @overload
def utf8(value: bytes) -> bytes: ... def utf8(value: bytes) -> bytes: ...
@overload @overload
def utf8(value: str) -> bytes: ... def utf8(value: str) -> bytes: ...
def utf8(value): def utf8(value):
# implementation goes here ... # implementation goes here
The overloads for a function can be retrieved at runtime using the The overloads for a function can be retrieved at runtime using the
get_overloads() function. get_overloads() function.
@ -2602,29 +2615,30 @@ def clear_overloads():
def final(f): def final(f):
"""A decorator to indicate final methods and final classes. """Decorator to indicate final methods and final classes.
Use this decorator to indicate to type checkers that the decorated Use this decorator to indicate to type checkers that the decorated
method cannot be overridden, and decorated class cannot be subclassed. method cannot be overridden, and decorated class cannot be subclassed.
For example:
class Base: For example::
@final
def done(self) -> None: class Base:
... @final
class Sub(Base): def done(self) -> None:
def done(self) -> None: # Error reported by type checker ...
class Sub(Base):
def done(self) -> None: # Error reported by type checker
... ...
@final @final
class Leaf: class Leaf:
... ...
class Other(Leaf): # Error reported by type checker class Other(Leaf): # Error reported by type checker
... ...
There is no runtime checking of these properties. The decorator There is no runtime checking of these properties. The decorator
sets the ``__final__`` attribute to ``True`` on the decorated object attempts to set the ``__final__`` attribute to ``True`` on the decorated
to allow runtime introspection. object to allow runtime introspection.
""" """
try: try:
f.__final__ = True f.__final__ = True
@ -2669,13 +2683,15 @@ class Other(Leaf): # Error reported by type checker
Collection = _alias(collections.abc.Collection, 1) Collection = _alias(collections.abc.Collection, 1)
Callable = _CallableType(collections.abc.Callable, 2) Callable = _CallableType(collections.abc.Callable, 2)
Callable.__doc__ = \ Callable.__doc__ = \
"""Callable type; Callable[[int], str] is a function of (int) -> str. """Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function of (int) -> str.
The subscription syntax must always be used with exactly two The subscription syntax must always be used with exactly two
values: the argument list and the return type. The argument list values: the argument list and the return type.
must be a list of types or ellipsis; the return type must be a single type. The argument list must be a list of types, a ParamSpec or ellipsis.
The return type must be a single type.
There is no syntax to indicate optional or keyword arguments, There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types. such function types are rarely used as callback types.
""" """
AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet') AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
@ -2689,7 +2705,9 @@ class Other(Leaf): # Error reported by type checker
# Tuple accepts variable number of parameters. # Tuple accepts variable number of parameters.
Tuple = _TupleType(tuple, -1, inst=False, name='Tuple') Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Tuple.__doc__ = \ Tuple.__doc__ = \
"""Tuple type; Tuple[X, Y] is the cross-product type of X and Y. """Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple to type variables T1 and T2. Tuple[int, float, str] is a tuple
@ -2716,25 +2734,26 @@ class Other(Leaf): # Error reported by type checker
AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2) AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
Type = _alias(type, 1, inst=False, name='Type') Type = _alias(type, 1, inst=False, name='Type')
Type.__doc__ = \ Type.__doc__ = \
"""A special construct usable to annotate class objects. """Deprecated alias to builtins.type.
builtins.type or typing.Type can be used to annotate class objects.
For example, suppose we have the following classes:: For example, suppose we have the following classes::
class User: ... # Abstract base for User classes class User: ... # Abstract base for User classes
class BasicUser(User): ... class BasicUser(User): ...
class ProUser(User): ... class ProUser(User): ...
class TeamUser(User): ... class TeamUser(User): ...
And a function that takes a class argument that's a subclass of And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class:: User and returns an instance of the corresponding class::
U = TypeVar('U', bound=User) U = TypeVar('U', bound=User)
def new_user(user_class: Type[U]) -> U: def new_user(user_class: Type[U]) -> U:
user = user_class() user = user_class()
# (Here we could write the user object to a database) # (Here we could write the user object to a database)
return user return user
joe = new_user(BasicUser) joe = new_user(BasicUser)
At this point the type checker knows that joe has type BasicUser. At this point the type checker knows that joe has type BasicUser.
""" """
@ -2743,6 +2762,7 @@ def new_user(user_class: Type[U]) -> U:
@runtime_checkable @runtime_checkable
class SupportsInt(Protocol): class SupportsInt(Protocol):
"""An ABC with one abstract method __int__.""" """An ABC with one abstract method __int__."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2753,6 +2773,7 @@ def __int__(self) -> int:
@runtime_checkable @runtime_checkable
class SupportsFloat(Protocol): class SupportsFloat(Protocol):
"""An ABC with one abstract method __float__.""" """An ABC with one abstract method __float__."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2763,6 +2784,7 @@ def __float__(self) -> float:
@runtime_checkable @runtime_checkable
class SupportsComplex(Protocol): class SupportsComplex(Protocol):
"""An ABC with one abstract method __complex__.""" """An ABC with one abstract method __complex__."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2773,6 +2795,7 @@ def __complex__(self) -> complex:
@runtime_checkable @runtime_checkable
class SupportsBytes(Protocol): class SupportsBytes(Protocol):
"""An ABC with one abstract method __bytes__.""" """An ABC with one abstract method __bytes__."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2783,6 +2806,7 @@ def __bytes__(self) -> bytes:
@runtime_checkable @runtime_checkable
class SupportsIndex(Protocol): class SupportsIndex(Protocol):
"""An ABC with one abstract method __index__.""" """An ABC with one abstract method __index__."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2793,6 +2817,7 @@ def __index__(self) -> int:
@runtime_checkable @runtime_checkable
class SupportsAbs(Protocol[T_co]): class SupportsAbs(Protocol[T_co]):
"""An ABC with one abstract method __abs__ that is covariant in its return type.""" """An ABC with one abstract method __abs__ that is covariant in its return type."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2803,6 +2828,7 @@ def __abs__(self) -> T_co:
@runtime_checkable @runtime_checkable
class SupportsRound(Protocol[T_co]): class SupportsRound(Protocol[T_co]):
"""An ABC with one abstract method __round__ that is covariant in its return type.""" """An ABC with one abstract method __round__ that is covariant in its return type."""
__slots__ = () __slots__ = ()
@abstractmethod @abstractmethod
@ -2829,7 +2855,6 @@ def _make_nmtuple(name, types, module, defaults = ()):
class NamedTupleMeta(type): class NamedTupleMeta(type):
def __new__(cls, typename, bases, ns): def __new__(cls, typename, bases, ns):
assert _NamedTuple in bases assert _NamedTuple in bases
for base in bases: for base in bases:
@ -2881,10 +2906,9 @@ class Employee(NamedTuple):
The resulting class has an extra __annotations__ attribute, giving a The resulting class has an extra __annotations__ attribute, giving a
dict that maps field names to types. (The field names are also in dict that maps field names to types. (The field names are also in
the _fields attribute, which is part of the namedtuple API.) the _fields attribute, which is part of the namedtuple API.)
Alternative equivalent keyword syntax is also accepted:: An alternative equivalent functional syntax is also accepted::
Employee = NamedTuple('Employee', name=str, id=int)
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
""" """
if fields is None: if fields is None:
fields = kwargs.items() fields = kwargs.items()
@ -2904,7 +2928,7 @@ def _namedtuple_mro_entries(bases):
class _TypedDictMeta(type): class _TypedDictMeta(type):
def __new__(cls, name, bases, ns, total=True): def __new__(cls, name, bases, ns, total=True):
"""Create new typed dict class object. """Create a new typed dict class object.
This method is called when TypedDict is subclassed, This method is called when TypedDict is subclassed,
or when TypedDict is instantiated. This way or when TypedDict is instantiated. This way
@ -2975,10 +2999,11 @@ def __subclasscheck__(cls, other):
def TypedDict(typename, fields=None, /, *, total=True, **kwargs): def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
"""A simple typed namespace. At runtime it is equivalent to a plain dict. """A simple typed namespace. At runtime it is equivalent to a plain dict.
TypedDict creates a dictionary type that expects all of its TypedDict creates a dictionary type such that a type checker will expect all
instances to have a certain set of keys, where each key is instances to have a certain set of keys, where each key is
associated with a value of a consistent type. This expectation associated with a value of a consistent type. This expectation
is not checked at runtime but is only enforced by type checkers. is not checked at runtime.
Usage:: Usage::
class Point2D(TypedDict): class Point2D(TypedDict):
@ -2998,20 +3023,25 @@ class Point2D(TypedDict):
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
By default, all keys must be present in a TypedDict. It is possible By default, all keys must be present in a TypedDict. It is possible
to override this by specifying totality. to override this by specifying totality::
Usage::
class point2D(TypedDict, total=False): class Point2D(TypedDict, total=False):
x: int x: int
y: int y: int
This means that a point2D TypedDict can have any of the keys omitted.A type This means that a Point2D TypedDict can have any of the keys omitted. A type
checker is only expected to support a literal False or True as the value of checker is only expected to support a literal False or True as the value of
the total argument. True is the default, and makes all items defined in the the total argument. True is the default, and makes all items defined in the
class body be required. class body be required.
The class syntax is only supported in Python 3.6+, while the other The Required and NotRequired special forms can also be used to mark
syntax form works for Python 2.7 and 3.2+ individual keys as being required or not required::
class Point2D(TypedDict):
x: int # the "x" key must always be present (Required is the default)
y: NotRequired[int] # the "y" key can be omitted
See PEP 655 for more details on Required and NotRequired.
""" """
if fields is None: if fields is None:
fields = kwargs fields = kwargs
@ -3041,8 +3071,9 @@ class body be required.
@_SpecialForm @_SpecialForm
def Required(self, parameters): def Required(self, parameters):
"""A special typing construct to mark a key of a total=False TypedDict """Special typing construct to mark a TypedDict key as required.
as required. For example:
This is mainly useful for total=False TypedDicts. For example::
class Movie(TypedDict, total=False): class Movie(TypedDict, total=False):
title: Required[str] title: Required[str]
@ -3062,8 +3093,9 @@ class Movie(TypedDict, total=False):
@_SpecialForm @_SpecialForm
def NotRequired(self, parameters): def NotRequired(self, parameters):
"""A special typing construct to mark a key of a TypedDict as """Special typing construct to mark a TypedDict key as potentially missing.
potentially missing. For example:
For example::
class Movie(TypedDict): class Movie(TypedDict):
title: str title: str
@ -3079,8 +3111,9 @@ class Movie(TypedDict):
class NewType: class NewType:
"""NewType creates simple unique types with almost zero """NewType creates simple unique types with almost zero runtime overhead.
runtime overhead. NewType(name, tp) is considered a subtype of tp
NewType(name, tp) is considered a subtype of tp
by static type checkers. At runtime, NewType(name, tp) returns by static type checkers. At runtime, NewType(name, tp) returns
a dummy callable that simply returns its argument. Usage:: a dummy callable that simply returns its argument. Usage::
@ -3342,12 +3375,11 @@ def reveal_type(obj: T, /) -> T:
x: int = 1 x: int = 1
reveal_type(x) reveal_type(x)
Running a static type checker (e.g., ``mypy``) on this example Running a static type checker (e.g., mypy) on this example
will produce output similar to 'Revealed type is "builtins.int"'. will produce output similar to 'Revealed type is "builtins.int"'.
At runtime, the function prints the runtime type of the At runtime, the function prints the runtime type of the
argument and returns it unchanged. argument and returns it unchanged.
""" """
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr) print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
return obj return obj
@ -3361,10 +3393,11 @@ def dataclass_transform(
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (), field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (),
**kwargs: Any, **kwargs: Any,
) -> Callable[[T], T]: ) -> Callable[[T], T]:
"""Decorator that marks a function, class, or metaclass as providing """Decorator to mark an object as providing dataclass-like behaviour.
dataclass-like behavior.
Example usage with a decorator function: The decorator can be applied to a function, class, or metaclass.
Example usage with a decorator function::
T = TypeVar("T") T = TypeVar("T")
@ -3378,7 +3411,7 @@ class CustomerModel:
id: int id: int
name: str name: str
On a base class: On a base class::
@dataclass_transform() @dataclass_transform()
class ModelBase: ... class ModelBase: ...
@ -3387,7 +3420,7 @@ class CustomerModel(ModelBase):
id: int id: int
name: str name: str
On a metaclass: On a metaclass::
@dataclass_transform() @dataclass_transform()
class ModelMeta(type): ... class ModelMeta(type): ...