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

Error from mypy when using converters in a list #1280

Closed
my1e5 opened this issue Apr 24, 2024 · 6 comments
Closed

Error from mypy when using converters in a list #1280

my1e5 opened this issue Apr 24, 2024 · 6 comments

Comments

@my1e5
Copy link
Contributor

my1e5 commented Apr 24, 2024

When I use multiple callables with the converter argument in field, I get an error from mypy.

Here is a simple example of what I'm trying to do.

# main.py
import numpy as np
import numpy.typing as npt
from attrs import field, define

@define
class Foo:
    x: npt.NDArray[np.float64] = field(converter=[np.array, np.float64])


foo = Foo(x=[1,2,3])

print(type(foo.x), foo.x.dtype)
$ python main.py
<class 'numpy.ndarray'> float64

But mypy (version 1.9.0) gives me an error:

$ mypy main.py
main.py:8: error: No overload variant of "field" matches argument type "list[object]"  [call-overload]
main.py:8: note: Possible overload variants:
main.py:8: note:     def field(*, default: None = ..., validator: None = ..., repr: bool | Callable[[Any], str] = ..., hash: bool | None = ..., init: bool = ..., metadata: Mapping[Any, Any] | None = ..., converter: None = ..., factory: None = ..., kw_only: bool = ..., eq: bool | None = ..., order: bool | None = ..., on_setattr: Callable[[Any, Attribute[Any], Any], Any] | list[Callable[[Any, Attribute[Any], Any], Any]] | _NoOpType | None = ..., 
alias: str | None = ..., type: type | None = ...) -> Any
main.py:8: note:     def [_T] field(*, default: None = ..., validator: Callable[[Any, Attribute[_T], _T], Any] | Sequence[Callable[[Any, Attribute[_T], _T], Any]] | None = ..., repr: bool | Callable[[Any], str] = ..., hash: 
bool | None = ..., init: bool = ..., metadata: Mapping[Any, Any] | None = ..., converter: Callable[[Any], Any] | None = ..., factory: Callable[[], _T] | None = ..., kw_only: bool = ..., eq: bool | Callable[[Any], Any] | None = ..., order: bool | Callable[[Any], Any] | None = ..., on_setattr: Callable[[Any, Attribute[Any], Any], Any] | list[Callable[[Any, Attribute[Any], Any], Any]] | _NoOpType | None = ..., alias: str | None = ..., type: type | None = ...) -> _T
main.py:8: note:     def [_T] field(*, default: _T, validator: Callable[[Any, Attribute[_T], _T], Any] | Sequence[Callable[[Any, Attribute[_T], _T], Any]] | None = ..., repr: bool | Callable[[Any], str] = ..., hash: bool | None = ..., init: bool = ..., metadata: Mapping[Any, Any] | None = ..., converter: Callable[[Any], Any] | None = ..., factory: Callable[[], _T] | None = ..., kw_only: bool = ..., eq: bool | Callable[[Any], Any] | None = ..., 
order: bool | Callable[[Any], Any] | None = ..., on_setattr: Callable[[Any, Attribute[Any], Any], Any] | list[Callable[[Any, Attribute[Any], Any], Any]] | _NoOpType | None = ..., alias: str | None = ..., type: type | None = 
...) -> _T
main.py:8: note:     def [_T] field(*, default: _T | None = ..., validator: Callable[[Any, Attribute[_T], _T], Any] | Sequence[Callable[[Any, Attribute[_T], _T], Any]] | None = ..., repr: bool | Callable[[Any], str] = ..., hash: bool | None = ..., init: bool = ..., metadata: Mapping[Any, Any] | None = ..., converter: Callable[[Any], Any] | None = ..., factory: Callable[[], _T] | None = ..., kw_only: bool = ..., eq: bool | Callable[[Any], Any] | None = ..., order: bool | Callable[[Any], Any] | None = ..., on_setattr: Callable[[Any, Attribute[Any], Any], Any] | list[Callable[[Any, Attribute[Any], Any], Any]] | _NoOpType | None = ..., alias: str | None = ..., type: type | None = ...) -> Any
main.py:8: error: Unsupported converter, only named functions, types and lambdas are currently supported  [misc]
Found 2 errors in 1 file (checked 1 source file)

Not sure why or if this is fixable.

@byte2sys
Copy link

I will look into it and try my best to troubleshoot and debug this code :)

@my1e5
Copy link
Contributor Author

my1e5 commented Apr 25, 2024

The [misc] error may be related to this discussion - python/mypy#6172

@hynek
Copy link
Member

hynek commented Apr 25, 2024

Yes this is most likely due to Mypy‘s limitations unfortunately.

@my1e5
Copy link
Contributor Author

my1e5 commented Apr 25, 2024

Should I report the bug on Mypy's github?

@hynek
Copy link
Member

hynek commented Apr 25, 2024

I’m pretty sure there’s a ticket there and/or here but I’m too swamped rn to do the research. Unfortunately, it’s a rather recurring topic.

@hynek
Copy link
Member

hynek commented May 8, 2024

Closing it now; if it turns out to be a new problem, feel free to re-open with a rationale.

@hynek hynek closed this as not planned Won't fix, can't repro, duplicate, stale May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants