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

Add support for converters with TypeVars on generic attrs classes#14908

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
hauntsaninja merged 3 commits intopython:masterfromchadrik:attrs_converters
Mar 25, 2023

Conversation

@chadrik
Copy link
Contributor

When creating generic classes usingattrs, converters with type vars are not properly integrated into the generated__init__:

fromtypingimportTypeVar,Generic,List,Iterable,IteratorimportattrT=TypeVar('T')defint_gen()->Iterator[int]:yield1deflist_converter(x:Iterable[T])->List[T]:returnlist(x)@attr.s(auto_attribs=True)classA(Generic[T]):x:List[T]=attr.ib(converter=list_converter)y:T=attr.ib()a1=A([1],2)# E: Argument 1 to "A" has incompatible type "Iterator[int]"; expected "Iterable[T]"

This MR fixes the bug by copying type vars from the field/attrib into the type extracted from the converter.

jonassorgenfrei reacted with thumbs up emoji
@github-actions

This comment has been minimized.

@chadrikchadrikforce-pushed theattrs_converters branch 3 times, most recently from98fead7 tof33f58cCompareMarch 16, 2023 05:00
@github-actions

This comment has been minimized.

@chadrik
Copy link
ContributorAuthor

@t4lz@euresti Do either of you have a moment to review this attrs-related change, please?

Copy link
Contributor

@eurestieuresti left a comment

Choose a reason for hiding this comment

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

This change looks good to me.

variables= {
binder.id:argforbinder,arginzip(converter_vars,init_vars)
}
init_type=expand_type(init_type,variables)
Copy link
ContributorAuthor

@chadrikchadrikMar 16, 2023
edited
Loading

Choose a reason for hiding this comment

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

The idea behind this change is that a converter should ideally return the same type as the attrs field that it is added to. For example:

deflist_converter(x:Iterable[T])->List[T]:returnlist(x)@attr.s(auto_attribs=True)classA(Generic[T]):x:List[T]=attr.ib(converter=list_converter)

A.x is typeList[T] andlist_converter returnsList[T], and in theory this should always be the case for converters, since their purpose is to convert the init arg to the type of the field. In my changes I'm forgoing a complete comparison of the two types, but the assumption tells us that the number and order of TypeVars should be the same. We use that to generate a replacement map which will replace the converter's broken typevars, with typevars that work within the class's context.

Copy link
Contributor

Choose a reason for hiding this comment

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

(I assume thedef int_gen slipped in from another example?)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

yep. I fixed the example.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According tomypy_primer, this change has no effect on the checked open source code. 🤖🎉

@chadrik
Copy link
ContributorAuthor

Hi@JukkaL, can you or someone else from the mypy team merge this, please?

@chadrik
Copy link
ContributorAuthor

Hi all, can anyone help me get this one merged? It's approved and all ready to go!

Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks!

@hauntsaninjahauntsaninja merged commit3f35ebb intopython:masterMar 25, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

+2 more reviewers

@ikonstikonstikonst left review comments

@eurestieurestieuresti approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@chadrik@euresti@ikonst@hauntsaninja@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp