Skip to content
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

How can I use tuple as a converter to attr.ib without upsetting mypy? #8389

Closed
MSK61 opened this issue Feb 11, 2020 · 2 comments
Closed

How can I use tuple as a converter to attr.ib without upsetting mypy? #8389

MSK61 opened this issue Feb 11, 2020 · 2 comments

Comments

@MSK61
Copy link

MSK61 commented Feb 11, 2020

I originally had this question on stack overflow almost a week ago https://stackoverflow.com/questions/60029635/how-can-i-use-tuple-as-a-converter-to-attr-ib-without-upsetting-mypy. As I haven't received any answers for it, I've decided to post it here in the hope that someone may know how it should be addressed.

When I try to run mypy against this code

import attr
from typing import Tuple
@attr.s
class Test:
    x: Tuple[int, ...] = attr.ib(converter=tuple)
l = [1, 2]
Test(l)

I get the following error message:

<string>:7: error: Argument 1 to "Test" has incompatible type "List[int]"; expected "Iterable[_T_co]"

If I define a wrapper function like this one

def int_tpl(int_lst: Iterable[int]) -> Tuple[int, ...]:
    return tuple(int_lst)

and use it to set the converter parameter

    x: Tuple[int, ...] = attr.ib(converter=int_tpl)

then mypy becomes happy. Is this the only way or is there a better one?

@ilevkivskyi
Copy link
Member

Initially I thought this is related to #5738, but actually converters are heavily special-cased in mypy attrs plugin, essentially this bug is a duplicate of #5313

@ryansobol
Copy link

Just ran into this bug. Thank you for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants