Skip to content

[ENH]: Improve typing of Figure.subplots #28045

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

Closed
bmerry opened this issue Apr 8, 2024 · 1 comment
Closed

[ENH]: Improve typing of Figure.subplots #28045

bmerry opened this issue Apr 8, 2024 · 1 comment
Milestone

Comments

@bmerry
Copy link

bmerry commented Apr 8, 2024

Bug summary

The type stubs for Figure.subplots has two @overloads. One is generic, and the other is specialised for squeeze=False (in which case the return value is always nd.ndarray). But there are also some other cases where the type could be more accurately inferred.

Code for reproduction

from matplotlib.figure import Figure

fig = Figure()
reveal_type(fig.subplots())
reveal_type(fig.subplots(2))
reveal_type(fig.subplots(1, 2))

Actual outcome

When run in mypy:

infer.py:4: note: Revealed type is "Union[numpy.ndarray[Any, Any], matplotlib.axes.SubplotBase, matplotlib.axes._axes.Axes]"
infer.py:5: note: Revealed type is "Union[numpy.ndarray[Any, Any], matplotlib.axes.SubplotBase, matplotlib.axes._axes.Axes]"
infer.py:6: note: Revealed type is "Union[numpy.ndarray[Any, Any], matplotlib.axes.SubplotBase, matplotlib.axes._axes.Axes]"

Expected outcome

infer.py:4: note: Revealed type is "Union[matplotlib.axes.SubplotBase, matplotlib.axes._axes.Axes]"
infer.py:5: note: Revealed type is "numpy.ndarray[Any, Any]]"
infer.py:6: note: Revealed type is "numpy.ndarray[Any, Any]]"

Additional information

The last two examples may be impossible to implement, because you'd need a way to type "a literal integer != 1". But the first case is the one I initially ran into. I was able to work around it by replacing it with fig.add_subplot(), but that isn't semantically identical.

Operating system

Ubuntu

Matplotlib Version

3.8.0

Matplotlib Backend

TkAgg

Python version

3.10.12

Jupyter version

No response

Installation

pip

bmerry added a commit to ska-sa/katgpucbf that referenced this issue Apr 8, 2024
The bulk of the errors were because matplotlib doesn't have particularly
good type annotations for `Figure.subplots`
(matplotlib/matplotlib#28045).
@tacaswell tacaswell added this to the v3.10.0 milestone Apr 9, 2024
@QuLogic
Copy link
Member

QuLogic commented Sep 21, 2024

I think this was already fixed by #27001.

@QuLogic QuLogic closed this as completed Sep 21, 2024
@QuLogic QuLogic modified the milestones: v3.10.0, v3.9.1 Sep 21, 2024
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

3 participants