-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
bpo-46066: Deprecate kwargs syntax for TypedDict definitions #31126
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c12774
Remove kwargs syntax for TypedDict
97littleleaf11 f3588ac
Add deprecation flag and warning
97littleleaf11 4d988a8
Add blurb and address reviews
97littleleaf11 f21c30a
Change docs
97littleleaf11 3f60fb0
Add name
97littleleaf11 033cf07
Fix
97littleleaf11 eea168f
Modify docs
97littleleaf11 15a7930
Use bullet list
97littleleaf11 b9badea
Fix
97littleleaf11 a6a870c
Fix
97littleleaf11 8b113dd
reverse
97littleleaf11 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -1389,11 +1389,19 @@ These are not used in annotations. They are building blocks for declaring types. | |||||
``Point2D.__optional_keys__``. | ||||||
To allow using this feature with older versions of Python that do not | ||||||
support :pep:`526`, ``TypedDict`` supports two additional equivalent | ||||||
syntactic forms:: | ||||||
syntactic forms. Firstly, using a literal :class:`dict` as the | ||||||
second argument:: | ||||||
|
||||||
Point2D = TypedDict('Point2D', x=int, y=int, label=str) | ||||||
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) | ||||||
|
||||||
Secondly, using keyword arguments:: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Point2D = TypedDict('Point2D', x=int, y=int, label=str) | ||||||
|
||||||
.. deprecated-removed:: 3.11 3.13 | ||||||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
The keyword-argument syntax is deprecated in 3.11 and will be removed | ||||||
in 3.13. It may also be unsupported by static type checkers. | ||||||
|
||||||
By default, all keys must be present in a ``TypedDict``. It is possible to | ||||||
override this by specifying totality. | ||||||
Usage:: | ||||||
|
@@ -1402,6 +1410,9 @@ These are not used in annotations. They are building blocks for declaring types. | |||||
x: int | ||||||
y: int | ||||||
|
||||||
# Alternative syntax | ||||||
Point2D = TypedDict('Point2D', {'x': int, 'y': int}, total=False) | ||||||
|
||||||
This means that a ``Point2D`` ``TypedDict`` can have any of the keys | ||||||
omitted. A type checker is only expected to support a literal ``False`` or | ||||||
``True`` as the value of the ``total`` argument. ``True`` is the default, | ||||||
|
This file contains 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 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 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/2022-02-08-16-42-20.bpo-46066.m32Hl0.rst
This file contains 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 @@ | ||
Deprecate kwargs-based syntax for :class:`typing.TypedDict` definitions. | ||
It had confusing semantics when specifying totality, and was largely unused. | ||
Patch by Jingchen Ye. | ||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
I'd suggest just writing "a
TypedDict
may be created using a functional form". This parallels https://docs.python.org/3.10/library/enum.html#functional-apiThere 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 this!