Support for "| None" would be great to have.
+1.
https://github.com/python/peps/blob/master/pep-0604.rst
On Fri, Mar 13, 2020, 9:36 AM Chris Angelico rosuav@gmail.com wrote:
On Sat, Mar 14, 2020 at 12:24 AM Noah Peter May me@noahpmay.dev wrote:
Hello everyone,
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
expected.
Yep, agreed.
Have a look at PEP 604, which is looking at this same idea.
https://www.python.org/dev/peps/pep-0604/
I'm personally in favour :)
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/WQ2TI5... Code of Conduct: http://python.org/psf/codeofconduct/