Skip to content

[Bug]: typing.get_type_hints fails for multiple pyplot plotting functions #28977

@YoraiLevi

Description

@YoraiLevi

Bug summary

typing.get_type_hints throws error name 'ArrayLike' is not defined for pyplot functions such as plot,scatter,imshow etc.

Code for reproduction

%pip install -U matplotlib # colab
import typing
import matplotlib.pyplot
import matplotlib.typing # sanity check, typing exist

print(matplotlib.pyplot.imshow.__annotations__)
typing.get_type_hints(matplotlib.pyplot.imshow)

Actual outcome

{'X': 'ArrayLike | PIL.Image.Image', 'cmap': 'str | Colormap | None', 'norm': 'str | Normalize | None', 'aspect': "Literal['equal', 'auto'] | float | None", 'interpolation': 'str | None', 'alpha': 'float | ArrayLike | None', 'vmin': 'float | None', 'vmax': 'float | None', 'origin': "Literal['upper', 'lower'] | None", 'extent': 'tuple[float, float, float, float] | None', 'interpolation_stage': "Literal['data', 'rgba'] | None", 'filternorm': 'bool', 'filterrad': 'float', 'resample': 'bool | None', 'url': 'str | None', 'return': 'AxesImage'}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[<ipython-input-6-616de5754b17>](https://localhost:8080/#) in <cell line: 7>()
      5 
      6 print(matplotlib.pyplot.imshow.__annotations__)
----> 7 typing.get_type_hints(matplotlib.pyplot.imshow)

3 frames
/usr/local/lib/python3.10/dist-packages/matplotlib/pyplot.py in <module>

NameError: name 'ArrayLike' is not defined

Expected outcome

a dictionary with correctly populated classes, no error

from numpy.typing import ArrayLike

def imshow(X: ArrayLike):
    pass

typing.get_type_hints(imshow)
{'X': typing.Union[numpy._typing._array_like._SupportsArray[numpy.dtype[typing.Any]], numpy._typing._nested_sequence._NestedSequence[numpy._typing._array_like._SupportsArray[numpy.dtype[typing.Any]]], bool, int, float, complex, str, bytes, numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes]]}

Additional information

when using colab matplotlib needs to be upgraded to include typing annotations

Operating system

Ubuntu

Matplotlib Version

3.9.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.10.12

Jupyter version

6.5.5

Installation

pip

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