Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-104050: Argument clinic: improve typing around adding C converters#107209
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| CConverterClassT=TypeVar("CConverterClassT",bound=type["CConverter"]) | ||
| defadd_c_converter( | ||
| f:type[CConverter], | ||
| f:CConverterClassT, | ||
| name:str|None=None | ||
| )->type[CConverter]: | ||
| )->CConverterClassT: |
AlexWaygoodJul 24, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 using aTypeVar here (and with the other functions in this PR), we can make the annotation much more expressive.
The annotation currently says "you put in an objectf that's some subtype oftype[CConverter], and you get out an object that's some subtype oftype[CConverter].
The new annotation, by contrast, says, "you put in an objectf, and you get out an object that has the same type as whatever the type off is,as long as the type off is a subtype oftype[CConverter].
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 an example, with the following snippet:
x=add_c_converter(bool_converter)
Mypy would have inferred the type ofx to betype[CConverter] with the current annotation. But with the new annotation, mypy should be able to infer that the type ofx isbool_converter.
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.
Neat!
erlend-aasland left a comment
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.
Thanks!
Uh oh!
There was an error while loading.Please reload this page.
Some of this looks alittle complicated at first glance, but it's hopefully not too hard to grok