Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pipe notation for more than two types in a type hint

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading