From a5705123d3180f8dd5543ee37b9fc0496a7f67a8 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 5 Apr 2023 16:06:14 -0500 Subject: [PATCH 1/2] Include data kwarg in pyi stubs Allows for stubtest to check the consistency of many prominent user facing methods Currently no type hint is provided for data, a follow up issue will be filed detailing what needs to happen to make that have a useful type hint. --- ci/mypy-stubtest-allowlist.txt | 38 ----------------- lib/matplotlib/axes/_axes.pyi | 74 +++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 49 deletions(-) diff --git a/ci/mypy-stubtest-allowlist.txt b/ci/mypy-stubtest-allowlist.txt index 7adc231ed557..cbbc13ba4c20 100644 --- a/ci/mypy-stubtest-allowlist.txt +++ b/ci/mypy-stubtest-allowlist.txt @@ -17,44 +17,6 @@ matplotlib.*\.set$ matplotlib.pyplot.* matplotlib.typing.* -# _preprocess_data arg decorator -matplotlib.axes.*Axes.acorr -matplotlib.axes.*Axes.angle_spectrum -matplotlib.axes.*Axes.bar -matplotlib.axes.*Axes.barbs -matplotlib.axes.*Axes.boxplot -matplotlib.axes.*Axes.broken_barh -matplotlib.axes.*Axes.cohere -matplotlib.axes.*Axes.contour -matplotlib.axes.*Axes.contourf -matplotlib.axes.*Axes.csd -matplotlib.axes.*Axes.ecdf -matplotlib.axes.*Axes.errorbar -matplotlib.axes.*Axes.eventplot -matplotlib.axes.*Axes.fill_between -matplotlib.axes.*Axes.fill_betweenx -matplotlib.axes.*Axes.hexbin -matplotlib.axes.*Axes.hist -matplotlib.axes.*Axes.hist2d -matplotlib.axes.*Axes.hlines -matplotlib.axes.*Axes.imshow -matplotlib.axes.*Axes.magnitude_spectrum -matplotlib.axes.*Axes.pcolor -matplotlib.axes.*Axes.pcolorfast -matplotlib.axes.*Axes.pcolormesh -matplotlib.axes.*Axes.phase_spectrum -matplotlib.axes.*Axes.pie -matplotlib.axes.*Axes.plot_date -matplotlib.axes.*Axes.psd -matplotlib.axes.*Axes.quiver -matplotlib.axes.*Axes.scatter -matplotlib.axes.*Axes.specgram -matplotlib.axes.*Axes.stairs -matplotlib.axes.*Axes.stem -matplotlib.axes.*Axes.violinplot -matplotlib.axes.*Axes.vlines -matplotlib.axes.*Axes.xcorr - # Other decorator modifying signature (maybe investigate) matplotlib.axis.Axis.draw matplotlib.offsetbox.*Offset[Bb]ox.get_offset diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index 15dc1cf25585..94536046df49 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -166,6 +166,8 @@ class Axes(_AxesBase): colors: Sequence[ColorType] | None = ..., linestyles: LineStyleType = ..., label: str = ..., + *, + data=..., **kwargs ) -> LineCollection: ... def vlines( @@ -176,6 +178,8 @@ class Axes(_AxesBase): colors: Sequence[ColorType] | None = ..., linestyles: LineStyleType = ..., label: str = ..., + *, + data=..., **kwargs ) -> LineCollection: ... def eventplot( @@ -188,6 +192,8 @@ class Axes(_AxesBase): colors: ColorType | Sequence[ColorType] | None = ..., alpha: float | Sequence[float] | None = ..., linestyles: LineStyleType | Sequence[LineStyleType] = ..., + *, + data=..., **kwargs ) -> EventCollection: ... def plot( @@ -195,7 +201,7 @@ class Axes(_AxesBase): *args: float | ArrayLike | str, scalex: bool = ..., scaley: bool = ..., - data: Any | None = ..., + data = ..., **kwargs ) -> list[Line2D]: ... def plot_date( @@ -206,13 +212,15 @@ class Axes(_AxesBase): tz: str | datetime.tzinfo | None = ..., xdate: bool = ..., ydate: bool = ..., + *, + data=..., **kwargs ) -> list[Line2D]: ... def loglog(self, *args, **kwargs) -> list[Line2D]: ... def semilogx(self, *args, **kwargs) -> list[Line2D]: ... def semilogy(self, *args, **kwargs) -> list[Line2D]: ... def acorr( - self, x: ArrayLike, **kwargs + self, x: ArrayLike, *, data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, LineCollection | Line2D, Line2D | None]: ... def xcorr( self, @@ -222,6 +230,8 @@ class Axes(_AxesBase): detrend: Callable[[ArrayLike], ArrayLike] = ..., usevlines: bool = ..., maxlags: int = ..., + *, + data = ..., **kwargs ) -> tuple[np.ndarray, np.ndarray, LineCollection | Line2D, Line2D | None]: ... def step( @@ -230,7 +240,7 @@ class Axes(_AxesBase): y: ArrayLike, *args, where: Literal["pre", "post", "mid"] = ..., - data: Any | None = ..., + data = ..., **kwargs ) -> list[Line2D]: ... def bar( @@ -241,6 +251,7 @@ class Axes(_AxesBase): bottom: float | ArrayLike | None = ..., *, align: Literal["center", "edge"] = ..., + data = ..., **kwargs ) -> BarContainer: ... def barh( @@ -251,7 +262,7 @@ class Axes(_AxesBase): left: float | ArrayLike | None = ..., *, align: Literal["center", "edge"] = ..., - data: Any | None = ..., + data = ..., **kwargs ) -> BarContainer: ... def bar_label( @@ -268,6 +279,8 @@ class Axes(_AxesBase): self, xranges: Sequence[tuple[float, float]], yrange: tuple[float, float], + *, + data=..., **kwargs ) -> BrokenBarHCollection: ... def stem( @@ -278,7 +291,8 @@ class Axes(_AxesBase): basefmt: str | None = ..., bottom: float = ..., label: str | None = ..., - orientation: Literal["vertical", "horizontal"] = ... + orientation: Literal["vertical", "horizontal"] = ..., + data=..., ) -> StemContainer: ... # TODO: data kwarg preprocessor? @@ -302,7 +316,8 @@ class Axes(_AxesBase): rotatelabels: bool = ..., *, normalize: bool = ..., - hatch: str | Sequence[str] | None = ... + hatch: str | Sequence[str] | None = ..., + data=..., ): ... def errorbar( self, @@ -321,6 +336,8 @@ class Axes(_AxesBase): xuplims: bool = ..., errorevery: int | tuple[int, int] = ..., capthick: float | None = ..., + *, + data=..., **kwargs ) -> ErrorbarContainer: ... def boxplot( @@ -352,6 +369,8 @@ class Axes(_AxesBase): autorange: bool = ..., zorder: float | None = ..., capwidths: float | ArrayLike | None = ..., + *, + data=..., ) -> dict[str, Any]: ... def bxp( self, @@ -392,6 +411,7 @@ class Axes(_AxesBase): *, edgecolors: Literal["face", "none"] | ColorType | Sequence[ColorType] | None = ..., plotnonfinite: bool = ..., + data=..., **kwargs ) -> PathCollection: ... def hexbin( @@ -414,6 +434,8 @@ class Axes(_AxesBase): reduce_C_function: Callable[[np.ndarray], float] = ..., mincnt: int | None = ..., marginals: bool = ..., + *, + data=..., **kwargs ) -> PolyCollection: ... def arrow( @@ -422,9 +444,9 @@ class Axes(_AxesBase): def quiverkey( self, Q: Quiver, X: float, Y: float, U: float, label: str, **kwargs ) -> QuiverKey: ... - def quiver(self, *args, **kwargs) -> Quiver: ... - def barbs(self, *args, **kwargs) -> Barbs: ... - def fill(self, *args, data: Any | None = ..., **kwargs) -> list[Polygon]: ... + def quiver(self, *args, data=..., **kwargs) -> Quiver: ... + def barbs(self, *args, data=..., **kwargs) -> Barbs: ... + def fill(self, *args, data=..., **kwargs) -> list[Polygon]: ... def fill_between( self, x: ArrayLike, @@ -433,6 +455,8 @@ class Axes(_AxesBase): where: Sequence[bool] | None = ..., interpolate: bool = ..., step: Literal["pre", "post", "mid"] | None = ..., + *, + data=..., **kwargs ) -> PolyCollection: ... def fill_betweenx( @@ -443,6 +467,8 @@ class Axes(_AxesBase): where: Sequence[bool] | None = ..., step: Literal["pre", "post", "mid"] | None = ..., interpolate: bool = ..., + *, + data=..., **kwargs ) -> PolyCollection: ... def imshow( @@ -463,6 +489,7 @@ class Axes(_AxesBase): filterrad: float = ..., resample: bool | None = ..., url: str | None = ..., + data=..., **kwargs ) -> AxesImage: ... def pcolor( @@ -474,6 +501,7 @@ class Axes(_AxesBase): cmap: str | Colormap | None = ..., vmin: float | None = ..., vmax: float | None = ..., + data=..., **kwargs ) -> Collection: ... def pcolormesh( @@ -486,6 +514,7 @@ class Axes(_AxesBase): vmax: float | None = ..., shading: Literal["flat", "nearest", "gouraud", "auto"] | None = ..., antialiased: bool = ..., + data=..., **kwargs ) -> QuadMesh: ... def pcolorfast( @@ -496,10 +525,11 @@ class Axes(_AxesBase): cmap: str | Colormap | None = ..., vmin: float | None = ..., vmax: float | None = ..., + data=..., **kwargs ) -> AxesImage | PcolorImage | QuadMesh: ... - def contour(self, *args, **kwargs) -> QuadContourSet: ... - def contourf(self, *args, **kwargs) -> QuadContourSet: ... + def contour(self, *args, data=..., **kwargs) -> QuadContourSet: ... + def contourf(self, *args, data=..., **kwargs) -> QuadContourSet: ... def clabel( self, CS: ContourSet, levels: ArrayLike | None = ..., **kwargs ) -> list[Text]: ... @@ -520,6 +550,8 @@ class Axes(_AxesBase): color: ColorType | Sequence[ColorType] | None = ..., label: str | Sequence[str] | None = ..., stacked: bool = ..., + *, + data=..., **kwargs ) -> tuple[ np.ndarray | list[np.ndarray], @@ -534,6 +566,7 @@ class Axes(_AxesBase): orientation: Literal["vertical", "horizontal"] = ..., baseline: float | ArrayLike = ..., fill: bool = ..., + data=..., **kwargs ) -> StepPatch: ... def hist2d( @@ -550,6 +583,8 @@ class Axes(_AxesBase): weights: ArrayLike | None = ..., cmin: float | None = ..., cmax: float | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, np.ndarray, QuadMesh]: ... def ecdf( @@ -560,6 +595,7 @@ class Axes(_AxesBase): complementary: bool=..., orientation: Literal["vertical", "horizonatal"]=..., compress: bool=..., + data=..., **kwargs ) -> Line2D: ... def psd( @@ -577,6 +613,8 @@ class Axes(_AxesBase): sides: Literal["default", "onesided", "twosided"] | None = ..., scale_by_freq: bool | None = ..., return_line: bool | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray] | tuple[np.ndarray, np.ndarray, Line2D]: ... def csd( @@ -595,6 +633,8 @@ class Axes(_AxesBase): sides: Literal["default", "onesided", "twosided"] | None = ..., scale_by_freq: bool | None = ..., return_line: bool | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray] | tuple[np.ndarray, np.ndarray, Line2D]: ... def magnitude_spectrum( @@ -606,6 +646,8 @@ class Axes(_AxesBase): pad_to: int | None = ..., sides: Literal["default", "onesided", "twosided"] | None = ..., scale: Literal["default", "linear", "dB"] | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, Line2D]: ... def angle_spectrum( @@ -616,6 +658,8 @@ class Axes(_AxesBase): window: Callable[[ArrayLike], ArrayLike] | ArrayLike | None = ..., pad_to: int | None = ..., sides: Literal["default", "onesided", "twosided"] | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, Line2D]: ... def phase_spectrum( @@ -626,6 +670,8 @@ class Axes(_AxesBase): window: Callable[[ArrayLike], ArrayLike] | ArrayLike | None = ..., pad_to: int | None = ..., sides: Literal["default", "onesided", "twosided"] | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, Line2D]: ... def cohere( @@ -642,6 +688,8 @@ class Axes(_AxesBase): pad_to: int | None = ..., sides: Literal["default", "onesided", "twosided"] = ..., scale_by_freq: bool | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray]: ... def specgram( @@ -664,6 +712,8 @@ class Axes(_AxesBase): scale: Literal["default", "linear", "dB"] | None = ..., vmin: float | None = ..., vmax: float | None = ..., + *, + data=..., **kwargs ) -> tuple[np.ndarray, np.ndarray, np.ndarray, AxesImage]: ... def spy( @@ -692,6 +742,8 @@ class Axes(_AxesBase): | float | Callable[[GaussianKDE], float] | None = ..., + *, + data=..., ) -> dict[str, Collection]: ... def violin( self, From 3f2b577fa14ea3de8ea88f5b15e1d0ebad208ff9 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 5 Apr 2023 17:09:12 -0500 Subject: [PATCH 2/2] Rerender pyplot.py --- lib/matplotlib/pyplot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 0f1078256f52..fd8d5e05b7f1 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2669,7 +2669,7 @@ def barh( left: float | ArrayLike | None = None, *, align: Literal["center", "edge"] = "center", - data: Any | None = None, + data=None, **kwargs, ) -> BarContainer: return gca().barh( @@ -2983,7 +2983,7 @@ def eventplot( # Autogenerated by boilerplate.py. Do not edit as changes will be lost. @_copy_docstring_and_deprecators(Axes.fill) -def fill(*args, data: Any | None = None, **kwargs) -> list[Polygon]: +def fill(*args, data=None, **kwargs) -> list[Polygon]: return gca().fill(*args, **({"data": data} if data is not None else {}), **kwargs) @@ -3476,7 +3476,7 @@ def plot( *args: float | ArrayLike | str, scalex: bool = True, scaley: bool = True, - data: Any | None = None, + data=None, **kwargs, ) -> list[Line2D]: return gca().plot( @@ -3740,7 +3740,7 @@ def step( y: ArrayLike, *args, where: Literal["pre", "post", "mid"] = "pre", - data: Any | None = None, + data=None, **kwargs, ) -> list[Line2D]: return gca().step(