Skip to content

Typing with converters not working #519

Open
@berquist

Description

@berquist

Using attrs 19.1.0 on Python 3.6.7,

from typing import Any, Iterable, List, Tuple, TypeVar

from attr import attrs, attrib

T = TypeVar("T")

def to_tuple(val: Iterable[T]) -> Tuple[T, ...]:
    return tuple(val)

def to_list(val: Iterable[T]) -> List[T]:
    return list(val)

@attrs
class C:
    elements: Tuple[str, ...] = attrib(converter=to_tuple)

@attrs
class D:
    elements: Tuple[str, ...] = attrib(converter=tuple)

@attrs
class E:
    elements: List[str] = attrib(converter=list)

@attrs
class F:
    elements: List[str] = attrib(converter=to_list)


if __name__ == "__main__":
    l = ["a", "b", "c"]
    c = C(l)
    d = D(l)
    e = E(l)
    f = F(l)

gives

32: error: Argument 1 to "C" has incompatible type "List[str]"; expected "Iterable[T]"
33: error: Argument 1 to "D" has incompatible type "List[str]"; expected "Iterable[_T_co]"
34: error: Argument 1 to "E" has incompatible type "List[str]"; expected "Iterable[_T]"
35: error: Argument 1 to "F" has incompatible type "List[str]"; expected "Iterable[T]"

This may be related to python/mypy#5738, but the issue goes back as far as mypy 0.610.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTypingTyping/stub/Mypy/PyRight related bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions