Skip to content

[Bug]: Bad type hint in _AxesBase.twinx() #28624

Closed

Description

@randolf-scholz

Bug summary

Incorrect type annotation for _AxesBase.twinx() (should be Self, not _AxesBase).

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

# First create some toy data:
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x**2)
z = np.cos(x**2)

# Create four polar Axes and access them through the returned array
fig, axsA = plt.subplots()
axsA.plot(x, y)
axsB = axsA.twinx()
axsB.plot(x, z)  # <-- Cannot access attribute "plot" for class "_AxesBase"

Actual outcome

axsB is of type _AxesBase

Expected outcome

The type of axsB should be the same as the type of axsA.

Matplotlib Version

3.9.1

Activity

  1. randolf-scholz commented on Jul 29, 2024

    @randolf-scholz
    ContributorAuthor

    Potentially fixing this requires adding typing_extensions as a dependency, since typing.Self is not available in 3.10.
    Otherwise, users might not have it in their virtual environment when installing matplotlib, leaving the stubs broken.

  2. randolf-scholz commented on Jul 29, 2024

    @randolf-scholz
    ContributorAuthor

    I made a provisional PR #28625, maintainers likely want to discuss first whether to add the extra dependency. Although imo typing_extensions is kind of must-have if you want to type the library as it is the official place for backporting new typing features.

  3. added this to the v3.9.2 milestone on Aug 1, 2024
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