Skip to content

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

Closed

Description

@stefanv

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

Activity

  1. rcomer commented on Dec 17, 2024

    @rcomer
    Member

    Duplicate of #27455 I think.

  2. stefanv commented on Dec 18, 2024

    @stefanv
    ContributorAuthor

    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.

  3. added this to the v3.11.0 milestone on Dec 24, 2024
  4. tacaswell commented on Dec 24, 2024

    @tacaswell
    Member

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

  5. QuLogic commented on Dec 24, 2024

    @QuLogic
    Member

    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...

  6. scottshambaugh commented on Jan 24, 2025

    @scottshambaugh
    Contributor

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

  7. stefanv commented on Jan 24, 2025

    @stefanv
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions