Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
AlexWaygood merged 1 commit intopython:mainfromAlexWaygood:add-converter-typing
Jul 25, 2023

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygoodAlexWaygood commentedJul 24, 2023
edited by bedevere-bot
Loading

Some of this looks alittle complicated at first glance, but it's hopefully not too hard to grok

Comment on lines +2651 to +2656
CConverterClassT=TypeVar("CConverterClassT",bound=type["CConverter"])

defadd_c_converter(
f:type[CConverter],
f:CConverterClassT,
name:str|None=None
)->type[CConverter]:
)->CConverterClassT:
Copy link
MemberAuthor

@AlexWaygoodAlexWaygoodJul 24, 2023
edited
Loading

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].

erlend-aasland reacted with rocket emoji
Copy link
MemberAuthor

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.

erlend-aasland reacted with rocket emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Neat!

Copy link
Contributor

@erlend-aaslanderlend-aasland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks!

AlexWaygood reacted with heart emoji
@AlexWaygoodAlexWaygood merged commitdbfe738 intopython:mainJul 25, 2023
@AlexWaygoodAlexWaygood deleted the add-converter-typing branchJuly 25, 2023 08:18
jtcave pushed a commit to jtcave/cpython that referenced this pull requestJul 27, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@erlend-aaslanderlend-aaslanderlend-aasland approved these changes

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@AlexWaygood@erlend-aasland@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp