Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TYP] Type changes from running against Pandas #26883

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

Merged
merged 3 commits into from
Oct 7, 2023

Conversation

ksunden
Copy link
Member

@ksunden ksunden commented Sep 22, 2023

PR summary

This is a few typing related changes identified by running against Pandas

  • Making ArtistList generic so that Axes properties can be type narrowed
  • Adding missed attributes to Axes
    • These are added in __clear, rather than __init__ directly, so they are available on all Axes, but were missed when looking for attrs
  • Add a __all__ to axes/__init__.pyi
  • Edit how axes/__init__.pyi handles imports/re-exporting Subplot
    • Only added to the pyi stub, not runtime
    • Subplot was not recognized as exported because it was not a redundant import
  • Add _AxisWrapper to type hint for TickHelper.set_axis
    • A more extensive fix for this may be warranted, as we are leaking private classes in the type hint
    • AxisWrapper is set in Polar code and flags if absent here
    • this just ensures that x.set_axis(x.axis) type checks as OK
    • More extensive approach would perhaps to use a Protocol class to dynamically ducktype rather than having concrete types here
      • Or a type alias (either to the union that includes those or to Any

PR checklist

@ksunden
Copy link
Member Author

ksunden commented Sep 22, 2023

Apparently stubtest doesn't like that I put __all__ only in the pyi... I was trying to avoid changing the .py file here because I kind of want to backport it to 3.8.1... and would prefer not to make that change to py/from matplotlib.axes import * behavior in a patch release (I'm relatively okay with doing it for stubs, especially since those don't export all imports by default)

@@ -1,10 +1,12 @@
from typing import TypeVar

from ._axes import *
from ._axes import Axes as Subplot
from ._axes import Axes as Axes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since that's the only thing in there, should we change the import in the .py file, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, logically yes, but technically at runtime more gets imported there, just not things we intend to re-export.

I was being a bit conservative here and only changing stub behavior (especially since the stubs didn't pick up Subplot as being exported, when it should be)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think __init__.py files (broadly) are good candidates for inlining type hints and just not having separate files: they are short, contain little to no actual function definitions, it's mostly imports and __all__ (which I'm also in favor of adding in more places)

Comment on lines +59 to +61
containers: list[Container]
callbacks: CallbackRegistry
child_axes: list[_AxesBase]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at __clear, I see also legend_ and title that aren't here; should they be added as well? (The former appears in axes.pyi on Axes)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say those ones (and perhaps several of the existing attrs) should be made private (title there is already _[left|right]_title which are private, though the getters/setters are defined in _axes.py, not _base.py, for some reason, and not defined for secondary_axes, even though the attrs exist)

But I'll add them since they are by naming convention public (though I will also note that none of these attrs (aside from dataLim and viewLim) actually appear in the rendered docs, though several (trans[Axes,Scale,Limits,Data] come to mind) are referred to by examples, etc. Is that something we want to include in the rendered docs? cc @timhoffm, this is reminiscent of the discussions regarding plt.Axes/plt.Figure, but is different in that these are not imports and are instance attributes)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ax.title is used in several tests as well as the lifecycle tutorial and tight_layout_guide

axes.legend_ is used outside of the axes module only in backend/qt_editor/figureoptions.py as far as I can tell through quick greps (and never in documentation)

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, if @timhoffm is good with the additional items.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instance attributes are technically public. Documenting the types is consistent with our current public API. It’s a separate question, whether they should be public, and IMHO many shouldn’t. But that has to go through the deprecation cycle.

@timhoffm timhoffm added this to the v3.8.1 milestone Oct 7, 2023
@timhoffm timhoffm merged commit d908b4f into matplotlib:main Oct 7, 2023
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Oct 7, 2023
QuLogic added a commit that referenced this pull request Oct 7, 2023
…883-on-v3.8.x

Backport PR #26883 on branch v3.8.x ([TYP] Type changes from running against Pandas)
@ksunden ksunden mentioned this pull request Nov 2, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants