Skip to content

ENH: [typing] NDarray[plt.Axes] not possible - Type argument of "NDArray" must be a subtype of "generic" #24738

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

Open
randolf-scholz opened this issue Sep 18, 2023 · 4 comments

Comments

@randolf-scholz
Copy link

randolf-scholz commented Sep 18, 2023

Proposed new feature or change:

For numpy.typing.NDArray, the argument needs to be a subtype of numpy.generic. However, this leads to problem since we can no longer specify concrete types for object-arrays, for example, mypy raises [type-var] for this code:

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from numpy.typing import NDArray

axes: NDArray[Axes]  # ✘ mypy: raises type-var
fig: Figure
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)

for ax in axes.flat:
    reveal_type(axes)  # Axes if the previous error is ignored, else ndarray
    ax.set_title("A subplot")

For NDArray of dtype np.object_, it should somehow wrap the given type. One approach could be to make np.object_ generic and change methods overloads so that for object-type, it generally returns the wrapped type.

@ksunden
Copy link
Contributor

ksunden commented Sep 18, 2023

In type hinting Matplotlib, I stopped short of specifying the type in subplots (Figure.subplots only says np.ndarray, pyplot.subplots actually just does Any)

In most cases adding that specificity was just too much to do all at once so I left it for follow up. I don't specifically recall trying to specify here, but it is possible I did and ran into this then.

@BvB93
Copy link
Member

BvB93 commented Sep 20, 2023

For NDArray of dtype np.object_, it should somehow wrap the given type. One approach could be to make np.object_ generic and change methods overloads so that for object-type, it generally returns the wrapped type.

It's a feature on the wish list, but yeah this will in all likelihood be the most feasible and sensible approach here. I do foresee one limitation though that I sincerely doubt we'd be to handle satisfactory: type promotions between arrays with different object dtypes (e.g. an object array wrapping builtins.int and one wrapping builtins.float for example). Still, this shouldn't really be an issue as long as you're only mixing np.object_[plt.Axes] object arrays with each other for example.

@randolf-scholz
Copy link
Author

Is there any workaround for this atm? I also run in this problem when trying to do NDArray[T] where T is a TypeVar bound to a custom Protocol type, which is very annoying...

@jorenham
Copy link
Member

jorenham commented Jul 14, 2024

Note that numpy.typing.NDArray[T: numpy.generic] is an alias for numpy.ndarray[Any, numpy.dtype[T]].

The first type parameter of numpy.ndarray is meant to be used for shape typing.
Currently, it accepts any type, but that is likely to be changed in the future to tuple[int, ...], see #26081.

See #16544 for an in-depth discussion on array shape typing.


FYI, the optype typing library provides a type alias optype.numpy.Array, which similar to numpy.typing.NDArray, but has two type parameters: one for the shape, and one for the scalar type. See the documentation for examples and details.

craymichael added a commit to craymichael/captum that referenced this issue Oct 25, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 25, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 25, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 25, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
craymichael added a commit to craymichael/captum that referenced this issue Oct 26, 2024
Summary:

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy


Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Differential Revision: D64998799
facebook-github-bot pushed a commit to pytorch/captum that referenced this issue Oct 29, 2024
Summary:
Pull Request resolved: #1416

visualize_image_attr_multiple can return a List[Axes], adds proper annotations to satisfy mypy

Why casting is necessary: numpy/numpy#24738

https://github.com/matplotlib/matplotlib/blob/v3.9.2/lib/matplotlib/pyplot.py#L1583C41-L1584C1

Reviewed By: cyrjano

Differential Revision: D64998799

fbshipit-source-id: 7b2249654f5eb784f6c0686160f78f1be61bf7a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants