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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/matplotlib/axes/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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)



_T = TypeVar("_T")

# Backcompat.
Subplot = Axes

class _SubplotBaseMeta(type):
def __instancecheck__(self, obj) -> bool: ...

Expand Down
37 changes: 22 additions & 15 deletions lib/matplotlib/axes/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from matplotlib import cbook
from matplotlib.artist import Artist
from matplotlib.axis import XAxis, YAxis, Tick
from matplotlib.backend_bases import RendererBase, MouseButton, MouseEvent
from matplotlib.cbook import CallbackRegistry
from matplotlib.container import Container
from matplotlib.collections import Collection
from matplotlib.cm import ScalarMappable
Expand All @@ -25,9 +26,11 @@ from cycler import Cycler

import numpy as np
from numpy.typing import ArrayLike
from typing import Any, Literal, overload
from typing import Any, Literal, TypeVar, overload
from matplotlib.typing import ColorType

_T = TypeVar("_T", bound=Artist)

class _axis_method_wrapper:
attr_name: str
method_name: str
Expand All @@ -53,6 +56,11 @@ class _AxesBase(martist.Artist):
transData: Transform
ignore_existing_data_limits: bool
axison: bool
containers: list[Container]
callbacks: CallbackRegistry
child_axes: list[_AxesBase]
Comment on lines +59 to +61
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)

legend_: Legend | None
title: Text
_projection_init: Any

def __init__(
Expand Down Expand Up @@ -125,8 +133,7 @@ class _AxesBase(martist.Artist):
def clear(self) -> None: ...
def cla(self) -> None: ...

# Could be made generic, but comments indicate it may be temporary anyway
class ArtistList(Sequence[Artist]):
class ArtistList(Sequence[_T]):
def __init__(
self,
axes: _AxesBase,
Expand All @@ -135,40 +142,40 @@ class _AxesBase(martist.Artist):
invalid_types: type | Iterable[type] | None = ...,
) -> None: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[Artist]: ...
def __iter__(self) -> Iterator[_T]: ...
@overload
def __getitem__(self, key: int) -> Artist: ...
def __getitem__(self, key: int) -> _T: ...
@overload
def __getitem__(self, key: slice) -> list[Artist]: ...
def __getitem__(self, key: slice) -> list[_T]: ...

@overload
def __add__(self, other: _AxesBase.ArtistList) -> list[Artist]: ...
def __add__(self, other: _AxesBase.ArtistList[_T]) -> list[_T]: ...
@overload
def __add__(self, other: list[Any]) -> list[Any]: ...
@overload
def __add__(self, other: tuple[Any]) -> tuple[Any]: ...

@overload
def __radd__(self, other: _AxesBase.ArtistList) -> list[Artist]: ...
def __radd__(self, other: _AxesBase.ArtistList[_T]) -> list[_T]: ...
@overload
def __radd__(self, other: list[Any]) -> list[Any]: ...
@overload
def __radd__(self, other: tuple[Any]) -> tuple[Any]: ...

@property
def artists(self) -> _AxesBase.ArtistList: ...
def artists(self) -> _AxesBase.ArtistList[Artist]: ...
@property
def collections(self) -> _AxesBase.ArtistList: ...
def collections(self) -> _AxesBase.ArtistList[Collection]: ...
@property
def images(self) -> _AxesBase.ArtistList: ...
def images(self) -> _AxesBase.ArtistList[AxesImage]: ...
@property
def lines(self) -> _AxesBase.ArtistList: ...
def lines(self) -> _AxesBase.ArtistList[Line2D]: ...
@property
def patches(self) -> _AxesBase.ArtistList: ...
def patches(self) -> _AxesBase.ArtistList[Patch]: ...
@property
def tables(self) -> _AxesBase.ArtistList: ...
def tables(self) -> _AxesBase.ArtistList[Table]: ...
@property
def texts(self) -> _AxesBase.ArtistList: ...
def texts(self) -> _AxesBase.ArtistList[Text]: ...
def get_facecolor(self) -> ColorType: ...
def set_facecolor(self, color: ColorType | None) -> None: ...
@overload
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/ticker.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _DummyAxis:

class TickHelper:
axis: None | Axis | _DummyAxis | _AxisWrapper
def set_axis(self, axis: Axis | _DummyAxis | None) -> None: ...
def set_axis(self, axis: Axis | _DummyAxis | _AxisWrapper | None) -> None: ...
def create_dummy_axis(self, **kwargs) -> None: ...

class Formatter(TickHelper):
Expand Down