Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-130117: Document why nestedUnion
,Literal
, andAnnotated
types referenced through a type alias are not flattened#130119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ghost commentedFeb 14, 2025 • edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Uh oh!
There was an error while loading.Please reload this page.
Annotated
types referenced through a type alias are not flattenedUnion
,Literal
, andAnnotated
types referenced through a type alias are not flattenedThis also applies to |
929121a
to1aafc37
CompareLib/typing.py Outdated
@@ -765,6 +765,13 @@ def Union(self, parameters): | |||
assert Union[Union[int, str], float] == Union[int, str, float] | |||
However, this does not apply to unions referenced through a type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think that we should document this in one place, no need to duplicate this info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Do you mean the existing duplication betweentyping.rst
andtyping.py
? I assume it's intended so I don't want to make the two diverge by only updating one of them.
Or is it that you'd prefer a separate section for mentioning thatUnion
/Literal
/Annotated
flattening doesn't work withTypeAliasType
, instead of it being inlined within each type's respective documentation?
I'd rather document a specific caveat about a feature as close as possible to where the feature itself is documented, otherwise it could easily be missed by readers. And since the flattening behavior is explained in each type's respective documentation, it makes sense to follow suit. I'm guessing that this is the intent of the original authors as the overall redundancy in the properties listed forUnion
/Literal
/Annotated
probably reduces the need to jump up and down the page to a hypothetical common section about "union-like" types when reading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
In general, the docstrings are meant to be more concise. I think the new information in this PR is specialized enough that we can document it only in typing.rst and not the docstrings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Alright I'll only modifytyping.rst
Doc/library/typing.rst Outdated
@@ -1222,6 +1228,33 @@ These can be used as types in annotations. They all support subscription using | |||
is allowed as type argument to ``Literal[...]``, but type checkers may | |||
impose restrictions. See :pep:`586` for more details about literal types. | |||
``Literal`` is very similar to :class:`Union`, the main difference is that its |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This sentence is likely to cause confusion for many readers. Literal and Union are very different things in the type system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Would it be better as "The Literal annotation supports the same operations as Union"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think it's better not to draw comparisons. Literal and Union have similar runtime behavior but they mean different things, and I don't think it will help most readers to compare them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Fair enough
Lib/typing.py Outdated
@@ -765,6 +765,13 @@ def Union(self, parameters): | |||
assert Union[Union[int, str], float] == Union[int, str, float] | |||
However, this does not apply to unions referenced through a type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
In general, the docstrings are meant to be more concise. I think the new information in this PR is specialized enough that we can document it only in typing.rst and not the docstrings.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This has a merge conflict now. |
b936ccd
intopython:mainUh oh!
There was an error while loading.Please reload this page.
Thanks@vberlier for the PR, and@JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…ed` types referenced through a type alias are not flattened (pythonGH-130119)(cherry picked from commitb936ccd)Co-authored-by: Valentin Berlier <berlier.v@gmail.com>Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
GH-133488 is a backport of this pull request to the3.13 branch. |
…ted` types referenced through a type alias are not flattened (GH-130119) (#133488)gh-130117: Document why nested `Union`, `Literal`, and `Annotated` types referenced through a type alias are not flattened (GH-130119)(cherry picked from commitb936ccd)Co-authored-by: Valentin Berlier <berlier.v@gmail.com>Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Thanks for taking the time to merge this! Is there an automated workflow that's supposed to close the associated issue or should we close it manually? |
Uh oh!
There was an error while loading.Please reload this page.
typing.Annotated
does not flatten throughtyping.TypeAliasType
#130117📚 Documentation preview 📚:https://cpython-previews--130119.org.readthedocs.build/