-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
PEP 728: TypedDict with Typed Extra Fields #3441
Conversation
Hi @AlexWaygood! Thanks for expressing interest in this feature. Could you confirm yourself as a sponsor of this PEP? |
There is significant overlap with some of the ideas in #3440, a proposed rewrite of PEP 705. It may be worth discussing the interaction of the two ideas on Discuss before committing to a PEP. |
While I assumed that literal blocks starting with |
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.
A few comments.
I'll also note that I don't like this proposal's current form of using a special-cased key. I would prefer some mechanism that doesn't look like the TypedDict has a key "__extra__"
.
peps/pep-0728.rst
Outdated
Title: TypedDict with Typed Extra Fields | ||
Author: Zixuan James Li <[email protected]> | ||
Sponsor: Alex Waygood <[email protected]> | ||
Discussions-To: https://discuss.python.org/t/pep-692-using-typeddict-for-more-precise-kwargs-typing/17314 |
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.
Should be a new thread, not the one for PEP 692
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.
Before this is merged, should I create a new "Typing" thread or leave it blank until a "pep" thread can be created?
Author of the PEP-705 rewrite! Excited to see this :) The syntax I'm going for in PEP-705 is class Foo(TypedDict, other_keys=False): I'm happy to rewrite the proposal to reference this PEP, once it is published. (I have a personal preference for My main concern that led me to add How would this proposal interact with the |
I was assuming you'd send me a draft before making a PR to the peps repo 😄 But no worries! In principle, I'm still happy to sponsor something along these lines, but please give me some time to read this and PEP-705 over and think about it before confirming. Ping me again if I haven't got back to you in a few days :) |
Converting to draft to mark that there's no sponsor (yet). A |
@AlexWaygood Did you get a chance to look at this and PEP-705 yet? I modified 705 to bring it more in line with this draft, and it's now gone out for another round of comments. |
Eric gave some great feedback on the subset-variant of this PEP that I included in PEP-705, raising questions that need more consideration. pyright has also changed to allow type discrimination without the I had a couple ideas for alternative syntax that might address Eric's points. EllipsisOption 1: use an ellipsis ( Foo = TypedDict("Foo", { "bar": int, ...: str })
foo: { "bar": int, ...: str } But it is a SyntaxError in class definition syntax: class Foo(TypedDict):
bar: int
...: str # syntax error That leaves us deciding between:
I think it looks decent though, especially in inline syntax, so throwing it out there. A type qualifierOption 2: use a type qualifier instead of a "reserved" key name. class Foo(TypedDict):
bar: int
_: OtherValues[str] Users can use any key that isn't already used, so there's no risk of accidentally changing the meaning of existing definitions, but the PEP can recommend a conventional default to use whenever possible. I like underscore as it is short and it is already conventional to use this for "other stuff" but haven't put much thought into it. This also has the advantage of being hyperlinked to documentation explaining what it is in editors, making it more discoverable than |
I kind of like the type qualifier idea, but it can have issues with subclassing to address. Will revisit the draft probably this weekend and incorporate some new progress made from recent discussions on PEP-705. |
@PIG208, what is the status of this PEP? Are you looking to move forward with it? It came up in a recent typing discussion. |
I need to catch up with the previous discussions on the topic to get it up-to-date. I just went back from a break and will push some updates soon. |
I talked to @AlexWaygood and I offered to take over as the sponsor of the PEP. If that's OK with you, @PIG208, please add my name to the headers. I'll also do another review pass on the PEP once you've addressed the open feedback. Feel free to email me (my email address is all over this repo) to discuss the logistics and process of moving the PEP forward. |
fec95c2
to
9922546
Compare
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
I left out the rewrite to other proposed syntax such as using "OtherFields[]" with an arbitrary key before there is sufficient discussion on the alternatives available to us. Signed-off-by: Zixuan James Li <[email protected]>
The last round of changes makes some great improvements. I left a few quick comments, but will do another review and then hopefully we can merge the PEP in. One of the next steps should be to implement the PEP in https://github.com/python/typing_extensions. I think that should just require some minor adjustments to |
Also split parts of it into individual sections dicussing changes to TypedDict type consistency with Mapping and dict. Signed-off-by: Zixuan James Li <[email protected]>
Addresses some review feedback. Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Thank you for the review @JelleZijlstra ! Opened python/typing_extensions#329. |
I'm considering renaming |
This follows the style of PEP 705 and the majority of the typing spec to use "key" or "item" instead of "field" depending on the context. It also makes use of "value type" more to be adequately more pedantic. The special key added is now referred to as the "reserved key" to make it clearer that it is not intended for other purposes anymore. It also make use of type aliases like `VT` more for brevity. Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Signed-off-by: Zixuan James Li <[email protected]>
Hmm, not sure why is |
Pushing a couple of small fixes to satisfy the merge requirements, I think we're almost there! |
And it's merged! It should be up at https://peps.python.org/pep-0728 momentarily. As a next step, could you post in https://discuss.python.org/c/peps/19 about the PEP, and then update the Discussions-To/Post-History headers here in another PR. |
Got it, thanks! |
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.
Please could you add the Discourse link to Discussions-To and Post-History headers?
Edit: done in #3658 ✅
Python-Version: 3.13 | ||
|
||
|
||
.. highlight:: rst |
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.
Please remove this, the code blocks should default to Python instead.
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.
Was wondering about why that was happening. Thanks for spotting that!
|
||
Thanks to Jelle Zijlstra for sponsoring this PEP and providing review feedback, | ||
Eric Traut who `proposed the original design | ||
<https://mail.python.org/archives/list/[email protected]/message/3Z72OQWVTOVS6UYUUCCII2UZN56PV5II/>` |
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.
This isn't rendering as a link properly, I think it needs a couple of underscores at the end?
Basic requirements (all PEP Types)
pep-NNNN.rst
), PR title (PEP 123: <Title of PEP>
) andPEP
headerAuthor
orSponsor
, and formally confirmed their approvalAuthor
,Status
(Draft
),Type
andCreated
headers filled out correctlyPEP-Delegate
,Topic
,Requires
andReplaces
headers completed if appropriate.github/CODEOWNERS
for the PEPStandards Track requirements
Python-Version
set to valid (pre-beta) future Python version, if relevantDiscussions-To
andPost-History
📚 Documentation preview 📚: https://pep-previews--3441.org.readthedocs.build/