8000 [Bug]: Mypy not finding the right return type for ax.subplots(...,projection="3d") · Issue #27455 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: Mypy not finding the right return type for ax.subplots(...,projection="3d") #27455
Closed as not planned
@jmdelaney8

Description


7178
@jmdelaney8

Bug summary

Mypy says that an ax created by ax = fig.add_subplot(..., projection="3d") is type Axes rather than the expected Axes3D. This causes type checking Axes3D methods to fail e.g. ax.plot_surface(). Maybe this is fixed if/when mpl_toolkits gets typing stubs?

Code for reproduction

import numpy as np
from matplotlib import pyplot as plt

# Demo data
x, y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100))
z = np.sqrt(x**2 + y**2)

fig = plt.figure()
ax = fig.add_subplot(1, 2, 1, projection="3d")
reveal_type(ax)  # matplotlib.axes._axes.Axes
ax.plot_surface(x, y, z)  # mypy error: "Axes" has no attribute "plot_surface"  [attr-defined]
plt.show()

Actual outcome

python3.9 -m mypy demo.py produces:

demo.py:10: note: Revealed type is "matplotlib.axes._axes.Axes"
demo.py:11: error: "Axes" has no attribute "plot_surface"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Expected outcome

Expected no attr-defined mypy error for calling ax.surface() on an Axes3D`

demo.py:10: note: Revealed type is "mpl_toolkits.mplot3d.axes3d.Axes3D"

Additional information

This occurs with the latest mypy (1.7.1)

Operating system

Ubuntu 20.04

Matplotlib Version

3.8.2

Matplotlib Backend

TKAgg

Python version

Python 3.9.5

Jupyter version

No response

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

      0