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 (Union)

  • Union

  • Python 3.10+

def my_exit(code: str | int):
    print(type(code).__name__)

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

$ mypy pipe.py
pipe.py:6: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)