-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
gh-91243: Add typing.Required and NotRequired (PEP 655) #32419
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.
LGTM.
@@ -0,0 +1,2 @@ | |||
Implement ``typing.Required`` and ``typing.NotRequired`` (:pep:`655`). Patch | |||
by Jelle Zijlstra. |
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.
by Jelle Zijlstra. | |
by David Foster and Jelle Zijlstra. |
@davidfstr wrote the original code in typing-extensions so I shouldn't claim sole credit.
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.
@davidfstr wrote the original code in typing-extensions so I shouldn't claim sole credit.
Then also add a Contributed-by header (or whatever it's called) to the commit message?
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 put it in the PR header and I'll add it to the commit message when I merge this. (I'll give it some more time though, to give other people a chance to give feedback.)
Some comments below. Sorry for any unusual formatting; am writing while mobile:
Perhaps checking just “Required” as a superclass and not only “type(Required)” would be useful?
This implementation recognizes both “Required[T]” and “Annotated[Required[T], A]”, but does not recognize multiply-nested Annotateds like ““Annotated[Annotated[Required[T], A1], A2]”. I’m guessing we probably want to support nested Annotateds?
Nit: Accepts only a single type? Also, is this error message necessarily accurate? That is, could you reasonably get it if you did something other than “pass multiple types”? Is there a test case for this error message? (I don’t remember one.) |
Thanks for the useful feedback!
Added
Added a few tests. This already works because the runtime collapses multiple levels of Annotated.
Fixed the wording. The message only gets used if the type is a tuple, so it is indeed accurate.
There is a test for
|
Last question:
Otherwise LGTM. |
These errors get formatted with a second sentence. For example, We should perhaps change those messages (at least, Final should say "a single type"), but we should do it for all types, not just the new ones, and it's better done in a separate PR. |
Makes sense.
+1 to make the change. +1 to do it in a separate PR. As mentioned before, everything else LGTM! |
Thanks! Since Guido and Ken already approved I'm merging this now. |
I talked to @davidfstr and I offered to implement the runtime part of PEP 655
to make sure we can get it in before the feature freeze. We're going to defer
the documentation to a separate PR, because it can wait until after the feature
freeze.
The runtime implementation conveniently already exists in typing-extensions,
so I largely copied that.
Co-authored-by: David Foster david@dafoster.net
https://bugs.python.org/issue47087