-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
84897b2
Remove "Requires: 604" because that PEP has been released already
davidfstr 52a57a5
Permit use of NotRequired[X|None] as a string annotation for Python 3…
davidfstr 8ad4900
null -> None
davidfstr 144caf5
Reference Implementation: Link to mypy and pyright implementations
davidfstr a1621ff
Add section: Interaction with Annotated[...]
davidfstr 5c09b08
Add section: Interaction with __required_keys__ and __optional_keys__
davidfstr 8f56383
Rejected Ideas: Special syntax around the *key* of a TypedDict item: …
davidfstr 03ab8bc
Add section: Interaction with get_type_hints()
davidfstr 59c5fcd
Add section: Interaction with get_origin() and get_args()
davidfstr d009d90
Clarify: The alternative syntax for TypedDict also supports (Not)Requ…
davidfstr a32f1f1
Mention existing meanings for +T, -T, ~T.
davidfstr f0a0787
Update Post-History
davidfstr 9ae8574
Eliminate many uses of '...' to decrease typographic noise
davidfstr b1ea366
Use native PEP link
davidfstr 3897926
Use improved PEP links from #2251
davidfstr 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
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.
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.
Aye.
typing_extensions.Required is identical to typing.Required at runtime if the latter is available:
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.
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.
👍
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.
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_extensions.Required[T]
incorrectly intoMyTypingDict.__optional_keys__
rather than puttingT
intoMyTypingDict.__required_keys__
.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.