Python Glossary Русский

Python 3.5+typingterm

typing

module

Type hint tools: Any, Optional, TypedDict, Protocol, generics — for mypy, IDEs and human readers.

Syntax

import typing

typing states intent but does not enforce it: the interpreter does not check annotations; mypy, pyright and IDEs do. Since Python 3.9 the built-in list[int] and dict[str, int] replace List[int] and Dict[str, int], and since 3.10 X | None replaces Optional[X], so what remains in typing is mostly Any, Protocol, TypedDict, Literal and the like. Do not confuse it with types, which is about real objects at run time.

Examples

from typing import get_type_hints
def double(x: int) -> int:
    return x * 2
print(double("ab"))  # → abab — подсказки типов при запуске не проверяются
print(get_type_hints(double))  # → {'x': <class 'int'>, 'return': <class 'int'>}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy