Make "X | Y" union syntax more prominent in documentation - #17835
Merged
Conversation
Soon 4 out of 5 supported Python versions will support the `X | Y` syntax, so we can make it more prominent (Python 3.13 will be out soon, and 3.8 will reach end of life). The syntax is available starting from Python 3.10, but it can be used in annotations in earlier Python versions as well if using `from __future__ import annotations`.
JelleZijlstra
approved these changes
Sep 26, 2024
JelleZijlstra
left a comment
Member
There was a problem hiding this comment.
Looks good, some small feedback
|
|
||
| Use the ``Union[T1, ..., Tn]`` type constructor to construct a union | ||
| type. For example, if an argument has type ``Union[int, str]``, both | ||
| Use the ``T1 | ... | Tn`` type constructor to construct a union |
Member
There was a problem hiding this comment.
Suggested change
| Use the ``T1 | ... | Tn`` type constructor to construct a union | |
| Use the ``T1 | ... | Tn`` operator to construct a union |
Feels odd to call this a "type constructor"
Collaborator
Author
There was a problem hiding this comment.
Using "operator" here also didn't feel quite right, so I just left out the "type constructor" bit.
| t1: int | str # equivalent to Union[int, str] | ||
|
|
||
| t2: int | None # equivalent to Optional[int] | ||
| The type ``... | None`` *does not* mean a function parameter with a default value. |
Member
There was a problem hiding this comment.
Since this confusion is related to the name Optional, it might be better to keep using Optional[T] here.
Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Soon 4 out of 5 supported Python versions will support the
X | Ysyntax,so we can make it more prominent (Python 3.13 will be out soon, and 3.8
will reach end of life). Use it in most examples.
The syntax is available starting from Python 3.10, but it can be used
in annotations in earlier Python versions as well if using
from __future__ import annotations.