KEMBAR78
Make "X | Y" union syntax more prominent in documentation by JukkaL · Pull Request #17835 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Sep 26, 2024

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). 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.

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`.
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this confusion is related to the name Optional, it might be better to keep using Optional[T] here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

JukkaL and others added 2 commits September 26, 2024 14:50
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JukkaL JukkaL merged commit db7b61b into master Sep 26, 2024
2 checks passed
@JukkaL JukkaL deleted the docs-union branch September 26, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants