> Though I wonder how to represent `Union[bytes, str]?` using the | syntax.  `(bytes | str)?` or `bytes? | str?`

Good question! From the theoretical point of view, all your examples should be equal.
Since Union always maintains its structure.

Currently, it works correctly:

```
from typing import Optional, Union

x: Optional[Union[int, str]]
y: Union[Optional[int], Optional[str]]

reveal_type(x)  # Union[builtins.int, builtins.str, None]
reveal_type(y)  # Union[builtins.int, None, builtins.str, None]
```

However, mypy can possibly flatten repeating `None` values, but that's another topic.



пт, 4 сент. 2020 г. в 01:48, Gregory P. Smith <greg@krypto.org>:
Nice.  This ? makes sense to me, especially given the similar use in many other languages.

Though I wonder how to represent `Union[bytes, str]?` using the | syntax.  `(bytes | str)?` or `bytes? | str?`. That should be covered.

I believe Thomas Wouters (cc'd) has been mulling over ? as a token for a non conflicting non-typing purpose. Maybe this is the decade of the ?.

I'm not worried about being unable to use a new feature on older versions of the language. That is normal, there is always a long time frame before a syntax feature can see wide adoption.  (ex: yield, with, set literals, 1_000, async, ...)

-gps

On Thu, Sep 3, 2020, 3:19 PM Guido van Rossum <guido@python.org> wrote:
Wow, you've been busy! (I know I still owe you a review on the PEP 604 implementation. It's on my TODO list. :-)

My main observation at this point is that introducing a new token means that there is no way to use this in code that has to be backwards compatible with Python 3.9 and before (assuming you could get it into 3.10). This is different for PEP 604's `|` operator, which can be used in Python 3.7+ as long as the `from __future__ import annotations` magic import is used to avoid runtime errors on things like `int | str`. The `|` operator could also be used in typeshed, since it is valid Python 3.7 syntax. (While I don't know of any type checkers that currently support it yet, supporting it would not require changes to the Python *parser* -- at least typed_ast can handle it without changes.)

However, this shouldn't be a big strike against the proposal. In a few short years Python 3.10 will be the oldest version supported and all will be well.

Another concern I have is that we already have two ways to spell "optional": `Optional[T]` and `T | None`.

Finally, IMO the argument against `Optional[T]` is stronger than the argument against `T | None`. (But your point that 7% of annotations use Optional is well taken.)

--Guido



On Thu, Sep 3, 2020 at 12:04 PM None via Typing-sig <typing-sig@python.org> wrote:
Hi there,

Recently I have been working on an implementation for PEP 604 (https://www.python.org/dev/peps/pep-0604/). While working on this, I started reviewing and exploring what new Optional syntax would look like in the Python language.

I've written a rough draft of a PEP and wanted to present it to this email list before submitting a PR. I know this is a topic that has been discussed in many settings (Github issues, in person typing meetups, etc. )previously, and wanted to solicit feedback here first. Let me know if submitting a PR would be a better way to gauge sentiment and feedback on this proposal.

Draft Proposal (branch compare): https://github.com/python/peps/compare/master...MaggieMoss:optional-syntax-pep
Gist: https://gist.github.com/MaggieMoss/c848cb3a581979f445d075c15629c950

Thanks again,
Maggie
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: guido@python.org


--
--Guido van Rossum (python.org/~guido)
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: greg@krypto.org
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: n.a.sobolev@gmail.com