python-attrs / python-attrs/attrs
Typing with converters not working
Open
Nobody has claimed this yet.
Bug
Typing
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
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 https://github.com/python/mypy/issues/5738, but the issue goes back as far as mypy 0.610.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python 3.6.7 reproduction with attrs 19.1.0 and a compatible mypy version, then compare the generated constructor signatures for classes C–F. Trace how attrs handles converter annotations and verify the fix against the reported List[str] calls, using the linked mypy issue as context.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100