Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor handling of attrs converters. #9354

Open
alanhdu opened this issue Aug 25, 2020 · 2 comments
Open

Poor handling of attrs converters. #9354

alanhdu opened this issue Aug 25, 2020 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-attrs

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Aug 25, 2020

When running mypy over the following file:

from typing import Sequence
import attr

@attr.s
class A:
    a: Sequence[str] = attr.ib(converter=list)
A(["a"])

I get:

$ mypy a.py
a.py:7: error: List item 0 has incompatible type "str"; expected "_T"

which is a little confusing, because str should work fine here. This is with mypy 0.782 and on Python 3.6.

@Akuli
Copy link
Contributor

Akuli commented Aug 26, 2020

Does it work if you use converter=(lambda thing: list(thing)) instead of converter=list? If it works, then this might be related to #9253

@alanhdu
Copy link
Contributor Author

alanhdu commented Aug 26, 2020

@Akuli When I use a lambda, mypy complains with

a.py:6: error: Unsupported converter, only named functions and types are currently supported

When I try to use a named generic:

from typing import Sequence, List, TypeVar
import attr

T = TypeVar("T")
def convert(x: Sequence[T]) -> List[T]:
    return list(x)

@attr.s
class A:
    a: Sequence[str] = attr.ib(converter=convert)
A(["a"])

then mypy errors with:

a.py:5: error: Type variable "a.T" is unbound
a.py:5: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
a.py:5: note: (Hint: Use "T" in function signature to bind "T" inside a function)
Found 1 error in 1 file (checked 1 source file)

This does work if I "monomorphize" convert to go from Sequence[str] to List[str] though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-attrs
Projects
None yet
Development

No branches or pull requests

3 participants