Python 3.10+Built-in typesterm
types.UnionType
annotation types
The type of an int | str union expression. Since Python 3.14 it is an alias of typing.Union, so both spellings produce the same kind of object.
Syntax
int | str # → объединение типов
Unlike list[int], a union can go into isinstance: isinstance(3, int | str) is true. Order and duplicates do not matter: int | str equals str | int, and int | int is just int. Before 3.14 types.UnionType and typing.Union were distinct, so code checking type(x) is types.UnionType missed typing.Union spellings; since 3.14 typing.get_origin(x) is typing.Union is the reliable check.
Examples
import types
import typing
u = int | str
print(isinstance(3, u), isinstance(3.0, u)) # → True False
print((int | str) == (str | int)) # → True
print(typing.get_args(u)) # → (<class 'int'>, <class 'str'>)
print(types.UnionType is typing.Union) # → ? True с Python 3.14, раньше — разные типы
See also
Open in the full glossarydocs.python.orgReport an inaccuracy