Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
In testing type-checking integration between matplotlib 3.8.0rc1 andcmyt, I found mypy flagged a couple errors that I think may be spurious ?
Here's a minimal reproducer of the issue I'm talking about.
Code for reproduction
# t.py , linted with `mypy t.py`frommatplotlib.colorsimportColormapimportnumpyasnpdefrgb_from_cmap(cmap:Colormap)->np.ndarray:RGBA=cmap(np.linspace(0,1,155))RGB=RGBA[:, :3]returnRGB
Actual outcome
t.py: note: In function "rgb_from_cmap":t.py:5: error: No overload variant of "__getitem__" of "tuple" matches argument type "Tuple[slice, slice]" [call-overload]t.py:5: note: Possible overload variants:t.py:5: note: def __getitem__(self, SupportsIndex, /) -> floatt.py:5: note: def __getitem__(self, slice, /) -> Tuple[float, ...]t.py:6: error: Incompatible return value type (got "Union[float, ndarray[Any, Any]]", expected "ndarray[Any, Any]") [return-value]Found 2 errors in 1 file (checked 1 source file)
Expected outcome
no error. The second one is easy enough to handle in user code by returningnp.asarray(RGB)
(though I am not 100% sure that this should be necessary), but the first error really feels undesired to me;
Quoting fromColormap.__call__
's docstring:
Returns ------- Tuple of RGBA values if X is scalar, otherwise an array of RGBA values with a shape of ``X.shape + (4, )``.
it should be clear that the return value is necessarily an array withndim >= 2
in my case ?
I think the problem is that the return type of this function is annotated as a union type
matplotlib/lib/matplotlib/colors.pyi
Line 74 indd09194
)->tuple[float,float,float,float]|np.ndarray: ... |
However it's not clear to me how to fix it.
Additional information
previous versions of matplotlib "type-checked" correctly (because everything was assumed to beAny
!)
Operating system
ubuntu
Matplotlib Version
3.8.0rc1
Matplotlib Backend
N/A
Python version
3.11.4
Jupyter version
N/A
Installation
pip