Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

typing.Union

Represent values that belong to at least one member type.

Version: Python 3.14; repository image, production runtime not attested

Syntax

Union[int, str]

Represent values that belong to at least one member type.

In Python 3.14 typing.Union and types.UnionType denote the same class, and the infix spelling produces an equivalent union. Duplicates are removed and order does not affect equality.

An isinstance test may short-circuit before a parameterized generic, but reaching list[int] there raises TypeError.

Examples

import types
from typing import Union
u = Union[int, str, int]
print(Union is types.UnionType, u == (str | int))
mixed = int | list[int]
print(isinstance(2, mixed))
try:
    isinstance([], mixed)
except TypeError:
    print("parameterized member")

Result

True True
True
parameterized member

An isinstance test may short-circuit before a parameterized generic, but reaching list[int] there raises TypeError.

Sources