Skip to content

gh-130117: Document why nested Union, Literal, and Annotated 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 6, 2025

Conversation

vberlier
Copy link
Contributor

@vberlier vberlier commented Feb 14, 2025

@ghost
Copy link

ghost commented Feb 14, 2025

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Feb 14, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@vberlier vberlier changed the title gh-130117: Document that nested Annotated types referenced through a type alias are not flattened gh-130117: Document why nested Union, Literal, and Annotated types referenced through a type alias are not flattened Feb 14, 2025
@vberlier
Copy link
Contributor Author

This also applies to Union and Literal, so I updated them as well. I also noticed the documentation for Literal was missing some of the helpful details that we have in the documentation for Union and Annotated.

@vberlier vberlier force-pushed the gh-130117 branch 2 times, most recently from 929121a to 1aafc37 Compare February 19, 2025 23:33
@vberlier vberlier requested a review from sobolevn February 19, 2025 23:35
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
Copy link
Member

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.

Copy link
Contributor Author

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 between typing.rst and typing.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 that Union/Literal/Annotated flattening doesn't work with TypeAliasType, 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 for Union/Literal/Annotated probably reduces the need to jump up and down the page to a hypothetical common section about "union-like" types when reading.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright I'll only modify typing.rst

@vberlier vberlier requested a review from sobolevn March 1, 2025 00:23
@@ -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
Copy link
Member

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.

Copy link
Contributor Author

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"?

Copy link
Member

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.

Copy link
Contributor Author

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

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.

@vberlier vberlier requested a review from JelleZijlstra March 6, 2025 21:59
Co-authored-by: Jelle Zijlstra <[email protected]>
@JelleZijlstra
Copy link
Member

This has a merge conflict now.

@JelleZijlstra JelleZijlstra added the needs backport to 3.13 bugs and security fixes label May 6, 2025
@JelleZijlstra JelleZijlstra merged commit b936ccd into python:main May 6, 2025
30 checks passed
@miss-islington-app
Copy link

Thanks @vberlier for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 6, 2025
…ed` types referenced through a type alias are not flattened (pythonGH-130119)

(cherry picked from commit b936ccd)

Co-authored-by: Valentin Berlier <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented May 6, 2025

GH-133488 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 6, 2025
JelleZijlstra added a commit that referenced this pull request May 6, 2025
…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 commit b936ccd)

Co-authored-by: Valentin Berlier <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
@vberlier
Copy link
Contributor Author

vberlier commented May 6, 2025

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?

Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…ed` types referenced through a type alias are not flattened (python#130119)

Co-authored-by: Jelle Zijlstra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants