typing — Support for type hints¶
New in version 3.5.
Source code: Lib/typing.py
Note
The typing module has been included in the standard library on a provisional basis. New features might be added and API may change even between minor releases if deemed necessary by the core developers.
This module supports type hints as specified by PEP 484 and PEP 526.:
from typing import List typing import Dict, Tuple, function:.
Callable¶
Frameworks expecting callback functions of specific signatures might be
type hinted using
Callable[[Arg1Type, Arg2Type], ReturnType].
For example:
from typing]., uses a metaclass that defines
__getitem__() so that
LoggedVar[t] is valid as a type:
from typing)
The metaclass used by
Generic is a subclass of
abc.ABCMeta.
A generic class can be an ABC by including abstract methods or properties,
and generic classes can also on.
Classes, functions, and decorators¶
The module defines the following classes, functions and decorators:
- class Generic for.
New in
- class
typing.
AsyncIterable(Generic[T_co])¶
A generic version of
collections.abc.AsyncIterable.
- class
typing.
AsyncIterator(AsyncIterable[T_co])¶
A generic version of
collections.abc.AsyncIterator.
- class
typing.
ContextManager(Generic[T_co])¶
A generic version of
contextlib.AbstractContextManager.
New in version 3.6.
- class
typing.
AsyncContextManager(Generic[T_co])¶
A generic version of
contextlib.AbstractAsyncContextManager.
New in version 3.6.
-]: ...
-.6.1.
- class
typing.
ChainMap(collections.ChainMap, MutableMapping[KT, VT])¶
A generic version of
collections.ChainMap.
New in version 3.6.1.
-.5.4.
-.
-].
-: Deprecated the
_field_typesattribute in favor of the more standard
__annotations__attribute which has the same information.
Changed in version 3.8: The
_field_typesand
__annotations__attributes are now regular dictionaries instead of instances of
OrderedDict.
typing.
NewType(typ)¶
A helper function to indicate a distinct types to a typechecker, see NewType. At runtime it returns a function that returns its argument. Usage:
UserId = NewType('UserId', int) first_user = UserId(1)
New in')
New in version 3.5.4.
typing.
Union¶
Union type;
Union[X, Y]means either X or Y.
To define a union, use e.g.
Union]
When comparing unions, the argument order is ignored, e.g.:
Union[int, str] == Union[str, int]: ...
typing.
Tuple¶
Tuple type;
Tuple[X, Y]is the type of a tuple of two items with the first item of type X and the second of type Y..