attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Error from mypy when using converters in a list

Open my1e5 opened this issue 10 months ago • 5 comments

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.

my1e5 avatar Apr 24 '24 14:04 my1e5

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

byte2sys avatar Apr 25 '24 01:04 byte2sys

The [misc] error may be related to this discussion - https://github.com/python/mypy/issues/6172

my1e5 avatar Apr 25 '24 08:04 my1e5

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

hynek avatar Apr 25 '24 09:04 hynek

Should I report the bug on Mypy's github?

my1e5 avatar Apr 25 '24 09:04 my1e5

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 avatar Apr 25 '24 12:04 hynek

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

hynek avatar May 08 '24 08:05 hynek