:mod:`typing` --- Support for type hints
Source code: :source:`Lib/typing.py`
Note
The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.
This module provides runtime support for type hints. The most fundamental support consists of the types :data:`Any`, :data:`Union`, :data:`Callable`, :class:`TypeVar`, and :class:`Generic`. For a full specification, please see PEP 484. For a simplified introduction to type hints, see PEP 483.
The function below takes and returns a string and is annotated as follows:
def greeting(name: str) -> str:
return 'Hello ' + name
In the function greeting
, the argument name
is expected to be of type
:class:`str` and the return type :class:`str`. Subtypes are accepted as
arguments.
New features are frequently added to the typing
module.
The typing_extensions package
provides backports of these new features to older versions of Python.
For a summary of deprecated features and a deprecation timeline, please see Deprecation Timeline of Major Features.
Relevant PEPs
Since the initial introduction of type hints in PEP 484 and PEP 483, a number of PEPs have modified and enhanced Python's framework for type annotations. These include:
-
- PEP 526: Syntax for Variable Annotations
- Introducing syntax for annotating variables outside of function definitions, and :data:`ClassVar`
-
- PEP 544: Protocols: Structural subtyping (static duck typing)
- Introducing :class:`Protocol` and the :func:`@runtime_checkable<runtime_checkable>` decorator
-
- PEP 585: Type Hinting Generics In Standard Collections
- Introducing :class:`types.GenericAlias` and the ability to use standard library classes as :ref:`generic types<types-genericalias>`
-
- PEP 586: Literal Types
- Introducing :data:`Literal`
-
- PEP 589: TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys
- Introducing :class:`TypedDict`
-
- PEP 591: Adding a final qualifier to typing
- Introducing :data:`Final` and the :func:`@final<final>` decorator
-
- PEP 593: Flexible function and variable annotations
- Introducing :data:`Annotated`
-
-
PEP 604: Allow writing union types as
X | Y
-
Introducing :data:`types.UnionType` and the ability to use
the binary-or operator
|
to signify a :ref:`union of types<types-union>`
-
PEP 604: Allow writing union types as
-
- PEP 612: Parameter Specification Variables
- Introducing :class:`ParamSpec` and :data:`Concatenate`
-
- PEP 613: Explicit Type Aliases
- Introducing :data:`TypeAlias`
-
- PEP 646: Variadic Generics
- Introducing :data:`TypeVarTuple`
-
- PEP 647: User-Defined Type Guards
- Introducing :data:`TypeGuard`
-
- PEP 655: Marking individual TypedDict items as required or potentially missing
- Introducing :data:`Required` and :data:`NotRequired`
-
- PEP 673: Self type
- Introducing :data:`Self`
-
- PEP 675: Arbitrary Literal String Type
- Introducing :data:`LiteralString`
-
- PEP 681: Data Class Transforms
- Introducing the :func:`@dataclass_transform<dataclass_transform>` decorator
Type aliases
A type alias is defined by assigning the type to the alias. In this example,
Vector
and list[float]
will be treated as interchangeable synonyms:
Vector = list[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
Type aliases are useful for simplifying complex type signatures. For example:
from collections.abc import Sequence
ConnectionOptions = dict[str, str]
Address = tuple[str, int]
Server = tuple[Address, ConnectionOptions]
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
...
# The static type checker will treat the previous type signature as
# being exactly equivalent to this one.
def broadcast_message(
message: str,
servers: Sequence[tuple[tuple[str, int], dict[str, str]]]) -> None:
...
Note that None
as a type hint is a special case and is replaced by
type(None)
.
NewType
Use the :class:`NewType` helper to create distinct types:
from typing import NewType
UserId = NewType('UserId', int)
some_id = UserId(524313)
The static type checker will treat the new type as if it were a subclass of the original type. This is useful in helping catch logical errors:
def get_user_name(user_id: UserId) -> str:
...
# passes type checking
user_a = get_user_name(UserId(42351))
# fails type checking; an int is not a UserId
user_b = get_user_name(-1)
You may still perform all int
operations on a variable of type UserId
,
but the result will always be of type int
. This lets you pass in a
UserId
wherever an int
might be expected, but will prevent you from
accidentally creating a UserId
in an invalid way:
# 'output' is of type 'int', not 'UserId'
output = UserId(23413) + UserId(54341)
Note that these checks are enforced only by the static type checker. At runtime,
the statement Derived = NewType('Derived', Base)
will make Derived
a
callable that immediately returns whatever parameter you pass it. That means
the expression Derived(some_value)
does not create a new class or introduce
much overhead beyond that of a regular function call.
More precisely, the expression some_value is Derived(some_value)
is always
true at runtime.
It is invalid to create a subtype of Derived
:
from typing import NewType
UserId = NewType('UserId', int)
# Fails at runtime and does not pass type checking
class AdminUserId(UserId): pass
However, it is possible to create a :class:`NewType` based on a 'derived' NewType
:
from typing import NewType
UserId = NewType('UserId', int)
ProUserId = NewType('ProUserId', UserId)
and typechecking for ProUserId
will work as expected.
See PEP 484 for more details.
Note
Recall that the use of a type alias declares two types to be equivalent to
one another. Doing Alias = Original
will make the static type checker
treat Alias
as being exactly equivalent to Original
in all cases.
This is useful when you want to simplify complex type signatures.
In contrast, NewType
declares one type to be a subtype of another.
Doing Derived = NewType('Derived', Original)
will make the static type
checker treat Derived
as a subclass of Original
, which means a
value of type Original
cannot be used in places where a value of type
Derived
is expected. This is useful when you want to prevent logic
errors with minimal runtime cost.
Callable
Frameworks expecting callback functions of specific signatures might be
type hinted using Callable[[Arg1Type, Arg2Type], ReturnType]
.
For example:
from collections.abc import Callable
def feeder(get_next_item: Callable[[], str]) -> None:
# Body
def async_query(on_success: Callable[[int], None],
on_error: Callable[[int, Exception], None]) -> None:
# Body
async def on_update(value: str) -> None:
# Body
callback: Callable[[str], Awaitable[None]] = on_update
It is possible to declare the return type of a callable without specifying
the call signature by substituting a literal ellipsis
for the list of arguments in the type hint: Callable[..., ReturnType]
.
Callables which take other callables as arguments may indicate that their
parameter types are dependent on each other using :class:`ParamSpec`.
Additionally, if that callable adds or removes arguments from other
callables, the :data:`Concatenate` operator may be used. They
take the form Callable[ParamSpecVariable, ReturnType]
and
Callable[Concatenate[Arg1Type, Arg2Type, ..., ParamSpecVariable], ReturnType]
respectively.
Generics
Since type information about objects kept in containers cannot be statically inferred in a generic way, abstract base classes have been extended to support subscription to denote expected types for container elements.
from collections.abc import Mapping, Sequence
def notify_by_email(employees: Sequence[Employee],
overrides: Mapping[str, str]) -> None: ...
Generics can be parameterized by using a factory available in typing called :class:`TypeVar`.
from collections.abc import Sequence
from typing import TypeVar
T = TypeVar('T') # Declare type variable
def first(l: Sequence[T]) -> T: # Generic function
return l[0]
User-defined generic types
A user-defined class can be defined as a generic class.
from typing import TypeVar, Generic
from logging import Logger
T = TypeVar('T')
class LoggedVar(Generic[T]):
def __init__(self, value: T, name: str, logger: Logger) -> None:
self.name = name
self.logger = logger
self.value = value
def set(self, new: T) -> None:
self.log('Set ' + repr(self.value))
self.value = new
def get(self) -> T:
self.log('Get ' + repr(self.value))
return self.value
def log(self, message: str) -> None:
self.logger.info('%s: %s', self.name, message)
Generic[T]
as a base class defines that the class LoggedVar
takes a
single type parameter T
. This also makes T
valid as a type within the
class body.
The :class:`Generic` base class defines :meth:`~object.__class_getitem__` so
that LoggedVar[T]
is valid as a type:
from collections.abc import Iterable
def zero_all_vars(vars: Iterable[LoggedVar[int]]) -> None:
for var in vars:
var.set(0)
A generic type can have any number of type variables. All varieties of :class:`TypeVar` are permissible as parameters for a generic type:
from typing import TypeVar, Generic, Sequence
T = TypeVar('T', contravariant=True)
B = TypeVar('B', bound=Sequence[bytes], covariant=True)
S = TypeVar('S', int, str)
class WeirdTrio(Generic[T, B, S]):
...
Each type variable argument to :class:`Generic` must be distinct. This is thus invalid:
from typing import TypeVar, Generic
...
T = TypeVar('T')
class Pair(Generic[T, T]): # INVALID
...
You can use multiple inheritance with :class:`Generic`:
from collections.abc import Sized
from typing import TypeVar, Generic
T = TypeVar('T')
class LinkedList(Sized, Generic[T]):
...
When inheriting from generic classes, some type variables could be fixed:
from collections.abc import Mapping
from typing import TypeVar
T = TypeVar('T')
class MyDict(Mapping[str, T]):
...
In this case MyDict
has a single parameter, T
.
Using a generic class without specifying type parameters assumes
:data:`Any` for each position. In the following example, MyIterable
is
not generic but implicitly inherits from Iterable[Any]
:
from collections.abc import Iterable
class MyIterable(Iterable): # Same as Iterable[Any]
User defined generic type aliases are also supported. Examples:
from collections.abc import Iterable
from typing import TypeVar
S = TypeVar('S')
Response = Iterable[S] | int
# Return type here is same as Iterable[str] | int
def response(query: str) -> Response[str]:
...
T = TypeVar('T', int, float, complex)
Vec = Iterable[tuple[T, T]]
def inproduct(v: Vec[T]) -> T: # Same as Iterable[tuple[T, T]]
return sum(x*y for x, y in v)
User-defined generics for parameter expressions are also supported via parameter
specification variables in the form Generic[P]
. The behavior is consistent
with type variables' described above as parameter specification variables are
treated by the typing module as a specialized type variable. The one exception
to this is that a list of types can be used to substitute a :class:`ParamSpec`:
>>> from typing import Generic, ParamSpec, TypeVar
>>> T = TypeVar('T')
>>> P = ParamSpec('P')
>>> class Z(Generic[T, P]): ...
...
>>> Z[int, [dict, float]]
__main__.Z[int, (<class 'dict'>, <class 'float'>)]
Furthermore, a generic with only one parameter specification variable will accept
parameter lists in the forms X[[Type1, Type2, ...]]
and also
X[Type1, Type2, ...]
for aesthetic reasons. Internally, the latter is converted
to the former, so the following are equivalent:
>>> class X(Generic[P]): ...
...
>>> X[int, str]
__main__.X[(<class 'int'>, <class 'str'>)]
>>> X[[int, str]]
__main__.X[(<class 'int'>, <class 'str'>)]
Do note that generics with :class:`ParamSpec` may not have correct
__parameters__
after substitution in some cases because they
are intended primarily for static type checking.
A user-defined generic class can have ABCs as base classes without a metaclass conflict. Generic metaclasses are not supported. The outcome of parameterizing generics is cached, and most types in the typing module are hashable and comparable for equality.
The :data:`Any` type
A special kind of type is :data:`Any`. A static type checker will treat every type as being compatible with :data:`Any` and :data:`Any` as being compatible with every type.
This means that it is possible to perform any operation or method call on a value of type :data:`Any` and assign it to any variable:
from typing import Any
a: Any = None
a = [] # OK
a = 2 # OK
s: str = ''
s = a # OK
def foo(item: Any) -> int:
# Passes type checking; 'item' could be any type,
# and that type might have a 'bar' method
item.bar()
...
Notice that no type checking is performed when assigning a value of type
:data:`Any` to a more precise type. For example, the static type checker did
not report an error when assigning a
to s
even though s
was
declared to be of type :class:`str` and receives an :class:`int` value at
runtime!
Furthermore, all functions without a return type or parameter types will implicitly default to using :data:`Any`:
def legacy_parser(text):
...
return data
# A static type checker will treat the above
# as having the same signature as:
def legacy_parser(text: Any) -> Any:
...
return data
This behavior allows :data:`Any` to be used as an escape hatch when you need to mix dynamically and statically typed code.
Contrast the behavior of :data:`Any` with the behavior of :class:`object`. Similar to :data:`Any`, every type is a subtype of :class:`object`. However, unlike :data:`Any`, the reverse is not true: :class:`object` is not a subtype of every other type.
That means when the type of a value is :class:`object`, a type checker will reject almost all operations on it, and assigning it to a variable (or using it as a return value) of a more specialized type is a type error. For example:
def hash_a(item: object) -> int:
# Fails type checking; an object does not have a 'magic' method.
item.magic()
...
def hash_b(item: Any) -> int:
# Passes type checking
item.magic()
...
# Passes type checking, since ints and strs are subclasses of object
hash_a(42)
hash_a("foo")
# Passes type checking, since Any is compatible with all types
hash_b(42)
hash_b("foo")
Use :class:`object` to indicate that a value could be any type in a typesafe manner. Use :data:`Any` to indicate that a value is dynamically typed.
Nominal vs structural subtyping
Initially PEP 484 defined the Python static type system as using
nominal subtyping. This means that a class A
is allowed where
a class B
is expected if and only if A
is a subclass of B
.
This requirement previously also applied to abstract base classes, such as :class:`~collections.abc.Iterable`. The problem with this approach is that a class had to be explicitly marked to support them, which is unpythonic and unlike what one would normally do in idiomatic dynamically typed Python code. For example, this conforms to PEP 484:
from collections.abc import Sized, Iterable, Iterator
class Bucket(Sized, Iterable[int]):
...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[int]: ...
PEP 544 allows to solve this problem by allowing users to write
the above code without explicit base classes in the class definition,
allowing Bucket
to be implicitly considered a subtype of both Sized
and Iterable[int]
by static type checkers. This is known as
structural subtyping (or static duck-typing):
from collections.abc import Iterator, Iterable
class Bucket: # Note: no base classes
...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[int]: ...
def collect(items: Iterable[int]) -> int: ...
result = collect(Bucket()) # Passes type check
Moreover, by subclassing a special class :class:`Protocol`, a user can define new custom protocols to fully enjoy structural subtyping (see examples below).
Module contents
The module defines the following classes, functions and decorators.
Note
This module defines several types that are subclasses of pre-existing
standard library classes which also extend :class:`Generic`
to support type variables inside []
.
These types became redundant in Python 3.9 when the
corresponding pre-existing classes were enhanced to support []
.
The redundant types are deprecated as of Python 3.9 but no deprecation warnings will be issued by the interpreter. It is expected that type checkers will flag the deprecated types when the checked program targets Python 3.9 or newer.
The deprecated types will be removed from the :mod:`typing` module in the first Python version released 5 years after the release of Python 3.9.0. See details in PEP 585—Type Hinting Generics In Standard Collections.
Special typing primitives
Special types
These can be used as types in annotations and do not support []
.
Special forms
These can be used as types in annotations using []
, each having a unique syntax.
A variable annotated with C
may accept a value of type C
. In
contrast, a variable annotated with Type[C]
may accept values that are
classes themselves -- specifically, it will accept the class object of
C
. For example:
a = 3 # Has type 'int'
b = int # Has type 'Type[int]'
c = type(a) # Also has type 'Type[int]'
Note that Type[C]
is covariant:
class User: ...
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...
# Accepts User, BasicUser, ProUser, TeamUser, ...
def make_new_user(user_class: Type[User]) -> User:
# ...
return user_class()
The fact that Type[C]
is covariant implies that all subclasses of
C
should implement the same constructor signature and class method
signatures as C
. The type checker should flag violations of this,
but should also allow constructor calls in subclasses that match the
constructor calls in the indicated base class. How the type checker is
required to handle this particular case may change in future revisions of
PEP 484.
The only legal parameters for :class:`Type` are classes, :data:`Any`, :ref:`type variables <generics>`, and unions of any of these types. For example:
def new_non_team_user(user_class: Type[BasicUser | ProUser]): ...
Type[Any]
is equivalent to Type
which in turn is equivalent
to type
, which is the root of Python's metaclass hierarchy.
Building generic types
These are not used in annotations. They are building blocks for creating generic types.
Abstract base class for generic types.
A generic type is typically declared by inheriting from an instantiation of this class 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:
X = TypeVar('X')
Y = TypeVar('Y')
def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y:
try:
return mapping[key]
except KeyError:
return default
Type variable.
Usage:
T = TypeVar('T') # Can be anything
S = TypeVar('S', bound=str) # Can be any subtype of str
A = TypeVar('A', str, bytes) # Must be exactly 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) -> Sequence[T]:
"""Return a list containing n references to x."""
return [x]*n
def print_capitalized(x: S) -> S:
"""Print x capitalized, and return x."""
print(x.capitalize())
return x
def concatenate(x: A, y: A) -> A:
"""Add two strings or bytes objects together."""
return x + y
Note that type variables can be bound, constrained, or neither, but cannot be both bound and constrained.
Bound type variables and constrained type variables have different
semantics in several important ways. Using a bound type variable means
that the TypeVar
will be solved using the most specific type possible:
x = print_capitalized('a string')
reveal_type(x) # revealed type is str
class StringSubclass(str):
pass
y = print_capitalized(StringSubclass('another string'))
reveal_type(y) # revealed type is StringSubclass
z = print_capitalized(45) # error: int is not a subtype of str
Type variables can be bound to concrete types, abstract types (ABCs or protocols), and even unions of types:
U = TypeVar('U', bound=str|bytes) # Can be any subtype of the union str|bytes
V = TypeVar('V', bound=SupportsAbs) # Can be anything with an __abs__ method
Using a constrained type variable, however, means that the TypeVar
can only ever be solved as being exactly one of the constraints given:
a = concatenate('one', 'two')
reveal_type(a) # revealed type is str
b = concatenate(StringSubclass('one'), StringSubclass('two'))
reveal_type(b) # revealed type is str, despite StringSubclass being passed in
c = concatenate('one', b'two') # error: type variable 'A' can be either str or bytes in a function call, but not both
At runtime, isinstance(x, T)
will raise :exc:`TypeError`. In general,
:func:`isinstance` and :func:`issubclass` should not be used with types.
Type variables may be marked covariant or contravariant by passing
covariant=True
or contravariant=True
. See PEP 484 for more
details. By default, type variables are invariant.
Type variable tuple. A specialized form of :class:`type variable <TypeVar>` that enables variadic generics.
A normal type variable enables parameterization with a single type. A type variable tuple, in contrast, allows parameterization with an arbitrary number of types by acting like an arbitrary number of type variables wrapped in a tuple. For example:
T = TypeVar('T')
Ts = TypeVarTuple('Ts')
def move_first_element_to_last(tup: tuple[T, *Ts]) -> tuple[*Ts, T]:
return (*tup[1:], tup[0])
# T is bound to int, Ts is bound to ()
# Return value is (1,), which has type tuple[int]
move_first_element_to_last(tup=(1,))
# T is bound to int, Ts is bound to (str,)
# Return value is ('spam', 1), which has type tuple[str, int]
move_first_element_to_last(tup=(1, 'spam'))
# T is bound to int, Ts is bound to (str, float)
# Return value is ('spam', 3.0, 1), which has type tuple[str, float, int]
move_first_element_to_last(tup=(1, 'spam', 3.0))
# This fails to type check (and fails at runtime)
# because tuple[()] is not compatible with tuple[T, *Ts]
# (at least one element is required)
move_first_element_to_last(tup=())
Note the use of the unpacking operator *
in tuple[T, *Ts]
.
Conceptually, you can think of Ts
as a tuple of type variables
(T1, T2, ...)
. tuple[T, *Ts]
would then become
tuple[T, *(T1, T2, ...)]
, which is equivalent to
tuple[T, T1, T2, ...]
. (Note that in older versions of Python, you might
see this written using :data:`Unpack <Unpack>` instead, as
Unpack[Ts]
.)
Type variable tuples must always be unpacked. This helps distinguish type variable types from normal type variables:
x: Ts # Not valid
x: tuple[Ts] # Not valid
x: tuple[*Ts] # The correct way to to do it
Type variable tuples can be used in the same contexts as normal type variables. For example, in class definitions, arguments, and return types:
Shape = TypeVarTuple('Shape')
class Array(Generic[*Shape]):
def __getitem__(self, key: tuple[*Shape]) -> float: ...
def __abs__(self) -> Array[*Shape]: ...
def get_shape(self) -> tuple[*Shape]: ...
Type variable tuples can be happily combined with normal type variables:
DType = TypeVar('DType')
class Array(Generic[DType, *Shape]): # This is fine
pass
class Array2(Generic[*Shape, DType]): # This would also be fine
pass
float_array_1d: Array[float, Height] = Array() # Totally fine
int_array_2d: Array[int, Height, Width] = Array() # Yup, fine too
However, note that at most one type variable tuple may appear in a single list of type arguments or type parameters:
x: tuple[*Ts, *Ts] # Not valid
class Array(Generic[*Shape, *Shape]): # Not valid
pass
Finally, an unpacked type variable tuple can be used as the type annotation
of *args
:
def call_soon(
callback: Callable[[*Ts], None],
*args: *Ts
) -> None:
...
callback(*args)
In contrast to non-unpacked annotations of *args
- e.g. *args: int
,
which would specify that all arguments are int
- *args: *Ts
enables reference to the types of the individual arguments in *args
.
Here, this allows us to ensure the types of the *args
passed
to call_soon
match the types of the (positional) arguments of
callback
.
See PEP 646 for more details on type variable tuples.
Base class for protocol classes. Protocol classes are defined like this:
class Proto(Protocol):
def meth(self) -> int:
...
Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:
class C:
def meth(self) -> int:
return 0
def func(x: Proto) -> int:
return x.meth()
func(C()) # Passes static type check