Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
JelleZijlstra merged 7 commits intopython:mainfromvberlier:gh-130117
May 6, 2025

Conversation

vberlier
Copy link
Contributor

@vberliervberlier commentedFeb 14, 2025
edited by github-actionsbot
Loading

@ghost
Copy link

ghost commentedFeb 14, 2025
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

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 theskip news label instead.

@vberliervberlier changed the titlegh-130117: Document that nestedAnnotated types referenced through a type alias are not flattenedgh-130117: Document why nestedUnion,Literal, andAnnotated types referenced through a type alias are not flattenedFeb 14, 2025
@vberlier
Copy link
ContributorAuthor

This also applies toUnion andLiteral, so I updated them as well. I also noticed the documentation forLiteral was missing some of the helpful details that we have in the documentation forUnion andAnnotated.

@vberliervberlierforce-pushed thegh-130117 branch 2 times, most recently from929121a to1aafc37CompareFebruary 19, 2025 23:33
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
ContributorAuthor

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.

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
ContributorAuthor

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

@vberliervberlier requested a review fromsobolevnMarch 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
ContributorAuthor

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
ContributorAuthor

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.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra
Copy link
Member

This has a merge conflict now.

@JelleZijlstraJelleZijlstra added the needs backport to 3.13bugs and security fixes labelMay 6, 2025
@JelleZijlstraJelleZijlstra merged commitb936ccd intopython:mainMay 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 requestMay 6, 2025
…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>
@bedevere-app
Copy link

GH-133488 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelMay 6, 2025
JelleZijlstra added a commit that referenced this pull requestMay 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 commitb936ccd)Co-authored-by: Valentin Berlier <berlier.v@gmail.com>Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@vberlier
Copy link
ContributorAuthor

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?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@JelleZijlstraJelleZijlstraJelleZijlstra approved these changes

@AlexWaygoodAlexWaygoodAwaiting requested review from AlexWaygoodAlexWaygood is a code owner

@sobolevnsobolevnAwaiting requested review from sobolevn

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@vberlier@JelleZijlstra@sobolevn

[8]ページ先頭

©2009-2025 Movatter.jp