Skip to content

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

@MSK61

Description

@MSK61

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions