The “|” operator is another way of doing Union[Literal[“John], None] which is already supported in the new versions of Python. Correct me if I am wrong please. Maybe the types already use __or__ and __ror__ to sugar coat Union? Maybe if integers or strings or any other literal occurs after “:”, we could treat them slightly differently than what their actual __or__ and __ror__ would normally do? 

On 5 Feb 2022, at 11:42 PM, David Mertz, Ph.D. <david.mertz@gmail.com> wrote:

To do this EVERY object that you might list as alternatives would have to support `__or__()` and `__ror__()`.

Moreover, for many types, `|` is defined with conflicting meaning.  E.g. `3 | 5 | 11 == 15`. But of course 3, 5, 11 aren't the unique collection of numbers that bitwise or to 15. At runtime, we'd have no idea what the alternatives so expressed were.

On Sat, Feb 5, 2022, 2:24 PM Abdulla Al Kathiri <alkathiri.abdulla@gmail.com> wrote:
Hello all,

Why can’t we use the literals directly as types? For example,

x: Literal[1, 2, 3] = 3
name: Literal[“John”] | None = “John"

Become ….

x: 1 | 2 | 3 = 3
name: “John” | None = “John"


def open(file: Path | str, mode: “w” | “a” = “w”): …

Best Regards,

Abdulla
_______________________________________________
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/IJ74AQRHCIIICNWYYBQTSQD2BQASRSBL/
Code of Conduct: http://python.org/psf/codeofconduct/