8000 ENH: [typing] `NDarray[plt.Axes]` not possible - Type argument of "NDArray" must be a subtype of "generic" · Issue #24738 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
ENH: [typing] NDarray[plt.Axes] not possible - Type argument of "NDArray" must be a subtype of "generic" #24738
Open
@randolf-scholz

Description

@randolf-scholz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0