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-112509: Fix keys being present in both required_keys and optional_keys in TypedDict#112512

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 3 commits intopython:mainfromJelleZijlstra:reqnreq
Nov 29, 2023

Conversation

@JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedNov 29, 2023
edited by bedevere-appbot
Loading

@alicederyn
Copy link

I don't think this change is required; see#112509 (comment)

Copy link
Member

@AlexWaygoodAlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks, I agree that this is a problem that's worth fixing.

I don't think this change is required; see#112509 (comment)

I actually think whether or not static type checkers should consider this behaviour illegal according toPEP-589 has little bearing on what we should do at runtime here. I don't want us to start raising aTypeError if aTypedDict incompatibly overrides a specific key to make itRequired when the subclass specified the same key as beingNotRequired. Doing that now would break backwards compatibility at runtime, and I don't think there's a strong motivation for doing so: it's the job of type checkers to catch this kind of error and emit warnings about it. So, if we've ruled out raising aTypeError in this kind of situation, we just have to do the thing at runtime that makes the most sense -- and the status quo doesn't really make any sense, I don't think :)

alicederyn reacted with thumbs up emoji
classChild(Base1,Base2):
pass

# Last base wins
Copy link
Member

@AlexWaygoodAlexWaygoodNov 29, 2023
edited
Loading

Choose a reason for hiding this comment

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

Is it correct that the last base should win? Doesn't that go against how multiple inheritance in Python usually works, where earlier bases in the__bases__ tuple generally have priority?

Python 3.13.0a2+ (heads/main:e9d1360c9a, Nov 24 2023, 11:23:45) [MSC v.1932 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>classFoo:...     x=1...>>>classBar:...     x=2...>>>classBaz(Foo,Bar):pass...>>> Baz.x1

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It's arguably not correct as you say, but it's the current behavior for__annotations__ and changing that behavior seems difficult.

>>> class A(TypedDict):...     a: int... >>> class B(TypedDict):...     a: str... >>> class C(A, B): pass... >>> C.__annotations__{'a': <class 'str'>}

Copy link
Member

Choose a reason for hiding this comment

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

Oof, that seems unfortunate. As you say, though, better to be internally consistent for now, I guess!

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@miss-islington-app
Copy link

Thanks@JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@JelleZijlstraJelleZijlstra deleted the reqnreq branchNovember 29, 2023 17:36
@bedevere-app
Copy link

GH-112530 is a backport of this pull request to the3.12 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 29, 2023
…ional_keys in TypedDict (pythonGH-112512)(cherry picked from commit4038869)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestNov 29, 2023
…ional_keys in TypedDict (pythonGH-112512)(cherry picked from commit4038869)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelNov 29, 2023
@bedevere-app
Copy link

GH-112531 is a backport of this pull request to the3.11 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.11only security fixes labelNov 29, 2023
JelleZijlstra added a commit that referenced this pull requestNov 29, 2023
…tional_keys in TypedDict (GH-112512) (#112531)(cherry picked from commit4038869)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
JelleZijlstra added a commit that referenced this pull requestNov 29, 2023
…tional_keys in TypedDict (GH-112512) (#112530)(cherry picked from commit4038869)Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
…ional_keys in TypedDict (python#112512)Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
…ional_keys in TypedDict (python#112512)Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@AlexWaygoodAlexWaygoodAlexWaygood approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@JelleZijlstra@alicederyn@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp