Mohammed Safwat
Mohammed Safwat

Reputation: 311

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

I'm trying to run the following code(stripped down from another project) against mypy:

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

However I keep getting the following error message:

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

The only way I've found so far to overcome this error message is to explicitly define a wrapper function like this

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

And use it to define the converter for the attribute:

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

But I see this as too much boilerplate code. Is there a better approach to handle this problem or is this the only way?

Upvotes: 3

Views: 418

Answers (1)

Mohammed Safwat
Mohammed Safwat

Reputation: 311

I reported the problem on mypy github repo https://github.com/python/mypy/issues/8389 and it turned out to be an already diagnosed bug https://github.com/python/mypy/issues/5313.

Upvotes: 2

Related Questions