Support for "| None" would be great to have.
On Fri, Mar 13, 2020, 9:36 AM Chris Angelico firstname.lastname@example.org wrote:
On Sat, Mar 14, 2020 at 12:24 AM Noah Peter May email@example.com wrote:
I'm not certain if this has been brought up before, but I felt like
bringing it to the table anyway.
As the title says, it would be convenient to represent a union type with
the or operator.
Yep! You're not the only one to want this.
The next is actually a question: do we use "bitwise or" or "boolean or"
(aka "|" vs "or")? I honestly don't know which would be preferable. The latter is more readable than the other but requires 3-4 characters (including whitespace) vs just 1-3 extra. Both are shorter than the 8-9 chars for the traditional union. I'll just use both in the examples for now.
The "or" operator is defined very thoroughly by the language and can't be used for this, so the decision is made for you already - the "|" operator is the only way to go.
In either case, we could override the or-dunder method to return the
"old" union representation of types
List | Tuple == Union[List, Tuple] (Union[List, Tuple] or Union[int, str]) == Union[List, Tuple, int, str]
This should allow any code relying on run-time annotations to work as
Have a look at PEP 604, which is looking at this same idea.
I'm personally in favour :)
ChrisA _______________________________________________ Python-ideas mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://firstname.lastname@example.org/message/WQ2TI5... Code of Conduct: http://python.org/psf/codeofconduct/