Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Either this or that type for Python before 3.10

  • Union
from typing import Union

def my_exit(code: Union[str, int]):
    print(type(code).__name__)

my_exit(3)
my_exit("problem")
my_exit(3.14)
$ python union.py
int
str
float

$ mypy union.py
union.py:8:9: error: Argument 1 to "my_exit" has incompatible type "float"; expected "str | int"  [arg-type]
Found 1 error in 1 file (checked 1 source file)