`attr.ib(converter=)` appears to cause mypy to lose generic types for `__init__` function.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
$ mypy --version
mypy 0.760
attr_test.py:
import attr
from typing import Sequence
@attr.s(auto_attribs=True)
class Foo:
things: Sequence[str]
@attr.s(auto_attribs=True)
class Bar:
things: Sequence[str] = attr.ib(converter=tuple)
@attr.s(auto_attribs=True)
class Raz:
thing: str = attr.ib(converter=str)
Foo(['hello', 'world'])
Raz('hello')
Bar(['hello', 'world'])
reveal_type(Foo.__init__)
reveal_type(Bar.__init__)
reveal_type(Raz.__init__)
Output:
$ mypy attr_test.py
attr_test.py:18: error: List item 0 has incompatible type "str"; expected "_T_co"
attr_test.py:18: error: List item 1 has incompatible type "str"; expected "_T_co"
attr_test.py:19: note: Revealed type is 'def (self: attr_test.Foo, things: typing.Sequence[builtins.str])'
attr_test.py:20: note: Revealed type is 'def (self: attr_test.Bar, things: typing.Iterable[_T_co`1])'
attr_test.py:21: note: Revealed type is 'def (self: attr_test.Raz, thing: builtins.object)'
I would expect attr_test.py to typecheck cleanly and for the signature of Bar.__init__ to be def (self: attr_test.Bar, things: typing.Iterable[str]) . I'm guessing mypy created a new type _T_co`1 that's something like TypeVar('T', bound=str, covariant=True) but it doesn't seem like that's being used later.
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 attr_test.py example with mypy 0.760 and compare the revealed init types for Foo, Bar, and Raz. Trace why converter=tuple produces Iterable[_T_co] and converter=str produces object. Done means the example typechecks cleanly and Bar.init is revealed as accepting Iterable[str].
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
- 35/100