8000 [Bug]: Type annotation for `add_subplots` has incorrect return type for `projection="3d"` · Issue #29334 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
8000

[Bug]: Type annotation for add_subplots has incorrect return type for projection="3d" #29334

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
Tracked by #813
stefanv opened this issue Dec 17, 2024 · 6 comments
Closed
Tracked by #813
Milestone

Comments

@stefanv
Copy link
Contributor
stefanv commented Dec 17, 2024

Bug summary

The return type for add_subplots with projection="3d" should be Axes3D.

The current implementation is:

    @overload
    def add_subplot(
        self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs
    ) -> Axes: ...
    @overload
    def add_subplot(self, pos: int, **kwargs) -> Axes: ...
    @overload
    def add_subplot(self, ax: Axes, **kwargs) -> Axes: ...
    @overload
    def add_subplot(self, ax: SubplotSpec, **kwargs) -> Axes: ...
    @overload
    def add_subplot(self, **kwargs) -> Axes: ...
    @overload
    def subplots(

Code for reproduction

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
ax: Axes3D = plt.figure().add_subplot(projection="3d")

Actual outcome

Pyright complains:

error: Type "Axes" is not assignable to declared type "Axes3D"
    "Axes" is not assignable to "Axes3D" (reportAssignmentType)

Expected outcome

No error.

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.0

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@rcomer
Copy link
Member
rcomer commented Dec 17, 2024

Duplicate of #27455 I think.

@stefanv
Copy link
Contributor Author
stefanv commented Dec 18, 2024

I'd need some feedback on #29341 from y'all who know more than I do about type annotation, but the PoC PR at least fixes the problem for me.

@tacaswell tacaswell added this to the v3.11.0 milestone Dec 24, 2024
@tacaswell
Copy link
Member

To be slightly picky, it is a correct but less specific base type.

@QuLogic
Copy link
Member
QuLogic commented Dec 24, 2024

The wider problem is that there are no type hints for any of the mpl_toolkits, so even if we annotate an Axes3D here, it won't help you any...

@scottshambaugh
Copy link
Contributor

This is a wider problem with typing in Axes3D, but this does seem like low hanging fruit

@stefanv
Copy link
Contributor Author
stefanv commented Jan 24, 2025

The wider problem is that there are no type hints for any of the mpl_toolkits, so even if we annotate an Axes3D here, it won't help you any...

This did solve my specific problem, FWIW: the matplotlib examples in the SP lecture notes.

@stefanv stefanv closed this as completed May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
0