Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…ional_keys in TypedDict
alicederyn commentedNov 29, 2023
I don't think this change is required; see#112509 (comment) |
AlexWaygood left a comment
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.
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 :)
Uh oh!
There was an error while loading.Please reload this page.
| classChild(Base1,Base2): | ||
| pass | ||
| # Last base wins |
AlexWaygoodNov 29, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
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
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.
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'>}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.
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>
Thanks@JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12. |
GH-112530 is a backport of this pull request to the3.12 branch. |
…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>
…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>
GH-112531 is a backport of this pull request to the3.11 branch. |
…ional_keys in TypedDict (python#112512)Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…ional_keys in TypedDict (python#112512)Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Uh oh!
There was an error while loading.Please reload this page.
__required_keys__and__optional_keys__can be wrong in the presence of inheritance #112509