How to Allow Two Types in a Python Type Hint with |
Some functions take more than one kind of value: an ID that can be a number or a text, or a size given as a number or as a word. A type hint can say so by joining the types with |: int | str means "an integer or a string". This guide writes such a function on Python 3.15.2 and calls it with both.
Syntax
def <function>(<parameter>: <type 1> | <type 2>) -> <return type>:
| Part | What it means |
|---|---|
<type 1> | <type 2> | The value is either of <type 1> or of <type 2>. Add more with more bars: int | str | float |
-> <return type> | What the function returns, which can be a | of types too |
The built-in types reference defines it: "X | Y means either X or Y. It is equivalent to typing.Union[X, Y]."
The Example
A function describe takes a value that's either a number or a word, and describes it. It checks which one it got with isinstance, which says whether a value is of a given type.
Prerequisites
- uv and Python 3.10 or newer.
|between types works from Python 3.10. This guide uses uv 0.13.10 with Python 3.15.2, in a project made withuv init.
Step 1: Write the Script
def describe(value: int | str) -> str:
if isinstance(value, int):
return f"the number {value}"
return f"the word {value}"
print(describe(42))
print(describe("hello"))
print(describe.__annotations__)

value: int | str reads "value is an integer or a string". Because the value can be either, the function checks: isinstance(value, int) is True for 42, so that call returns the first text. Anything else must be a string, so it gets the second. __annotations__ is the dictionary where Python keeps a function's hints.
Step 2: Run It
uv run python describe.py
the number 42
the word hello
{'value': int | str, 'return': <class 'str'>}

Each value took its own branch, and the hint was stored as int | str, just as written. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Using something only one of the types has | A function with value: int | str that returned value.upper() printed HELLO for "hello", then stopped on 42 with AttributeError: 'int' object has no attribute 'upper' | Check the type with isinstance first, and handle each type |
Writing int or str | Python works out int or str as just int, so the stored hint was {'value': <class 'int'>, 'return': <class 'str'>}, with no str in it | Use int | str |
FAQ
Is Union[int, str] the same as int | str?
Yes. The documentation says X | Y "is equivalent to typing.Union[X, Y]". Union needs from typing import Union first; | needs no import.
Does Python reject a value of a third type?
No. Hints aren't checked when the program runs: describe(2.5) returned the word 2.5. A type checker reads the hints and reports such a call before the program runs.
Conclusion
int | str told readers and tools that describe takes a number or a word, and isinstance picked the right branch for each. Python stored the hint as written, and int or str isn't a shorter way to write it.
Additional Resources
- Union type — the
|operator between types. - isinstance — checking a value's type.
- typing — type hints in Python.
