-
-
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
Changes from 5 commits
0c12774
f3588ac
4d988a8
f21c30a
3f60fb0
033cf07
eea168f
15a7930
b9badea
a6a870c
8b113dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1389,10 +1389,18 @@ 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 third-party type-checking tools. | ||||||
97littleleaf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
By default, all keys must be present in a ``TypedDict``. It is possible to | ||||||
override this by specifying totality. | ||||||
|
@@ -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, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1976,6 +1976,7 @@ Masayuki Yamamoto | |
Ka-Ping Yee | ||
Chi Hsuan Yen | ||
Jason Yeo | ||
Jingchen Ye | ||
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. It feels absurd to bring this up (sorry!!), but: I think "Jingchen Ye" should go before "Ka-Ping Yee", if we're keeping this list alphabetised. |
||
EungJun Yi | ||
Bob Yodlowski | ||
Danny Yoo | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,2 @@ | ||||||
Deprecate kwargs-based syntax for :class:`typing.TypedDict` definitions. | ||||||
It was unsupported by type checkers. Patch by Jingchen Ye. | ||||||
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
Pyright supports it. 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. I think it's good to give a short summary of why this change is being made. Maybe |
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!