Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or str, or in the other example str or NoneType).

Python deals in objects only, so there is never a need to even consider 'union types'. Python functions return what they return, if the programmer wants to return different types for different results then that's their choice. The choice is then an architecture choice, and makes no difference to the Python interpreter (so there is nothing to 'benchmark' here).

Python 3.5 does introduce a standard for creating optional type hints, and that standard includes Union[...] and Optional[...] annotations. Type hinting adds optional static type checking outside of the runtime, the same way types in TypeScript are not part of the JavaScript runtime.

Answer from Martijn Pieters on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
These can be used as types in annotations. They all support subscription using [], but each has a unique syntax. ... Union type; Union[X, Y] is equivalent to X | Y and means either X or Y.
Discussions

Why does `typing.Union` (`types.UnionType`) not implement the `collections.abc.Collection` (or `Set`) Protocol?
It seems extremely natural that the union type should support __iter__, __len__ and __contains__, what’s the design rationale behind not supporting these operations? More on discuss.python.org
🌐 discuss.python.org
19
2
September 7, 2023
Union[str, None] vs Optional[str]

The "modern" way to write this is str | None. Traditionally, I used Optional[str], mostly because Optional existed specifically for this purpose.

More on reddit.com
🌐 r/Python
25
7
June 15, 2022
🌐
Medium
medium.com › @apps.merkurev › union-type-expression-in-python-50a1b7d012cd
Union Type Expression in Python. Another useful use of the | operator in… | by AM | Medium
September 7, 2023 - x = 42 print(isinstance(x, int | str)) # output: True print(issubclass(type(x), int | str)) # output: True · This example is equivalent to the previous one, but it looks cleaner. Instead of (X, Y), we write X | Y using the | (bitwise or) operator. Union type expression is particularly expressive when used in type annotations.
🌐
Python
peps.python.org › pep-0604
PEP 604 – Allow writing union types as X | Y | peps.python.org
annotation: name_type name_type: NAME (args)? args: '[' paramslist ']' paramslist: annotation (',' annotation)* [','] To describe a disjunction (union type), the user must use Union[X, Y].
🌐
mypy
mypy.readthedocs.io › en › latest › kinds_of_types.html
Kinds of types - mypy 2.5.0+dev.73829857fe6c66c2a809fa8ec9c11dbd281194b1 documentation
Instead, you can use the legacy Union[T1, ..., Tn] type constructor. Example: from typing import Union def f(x: Union[int, str]) -> None: ... It is also possible to use the new syntax with versions of Python where it isn’t supported by the runtime with some limitations, if you use from __future__ import annotations (see Annotation issues at runtime):
🌐
Python.org
discuss.python.org › python help
Why does `typing.Union` (`types.UnionType`) not implement the `collections.abc.Collection` (or `Set`) Protocol? - Python Help - Discussions on Python.org
September 7, 2023 - It seems extremely natural that the union type should support __iter__, __len__ and __contains__, what’s the design rationale behind not supporting these operations?
🌐
NAO IT Systems LLC.
digibeatrix.com › home › functions & classes › python union types: guide to new & old syntax with examples
Python Union Types: Guide to New & Old Syntax with Examples - Practical Python Programming
November 29, 2025 - Union types are a feature in Python used to specify multiple different data types as a single type hint. This allows code to maintain both flexibility and type safety even when arguments or return values can be of multiple types.
Find elsewhere
🌐
CodersLegacy
coderslegacy.com › home › python › python union in typing – specify multiple types
Python Union in Typing - Specify Multiple Types - CodersLegacy
February 25, 2022 - Amongst the features introduced in the Python Typing library, was Union, which can be used to specify multiple possible types for a variable.
🌐
Instructor
python.useinstructor.com › concepts › unions
Union Types in Instructor - Instructor
You can use Union types to write "agents" that dynamically choose actions by selecting an output class.
🌐
Medium
medium.com › @wrefordmessi › advanced-python-typing-union-and-intersection-in-type-hints-45e7090e2a76
Advanced Python (Typing: Union and Intersection in Type Hints) | by Andrew Wreford Eshakz | Medium
April 6, 2023 - The Union and Intersection are special types provided by the Python Typing Module. They allow you to represent complex relationships between types in your type hints.
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
To define it you use the vertical bar (|) to separate both types. This is called a "union", because the variable can be anything in the union of those two sets of types.
🌐
Boot.dev
boot.dev › lessons › 84c21a30-d12a-4ea7-b764-8929461618e8
Learn Functional Programming in Python: Union Types | Boot.dev
Crucially, the | ("or") operator ... don't need to belong to a shared class hierarchy. A union type can list any number of possible types for a given value....
🌐
Reddit
reddit.com › r/python › union[str, none] vs optional[str]
r/Python on Reddit: Union[str, None] vs Optional[str]
June 15, 2022 -

Recently, one of my coworkers and I had a long debate on using Union[str, None] vs Optional[str]. I wrote a function that was supposed to return either a string or None. Based on the returned value my program was supposed to trigger some action. In my opinion, since None was a deciding factor it would be a better choice to use Union[str, None] for the sake of clarity. However, according to his opinion, Optional is a better choice in terms of simplicity/cleaner look. We both understand clearly that Optional is nothing but a shorthand of Union[..., None]. After a few searches, we figured people use both and both teams have their logic. In fact, the creator of Fast API, u/tiangolo also supports using Union (According to one of his recent tweets). My question is, what do you use and why?

🌐
Pydantic
docs.pydantic.dev › 2.0 › usage › types › unions
Types/unions | Pydantic Docs
The Union type allows a model attribute to accept different types, e.g.:
🌐
Mouse Vs Python
blog.pythonlibrary.org › home › python 3.10 – simplifies unions in type annotations
Python 3.10 - Simplifies Unions in Type Annotations - Mouse Vs Python
September 11, 2021 - In Python 3.10, you no longer need to import Union at all. All the details are in PEP 604. The PEP allows you to replace it entirely with the | operator. Once you have done that, the code above becomes the following: def square(number: int | float) -> int | float: return number ** 2 · You can use the new syntax to replace Optional as well, which is what you used for type hinting that an argument could be None:
🌐
mypy
mypy.readthedocs.io › en › latest › kinds_of_types.html
Kinds of types - mypy 2.5.0+dev.1ca7337fefd5d3b427bbecc093d6e4b34bf2501e documentation
Instead, you can use the legacy Union[T1, ..., Tn] type constructor. Example: from typing import Union def f(x: Union[int, str]) -> None: ... It is also possible to use the new syntax with versions of Python where it isn’t supported by the runtime with some limitations, if you use from __future__ import annotations (see Annotation issues at runtime):
🌐
Graphene-python
docs.graphene-python.org › en › latest › types › unions
Graphene-Python
Each Union is a Python class that inherits from graphene.Union.
🌐
Playful Python
playfulpython.com › type-hinting-union-and-sum-types
Type Hinting: Union and Sum types
April 20, 2023 - Now, it can be quite irritating writing Union[Book, VideoGame] over and over again whenever we use a variable to represent an item. To solve this problem, Python allows us to create type aliases.
🌐
CodeRivers
coderivers.org › blog › python-union-type
Python Union Type: A Comprehensive Guide - CodeRivers
February 22, 2026 - In Python, the type system has evolved over the years to provide more clarity and expressiveness. One such addition is the `Union` type, which allows you to specify that a variable or function argument can have multiple types. This can be incredibly useful in scenarios where you want to be ...
🌐
Stefsmeets
stefsmeets.nl › posts › type-union
Type union operator in Python 3.7+
January 21, 2022 - I'm a big fan of Python type hints. I try to use them whenever I can. Not only for static type checking with mypy. They also help document functions (sphinx has nice support for them) and force me to think about how I structure my functions. One thing that bothers me, is that type hints can get quite verbose. Especially if you have multiple possible input types or optional values. For example, take this function that takes an int or a float as input: # type_union.py from typing import Union def square(a : Union[int, float]): return a**2