-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-137840: Implement PEP 728 (closed and extra_items in typing.TypedDict) #137933
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cbea6f8
Introduce NoExtraItems as a C-level singleton
angela-tarantula 4cde2f9
expose NoExtraItems via _typing
angela-tarantula 06de7a9
Clearer comments in the C internals
angela-tarantula 407bf74
Import NoExtraItems into typing
angela-tarantula f9755d3
Update _TypedDictMeta and TypedDict
angela-tarantula 385812a
fix typo
angela-tarantula bdbae49
Port over the relevant tests
angela-tarantula e68c2ed
Updated docstring and error messages
angela-tarantula 8203bc1
NEWS entry
angela-tarantula 1b3ce52
New draft of docstring and error message
angela-tarantula 2f5c42e
just use the original error message
angela-tarantula 1bd3a6d
fix typo
angela-tarantula 42a1aac
undo capitalization
angela-tarantula 36ee4d2
fix typo x2
angela-tarantula 503b825
fix typo x3
angela-tarantula 1f23caa
Hush the c-analyzer: NoExtraItems is a singleton
angela-tarantula 9a9c5c5
remove c-level NoExtraItems singleton
angela-tarantula 18b929b
Update Lib/typing.py
angela-tarantula 611b3e6
define NoExtraItems singleton in Python
angela-tarantula a6250f4
enforce singleton qualities on NoExtraItems
angela-tarantula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2025-08-18-07-10-55.gh-issue-137840.9b7AnG.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:class:`typing.TypedDict` now supports the ``closed`` and ``extra_items`` | ||
keyword arguments (as described in :pep:`728`) to control whether additional | ||
non-required keys are allowed and to specify their value type. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
range
the builtin here?That's a bit strange.
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.
I agree it looks a bit odd as an arbitrary
extra_items
value, but it's valid, since it just means extra keys must have values assignable to range. For context, this test is copied fromtyping_extensions
, where it was used to cover the same case. We could edit it for readability, but I kind of like it as a reminder for maintainers thatextra_items
accepts any type, not just the obvious ones.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.
That could be valid for a regular test that validates stored values.
this however is a negative test, isn’t it?
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 still valid/correct as a negative test. By "valid" do you mean "correct" or "readable?" Passing range into extra_items is valid, and passing closed=True at the same time should raise a runtime error.
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.
Sorry I was being terse.
I'm trying to say that this test:
Could have an
extra_items=range
counterpart.That would make a solid test, both understandable and useful.
Meanwhile, the negative test,
class TD(TypedDict, closed=True, extra_items=range): --> error
would be better served with a simpler, more straightforwardextra_items=int
argument.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.
P.S. my comment overall is minor, please don't let me stop your work!
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.
Thanks for clarifying. I thought about it some more.
I don't think introducing an
extra_items=range
counterpart would actually widen the test coverage, since it wouldn't be exercising any new behavior. PEP 728 splits responsibilities between runtime and type checker behavior. Whileextra_items
is only supposed to accept a valid type expression, validating that is the type checker's job (e.g. MyPy's valid-type error), not the runtime's. The runtime just stores whatever is passed in.So the real subject under test is simply:
We don't need multiple values to prove that behavior.
And although
range
is a less obvious type, I think it makes sense in the negative test. That test is specifically asserting the error message “Cannot combineclosed=True
andextra_items
”. Usingrange
highlights that the failure comes from the combination, not fromrange
itself being an invalid value ofextra_items
.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.
I tend to use
range
sometimes as it's a builtin type that isn't generic (likelist
) and doesn't participate in promotion weirdness (likefloat
and historicallybytes
), so it's a good basic type to test with.Plus, people who forget that
range
is a type get to learn something :)