A union type lets a function parameter accept one of several permitted data types. In modern Python type hints, separate alternatives with the vertical bar (|), such as int | float | str. section 6.3.3
Accepting a list or tuple
A statistics function can accept either a list of floating-point values or a tuple containing multiple floating-point values:\n\ndef generate_stats(measures: list[float] | tuple[float, ...]) -> tuple[float, float]:\n measure_mean = mean(measures)\n measure_std = stdev(measures)\n return measure_mean, measure_std\n\nThe union documents both supported forms while the return annotation describes the resulting pair. section 6.3.3
Use the same notation in several common situations:
- Place a union inside a container annotation, such as set[int | str], when the container may hold either type. section 6.3.5
- Combine complete container types, such as list[int] | list[str], when the function accepts either a list of integers or a list of strings. section 6.3.5
Precise union annotations help IDEs identify incompatible arguments during development. The syntax belongs to evolving features in recent Python versions, so availability depends on the Python version in use. section 6.3.3









