Type Hint
An annotation that specifies the expected type for:
- variable or class attribute (New in Python 3.6, See PEP 526)
- function/method parameter and return type (See PEP 3107)
Use Case
- static type analysis tools
- aid IDEs with code completion and refactoring
Typeshed Stub
pip install mypy
pip install types-xxx
See typeshed
on GitHub
and mypy
on GitHub.
Backward Compability
pip install typing-extensions
See typing-extensions
package on PyPI.
typing.get_type_hints()
Type hints of global variables, class attributes, and functions, but not local variables,
can be accessed using typing.get_type_hints()
.
typing.get_type_hints(obj, globalns=None, localns=None, include_extras=False) -> dict
Return a dictionary containing type hints for a function, method, module or class object.
This is often the same as obj.__annotations__
.
In addition, forward references encoded as string literals
are handled by evaluating them in globalns
and localns
namespaces.