Skip to content

attr.ib(converter=) appears to cause mypy to lose generic types for __init__ function. #8417

Open
@rowillia

Description

@rowillia
$ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions