Skip to content
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

gh-112509: Fix keys being present in both required_keys and optional_keys in TypedDict #112512

Merged
merged 3 commits into from
Nov 29, 2023

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Nov 29, 2023

@alicederyn
Copy link

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

Copy link
Member

@AlexWaygood AlexWaygood 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 to PEP-589 has little bearing on what we should do at runtime here. I don't want us to start raising a TypeError if a TypedDict incompatibly overrides a specific key to make it Required when the subclass specified the same key as being NotRequired. 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 a TypeError 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 :)

class Child(Base1, Base2):
pass

# Last base wins
Copy link
Member

@AlexWaygood AlexWaygood Nov 29, 2023

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 win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
...     x = 1
...
>>> class Bar:
...     x = 2
...
>>> class Baz(Foo, Bar): pass
...
>>> Baz.x
1

Copy link
Member Author

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.
🐍🍒⛏🤖

@JelleZijlstra JelleZijlstra deleted the reqnreq branch November 29, 2023 17:36
@bedevere-app
Copy link

bedevere-app bot commented Nov 29, 2023

GH-112530 is a backport of this pull request to the 3.12 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 29, 2023
…ional_keys in TypedDict (pythonGH-112512)

(cherry picked from commit 4038869)

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 request Nov 29, 2023
…ional_keys in TypedDict (pythonGH-112512)

(cherry picked from commit 4038869)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 29, 2023
@bedevere-app
Copy link

bedevere-app bot commented Nov 29, 2023

GH-112531 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Nov 29, 2023
JelleZijlstra added a commit that referenced this pull request Nov 29, 2023
…tional_keys in TypedDict (GH-112512) (#112531)

(cherry picked from commit 4038869)

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 request Nov 29, 2023
…tional_keys in TypedDict (GH-112512) (#112530)

(cherry picked from commit 4038869)

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 request Feb 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 request Sep 2, 2024
…ional_keys in TypedDict (python#112512)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants