-
-
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 655: Integrate feedback from circa Feb 2021 #2248
Conversation
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.
Welcome back!
|
||
:: | ||
|
||
assert Movie.__required_keys__ == frozenset({'title'}) |
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.
fyi, the typing-extensions implementation doesn't do this. Implementing this behavior would require the TypedDict constructor to inspect the annotations on each value, which is somewhat fragile and won't work if you use typing.TypedDict with typing_extensions.Required.
As the author of a tool that needs to understand this at runtime, I'd be OK with changing the spec so that Required and NotRequired aren't reflected in required_keys and optional_keys.
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.
Implementing this behavior would require the TypedDict constructor to inspect the annotations on each value
Aye.
won't work if you use typing.TypedDict with typing_extensions.Required.
typing_extensions.Required is identical to typing.Required at runtime if the latter is available:
# typing_extensions.py
if hasattr(typing, 'Required'):
Required = typing.Required
NotRequired = typing.NotRequired
is somewhat fragile
Doesn't seem to me like the code to do the inspection would be very complex: It just has to unwrap Annotated[]
values until it hits (Not)Required[]
or something else. And then toss out the (Not)Required[]
wrapper before stuffing the result into the appropiate __*_keys__
attribute.
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.
typing_extensions.Required is identical to typing.Required at runtime if the latter is available:
But on 3.8 through 3.10 typing.TypedDict exists and doesn't know about typing_extensions.Required.
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.
As the author of a tool that needs to understand this at runtime, I'd be OK with changing the spec so that Required and NotRequired aren't reflected in required_keys and optional_keys.
👍
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.
But on 3.8 through 3.10 typing.TypedDict exists and doesn't know about typing_extensions.Required.
Good point: I probably can't update typing.TypedDict in 3.8-3.10 to support a feature from 3.11.
Looks like the strategy in the past for this type of issue is to make typing_extensions.TypedDict recognize all supported syntax in the latest accepted typing PEPs, with users of typing.TypedDict sometimes receiving incomplete functionality. For example, under that interpretation:
- typing.TypedDict in Python 3.8-3.10 would not understand typing_extensions.Required and would put something like
typing_extensions.Required[T]
incorrectly intoMyTypingDict.__optional_keys__
rather than puttingT
intoMyTypingDict.__required_keys__
. - typing_extensions.TypedDict in Python 3.8-3.10 would correctly understand typing_extensions.Required.
- typing_extensions.TypedDict in Python 3.11+ would alias typing.TypedDict, which would correctly recognize both typing.Required and typing_extensions.Required (because the latter would alias the former).
Does that sound reasonable @JelleZijlstra ?
(If so I should update the "How to Teach This" section to recommend use of typing_extensions.TypedDict over typing.TypedDict in Python <= 3.10 when used in conjunction with typing_extensions.Required. 🎗️ )
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 would work and it's fine with me.
I'd still be OK with just skipping this work completely. My experience is that runtime checks in typing.py are fragile (in case a later PEP changes what kind of things are accepted at runtime) and often don't do exactly what I'd want when consuming the annotations. For example, in pyanalyze I'd still want to parse Required/NotRequired myself for use cases where users use 3.8-3.10 typing.TypedDict. Therefore, I'd generally favor minimizing runtime work and just making sure whatever the user wrote is recoverable through runtime introspection.
Minor nit (GH doesn't seem to allow leaving review comments on unchanged lines -- this is line 17) - `PEP 589 <https://www.python.org/dev/peps/pep-0589/>`__ defines syntax
+ :pep:`589` defines syntax A |
Thanks. @AA-Turner Happen to know how I can adjust links to specific PEP sections with specific text? For example:
|
@davidfstr Currently you can't do any of those (I believe even the I could cherry-pick just that referenced commit and propose it as a PR, which would allow the "best" form ( A |
@AA-Turner Seems like you're planning to update all references anyway, so I have no preference on timing. For now I'll leave the more-complex PEP links in their original URL-based form. If you happen to get improved |
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.
Happy to merge this whenever you want me to. We can also wait if Guido has any feedback.
@davidfstr #2251 was just merged, so you should now be able to use A |
c687b87
to
3897926
Compare
Done. Did rebase branch to tip of main (with the new syntax) and applied syntax in new commit at end of branch. |
Looks to me like all of the formatting issues have been resolved. There are 2 more-major pieces of feedback to integrate, but it would probably be easier to do that in (1) another PR to the PEP and (2) another mailing-list post to typing-sig. So @JelleZijlstra I'd advocate just merging this branch now that we're happy with it. |
I'm not Jelle, but I'll merge as I was on this PR anyway & Jelle already approved. |
This branch is easiest to review either:
CC sponsor @gvanrossum