Pipe notation for more than two types in a type hint

Question:

I am trying:

def foo(x: int | float | str):
    pass
foo(0)

and get the error:

TypeError: unsupported operand type(s) for |: ‘type’ and ‘type’

Is it possible to use more than two types with pipe notation or I have to write Union?

EDIT It turns out that I have a version of python that does not support the pipe notation at all, even for two types…

Asked By: AlwaysLearning

||

Answers:

Syntactic sugar like this to represent union types wasn’t added until 3.10 with the introduction of PEP 604. Update to 3.10+ or use typing.Union.

Answered By: erip
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.