Skip to main content

How to Allow Two Types in a Python Type Hint with |

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
def <function>(<parameter>: <type 1> | <type 2>) -> <return type>:
PartWhat 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 with uv init.

Step 1: Write the Script​

describe.py
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__)

The describe.py script in a macOS-style code window: describe takes value as int | str and returns a str; if value is an int it returns the number, otherwise the word; then prints for 42, for hello, and describe's __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​

Terminal
uv run python describe.py
Output
the number 42
the word hello
{'value': int | str, 'return': <class 'str'>}

The terminal after uv run python describe.py: the number 42, the word hello, and the hints, value int | str and return 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​

MistakeWhat happensDo this instead
Using something only one of the types hasA 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 strPython works out int or str as just int, so the stored hint was {'value': <class 'int'>, 'return': <class 'str'>}, with no str in itUse 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​