bpo-44957: Promote PEP 604 syntax in typing docs#27833
Conversation
* Use "X | Y" instead of "Union" where it makes sense. * Mention that "X | Y" is equivalent to "Union[X, Y]" in Union section. * Remove "Optional[X]" as shorthand for "Union[X, None]" as the new shorthand is now "X | None". * Mention that "Optional[X]" can be written as "X | None" in section about "Optional".
|
This should be backported to Python 3.10 (where PEP 604 was implemented), but not further. |
Fidget-Spinner
left a comment
There was a problem hiding this comment.
LGTM. Thanks Sebastian!
PS, I feel that the news item is optional, so if you don't want to include it, I'll be happy to apply skip news. However you're free to leave it in too.
| * Redundant arguments are skipped, e.g.:: | ||
|
|
||
| Union[int, str, int] == Union[int, str] | ||
| Union[int, str, int] == Union[int, str] == int | str |
There was a problem hiding this comment.
Not a critique and slightly off topic: this threw me off a little bit, because in earlier versions of 3.10 this was false because they are different types at runtime. Only since rc1 this is true.
There was a problem hiding this comment.
Interesting, I didn't know that.
There was a problem hiding this comment.
Sorry for the misinformation, but I was wrong 😉 . I think my memory is getting hazy, it was hash(int | str) that wasn't equivalent to hash(Union[int, str]) up till rc1. Equality was there from day 1.
| * You cannot subclass or instantiate a union. | ||
| * You cannot subclass or instantiate a ``Union``. | ||
|
|
||
| * You cannot write ``Union[X][Y]``. |
There was a problem hiding this comment.
Oh I just noticed this and maybe this can be another PR (since it needs to be backported to 3.9) but isn't this inaccurate if X is a TypeVar?
There was a problem hiding this comment.
At least the sentence doesn't sound super relevant to me. All examples show how to use Union, so would someone really want to write Union[X][Y]? Maybe this is some historic discussion.
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
I added it, since this is a bit more than just a fix, but I have no strong opinions either way. |
Fidget-Spinner
left a comment
There was a problem hiding this comment.
Alright. I just have one more minor point left for discussion.
| To define a union, use e.g. ``Union[int, str]``. Details: | ||
| To define a union, use e.g. ``Union[int, str]`` or the shorthand ``int | str``. Details: | ||
|
|
||
| * The arguments must be types and there must be at least one. |
There was a problem hiding this comment.
Out of curiosity, why did you change the redundant arguments are skipped bullet point, but not the others? I can see that Unions of a single argument vanish may not apply directly to the new syntax, but the others do right?
If we manage to merge them, then perhaps we can shorten the contents of the union_object == other section here https://docs.python.org/3.10/library/stdtypes.html#types-union and just link to the typing version. That will save us from some repetition.
There was a problem hiding this comment.
I went a bit back and forth over this. In the end I decided that most of them apply mostly to Union:
- Unions of unions don't really exist for PEP 604 unions, except when counting
(X | Y) | Z. So we could write one of the following:Union[Union[int, str], float] == Union[int, str, float] == int | str | floatUnion[Union[int, str], float] == int | str | float
I kind of like the second one, since it emphasizes the new syntax, but I thought it might be a bit confusing.
- The redundant arguments should probably be changed to either list both
Union[int, str, int] == Union[int, str]andint | str | int == int | stror only the latter (my preference). No idea why I chose this strange middle thing. - Same of comparing unions, really.
There was a problem hiding this comment.
You points make sense. I would like to not outright replace the examples under typing.Union with only the PEP 604 version. For the same reason that we haven't changed examples under typing.List [1] to PEP 585 style list and instead just changed every other use of typing.List elsewhere (same for every other PEP 585-ied type really). Considering at runtime they aren't the same, I'd hate if beginners tripped over some runtime corner case thinking they were. (e.g. isinstance(1, int | str) works, isinstance(1, Union[int, str]) fails before 3.10)
This is one tough pickle :(. So I'm OK with the status quo.
[1] https://docs.python.org/3.11/library/typing.html#typing.List
* Use "X | Y" instead of "Union" where it makes sense. * Mention that "X | Y" is equivalent to "Union[X, Y]" in Union section. * Remove "Optional[X]" as shorthand for "Union[X, None]" as the new shorthand is now "X | None". * Mention that "Optional[X]" can be written as "X | None" in section about "Optional". Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> (cherry picked from commit dabb6e8) Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
|
GH-27897 is a backport of this pull request to the 3.10 branch. |
shorthand is now "X | None".
about "Optional".
https://bugs.python.org/issue44957