From ada9df89a5a8f67d3b25994b2206377dcc635a76 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Mon, 15 Dec 2025 20:17:57 +0100 Subject: [PATCH 1/4] native types and groupby --- pandas-stubs/core/frame.pyi | 16 ++++----- pandas-stubs/core/groupby/groupby.pyi | 8 ++--- pandas-stubs/core/resample.pyi | 2 +- pandas-stubs/core/series.pyi | 6 ++-- tests/frame/test_frame.py | 36 ++++++++++++++------ tests/series/test_series.py | 6 ++-- tests/test_api_typing.py | 49 ++++++++++++++++----------- tests/test_extension.py | 7 ++-- tests/test_resampler.py | 9 +++-- 9 files changed, 84 insertions(+), 55 deletions(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index c45da47c1..e7fd28e0b 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -484,7 +484,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: str = ..., *, - into: type[defaultdict], + into: type[defaultdict[Any, Any]], index: Literal[True] = True, ) -> Never: ... @overload @@ -500,7 +500,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["records"], *, - into: type[dict] = ..., + into: type[dict[Any, Any]] = ..., index: Literal[True] = True, ) -> list[dict[Hashable, Any]]: ... @overload @@ -516,7 +516,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["index"], *, - into: OrderedDict | type[OrderedDict], + into: OrderedDict[Any, Any] | type[OrderedDict[Any, Any]], index: Literal[True] = True, ) -> OrderedDict[Hashable, dict[Hashable, Any]]: ... @overload @@ -524,7 +524,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["index"], *, - into: type[MutableMapping], + into: type[MutableMapping[Any, Any]], index: Literal[True] = True, ) -> MutableMapping[Hashable, dict[Hashable, Any]]: ... @overload @@ -532,7 +532,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["index"], *, - into: type[dict] = ..., + into: type[dict[Any, Any]] = ..., index: Literal[True] = True, ) -> dict[Hashable, dict[Hashable, Any]]: ... @overload @@ -548,7 +548,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["dict", "list", "series"] = ..., *, - into: type[dict] = ..., + into: type[dict[Any, Any]] = ..., index: Literal[True] = True, ) -> dict[Hashable, Any]: ... @overload @@ -556,7 +556,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["split", "tight"], *, - into: MutableMapping[Any, Any] | type[MutableMapping], + into: MutableMapping[Any, Any] | type[MutableMapping[Any, Any]], index: bool = ..., ) -> MutableMapping[str, list[Any]]: ... @overload @@ -564,7 +564,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): self, orient: Literal["split", "tight"], *, - into: type[dict] = ..., + into: type[dict[Any, Any]] = ..., index: bool = ..., ) -> dict[str, list[Any]]: ... @classmethod diff --git a/pandas-stubs/core/groupby/groupby.pyi b/pandas-stubs/core/groupby/groupby.pyi index e9699eccc..d0168198e 100644 --- a/pandas-stubs/core/groupby/groupby.pyi +++ b/pandas-stubs/core/groupby/groupby.pyi @@ -338,7 +338,7 @@ class GroupBy(BaseGroupBy[NDFrameT]): random_state: RandomState | None = ..., ) -> NDFrameT: ... -_GroupByT = TypeVar("_GroupByT", bound=GroupBy) +_GroupByT = TypeVar("_GroupByT", bound=GroupBy[Any]) # GroupByPlot does not really inherit from PlotAccessor but it delegates # to it using __call__ and __getattr__. We lie here to avoid repeating the @@ -383,15 +383,15 @@ class BaseGroupBy(SelectionMixin[NDFrameT], GroupByIndexingMixin): @final def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]: ... @overload - def __getitem__(self: BaseGroupBy[DataFrame], key: Scalar) -> generic.SeriesGroupBy: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] + def __getitem__(self: BaseGroupBy[DataFrame], key: Scalar) -> generic.SeriesGroupBy[Any, Any]: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload] @overload def __getitem__( self: BaseGroupBy[DataFrame], key: Iterable[Hashable] - ) -> generic.DataFrameGroupBy: ... + ) -> generic.DataFrameGroupBy[Any, Any]: ... @overload def __getitem__( self: BaseGroupBy[Series[S1]], idx: list[str] | Index | Series[S1] | MaskType | tuple[Hashable | slice, ...], - ) -> generic.SeriesGroupBy: ... + ) -> generic.SeriesGroupBy[Any, Any]: ... @overload def __getitem__(self: BaseGroupBy[Series[S1]], idx: Scalar) -> S1: ... diff --git a/pandas-stubs/core/resample.pyi b/pandas-stubs/core/resample.pyi index 9f52c69ef..726d530ae 100644 --- a/pandas-stubs/core/resample.pyi +++ b/pandas-stubs/core/resample.pyi @@ -55,7 +55,7 @@ _SeriesGroupByFuncArgs: TypeAlias = ( ) class Resampler(BaseGroupBy[NDFrameT]): - def __getattr__(self, attr: str) -> SeriesGroupBy: ... + def __getattr__(self, attr: str) -> SeriesGroupBy[Any, Any]: ... @overload def aggregate( self: Resampler[DataFrame], diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index c2fc80ea7..7778931f6 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -779,10 +779,10 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): def items(self) -> Iterator[tuple[Hashable, S1]]: ... def keys(self) -> Index: ... @overload - def to_dict(self, *, into: type[dict] = ...) -> dict[Any, S1]: ... + def to_dict(self, *, into: type[dict[Any, Any]] = ...) -> dict[Hashable, S1]: ... @overload def to_dict( - self, *, into: type[MutableMapping] | MutableMapping[Any, Any] + self, *, into: type[MutableMapping[Any, Any]] | MutableMapping[Any, Any] ) -> MutableMapping[Hashable, S1]: ... def to_frame(self, name: object | None = ...) -> DataFrame: ... @overload @@ -1105,7 +1105,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame): def swaplevel( self, i: Level = -2, j: Level = -1, copy: _bool = True ) -> Series[S1]: ... - def reorder_levels(self, order: list) -> Series[S1]: ... + def reorder_levels(self, order: list[Any]) -> Series[S1]: ... def explode(self, ignore_index: _bool = ...) -> Series[S1]: ... def unstack( self, diff --git a/tests/frame/test_frame.py b/tests/frame/test_frame.py index 67e5b0445..d895b9981 100644 --- a/tests/frame/test_frame.py +++ b/tests/frame/test_frame.py @@ -3021,10 +3021,10 @@ def test_to_dict_simple() -> None: check(assert_type(data.to_dict("dict"), dict[Hashable, Any]), dict) check(assert_type(data.to_dict("list"), dict[Hashable, Any]), dict) check(assert_type(data.to_dict("series"), dict[Hashable, Any]), dict) - check(assert_type(data.to_dict("split"), dict[str, list]), dict, str) + check(assert_type(data.to_dict("split"), dict[str, list[Any]]), dict, str) # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html - check(assert_type(data.to_dict("tight"), dict[str, list]), dict, str) + check(assert_type(data.to_dict("tight"), dict[str, list[Any]]), dict, str) if TYPE_CHECKING_INVALID_USAGE: @@ -3075,7 +3075,7 @@ def test_to_dict_into_defaultdict() -> None: defaultdict, ) check( - assert_type(data.to_dict("tight", into=target), MutableMapping[str, list]), + assert_type(data.to_dict("tight", into=target), MutableMapping[str, list[Any]]), defaultdict, str, ) @@ -3093,7 +3093,11 @@ def test_to_dict_into_ordered_dict() -> None: data = pd.DataFrame({("str", "rts"): [[1, 2, 4], [2, 3], [3]]}) - check(assert_type(data.to_dict(into=OrderedDict), OrderedDict), OrderedDict, tuple) + check( + assert_type(data.to_dict(into=OrderedDict), OrderedDict[Any, Any]), + OrderedDict, + tuple, + ) check( assert_type( data.to_dict("index", into=OrderedDict), @@ -3102,12 +3106,16 @@ def test_to_dict_into_ordered_dict() -> None: OrderedDict, ) check( - assert_type(data.to_dict("tight", into=OrderedDict), MutableMapping[str, list]), + assert_type( + data.to_dict("tight", into=OrderedDict), MutableMapping[str, list[Any]] + ), OrderedDict, str, ) check( - assert_type(data.to_dict("records", into=OrderedDict), list[OrderedDict]), + assert_type( + data.to_dict("records", into=OrderedDict), list[OrderedDict[Any, Any]] + ), list, OrderedDict, ) @@ -3446,16 +3454,24 @@ def test_to_dict_index() -> None: dict, ) check( - assert_type(df.to_dict(orient="split", index=True), dict[str, list]), dict, str + assert_type(df.to_dict(orient="split", index=True), dict[str, list[Any]]), + dict, + str, ) check( - assert_type(df.to_dict(orient="tight", index=True), dict[str, list]), dict, str + assert_type(df.to_dict(orient="tight", index=True), dict[str, list[Any]]), + dict, + str, ) check( - assert_type(df.to_dict(orient="tight", index=False), dict[str, list]), dict, str + assert_type(df.to_dict(orient="tight", index=False), dict[str, list[Any]]), + dict, + str, ) check( - assert_type(df.to_dict(orient="split", index=False), dict[str, list]), dict, str + assert_type(df.to_dict(orient="split", index=False), dict[str, list[Any]]), + dict, + str, ) if TYPE_CHECKING_INVALID_USAGE: _0 = df.to_dict(orient="records", index=False) # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue] diff --git a/tests/series/test_series.py b/tests/series/test_series.py index 96d3e411c..bfd4028a7 100644 --- a/tests/series/test_series.py +++ b/tests/series/test_series.py @@ -838,7 +838,7 @@ def makeseries(x: float) -> pd.Series: def retseries(x: float) -> float: return x - check(assert_type(s.apply(retseries).tolist(), list), list) + check(assert_type(s.apply(retseries).tolist(), list[Any]), list) def retlist(x: float) -> list[float]: return [x] @@ -1780,7 +1780,7 @@ def test_types_to_list() -> None: def test_types_to_dict() -> None: s = pd.Series(["a", "b", "c"], dtype=str) - assert_type(s.to_dict(), dict[Any, str]) + assert_type(s.to_dict(), dict[Hashable, str]) def test_categorical_codes() -> None: @@ -2182,7 +2182,7 @@ def test_change_to_dict_return_type() -> None: value = ["a", "b", "c"] df = pd.DataFrame(zip(id, value), columns=["id", "value"]) fd = df.set_index("id")["value"].to_dict() - check(assert_type(fd, dict[Any, Any]), dict) + check(assert_type(fd, dict[Hashable, Any]), dict) ASTYPE_BOOL_ARGS: list[tuple[BooleanDtypeArg, type]] = [ diff --git a/tests/test_api_typing.py b/tests/test_api_typing.py index 5b01be922..abdc0bc52 100644 --- a/tests/test_api_typing.py +++ b/tests/test_api_typing.py @@ -1,6 +1,10 @@ """Test module for classes in pandas.api.typing.""" -from typing import TypeAlias +from typing import ( + TYPE_CHECKING, + Any, + TypeAlias, +) import numpy as np import pandas as pd @@ -26,27 +30,32 @@ Window, ) import pytest -from typing_extensions import ( - assert_type, -) +from typing_extensions import assert_type from tests import ( check, ensure_clean, ) -ResamplerGroupBy: TypeAlias = ( - DatetimeIndexResamplerGroupby - | PeriodIndexResamplerGroupby - | TimedeltaIndexResamplerGroupby -) +if TYPE_CHECKING: + ResamplerGroupBy: TypeAlias = ( + DatetimeIndexResamplerGroupby[Any] + | PeriodIndexResamplerGroupby[Any] + | TimedeltaIndexResamplerGroupby[Any] + ) +else: + ResamplerGroupBy: TypeAlias = ( + DatetimeIndexResamplerGroupby + | PeriodIndexResamplerGroupby + | TimedeltaIndexResamplerGroupby + ) def test_dataframegroupby() -> None: df = pd.DataFrame({"a": [1, 2, 3]}) group = df.groupby("a") - def f1(gb: DataFrameGroupBy) -> None: + def f1(gb: "DataFrameGroupBy[Any, Any]") -> None: check(gb, DataFrameGroupBy) f1(group) @@ -55,7 +64,7 @@ def f1(gb: DataFrameGroupBy) -> None: def test_seriesgroupby() -> None: sr = pd.Series([1, 2, 3], index=pd.Index(["a", "b", "a"])) - def f1(gb: SeriesGroupBy) -> None: + def f1(gb: "SeriesGroupBy[Any, Any]") -> None: check(gb, SeriesGroupBy) f1(sr.groupby(level=0)) @@ -113,7 +122,7 @@ def test_nattype() -> None: def test_expanding() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: Expanding) -> None: + def f1(gb: "Expanding[Any]") -> None: check(gb, Expanding) f1(df.expanding()) @@ -122,7 +131,7 @@ def f1(gb: Expanding) -> None: def test_expanding_groubpy() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: ExpandingGroupby) -> None: + def f1(gb: "ExpandingGroupby[Any]") -> None: check(gb, ExpandingGroupby) f1(df.groupby("B").expanding()) @@ -131,7 +140,7 @@ def f1(gb: ExpandingGroupby) -> None: def test_ewm() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: ExponentialMovingWindow) -> None: + def f1(gb: "ExponentialMovingWindow[Any]") -> None: check(gb, ExponentialMovingWindow) f1(df.ewm(2)) @@ -140,7 +149,7 @@ def f1(gb: ExponentialMovingWindow) -> None: def test_ewm_groubpy() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: ExponentialMovingWindowGroupby) -> None: + def f1(gb: "ExponentialMovingWindowGroupby[Any]") -> None: check(gb, ExponentialMovingWindowGroupby) f1(df.groupby("B").ewm(2)) @@ -149,7 +158,7 @@ def f1(gb: ExponentialMovingWindowGroupby) -> None: def test_json_reader() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: JsonReader) -> None: + def f1(gb: JsonReader[Any]) -> None: check(gb, JsonReader) with ensure_clean() as path: @@ -162,7 +171,7 @@ def f1(gb: JsonReader) -> None: def test_resampler() -> None: s = pd.Series([1, 2, 3, 4, 5], index=pd.date_range("20130101", periods=5, freq="s")) - def f1(gb: Resampler) -> None: + def f1(gb: "Resampler[Any]") -> None: check(gb, Resampler) f1(s.resample("3min")) @@ -171,7 +180,7 @@ def f1(gb: Resampler) -> None: def test_rolling() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: Rolling) -> None: + def f1(gb: "Rolling[Any]") -> None: check(gb, Rolling) f1(df.rolling(2)) @@ -180,7 +189,7 @@ def f1(gb: Rolling) -> None: def test_rolling_groupby() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: RollingGroupby) -> None: + def f1(gb: "RollingGroupby[Any]") -> None: check(gb, RollingGroupby) f1(df.groupby("B").rolling(2)) @@ -198,7 +207,7 @@ def f1(gb: TimeGrouper) -> None: def test_window() -> None: ser = pd.Series([0, 1, 5, 2, 8]) - def f1(gb: Window) -> None: + def f1(gb: "Window[Any]") -> None: check(gb, Window) f1(ser.rolling(2, win_type="gaussian")) diff --git a/tests/test_extension.py b/tests/test_extension.py index c2e0c52f2..15acdf328 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -1,4 +1,5 @@ import decimal +from typing import Any import numpy as np import pandas as pd @@ -29,9 +30,9 @@ def test_tolist() -> None: s1 = pd.Series(data1) # python/mypy#19952: mypy believes ExtensionArray and its subclasses have a # conflict and gives Any for s.array - check(assert_type(s.array.tolist(), list), list) # type: ignore[assert-type] - check(assert_type(s1.array.tolist(), list), list) - check(assert_type(pd.array([1, 2, 3]).tolist(), list), list) + check(assert_type(s.array.tolist(), list[Any]), list) # type: ignore[assert-type] + check(assert_type(s1.array.tolist(), list[Any]), list) + check(assert_type(pd.array([1, 2, 3]).tolist(), list[Any]), list) def test_ExtensionArray_reduce_accumulate() -> None: diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 3fa7ac15a..3a5c21128 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -2,6 +2,7 @@ Hashable, Iterator, ) +from typing import Any # noqa: F401 from typing import TypeAlias import numpy as np @@ -141,7 +142,7 @@ def test_asfreq() -> None: def test_getattr() -> None: - check(assert_type(DF.resample("ME").col1, SeriesGroupBy), SeriesGroupBy) + check(assert_type(DF.resample("ME").col1, "SeriesGroupBy[Any, Any]"), SeriesGroupBy) def test_interpolate() -> None: @@ -466,8 +467,10 @@ def df2scalar(val: DataFrame) -> float: def test_getitem() -> None: - check(assert_type(DF.resample("ME")["col1"], SeriesGroupBy), SeriesGroupBy) check( - assert_type(DF.resample("ME")[["col1", "col2"]], DataFrameGroupBy), + assert_type(DF.resample("ME")["col1"], "SeriesGroupBy[Any, Any]"), SeriesGroupBy + ) + check( + assert_type(DF.resample("ME")[["col1", "col2"]], "DataFrameGroupBy[Any, Any]"), DataFrameGroupBy, ) From 232e7cf0709d2d756036b99671ac586ac50a5ef1 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 16 Dec 2025 17:03:41 +0100 Subject: [PATCH 2/4] https://github.com/pandas-dev/pandas-stubs/pull/1556#discussion_r2623833429 --- tests/test_api_typing.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/test_api_typing.py b/tests/test_api_typing.py index abdc0bc52..3288e8c55 100644 --- a/tests/test_api_typing.py +++ b/tests/test_api_typing.py @@ -1,3 +1,4 @@ +# pyright: reportMissingTypeArgument=false """Test module for classes in pandas.api.typing.""" from typing import ( @@ -55,7 +56,7 @@ def test_dataframegroupby() -> None: df = pd.DataFrame({"a": [1, 2, 3]}) group = df.groupby("a") - def f1(gb: "DataFrameGroupBy[Any, Any]") -> None: + def f1(gb: DataFrameGroupBy) -> None: check(gb, DataFrameGroupBy) f1(group) @@ -64,7 +65,7 @@ def f1(gb: "DataFrameGroupBy[Any, Any]") -> None: def test_seriesgroupby() -> None: sr = pd.Series([1, 2, 3], index=pd.Index(["a", "b", "a"])) - def f1(gb: "SeriesGroupBy[Any, Any]") -> None: + def f1(gb: SeriesGroupBy) -> None: check(gb, SeriesGroupBy) f1(sr.groupby(level=0)) @@ -122,7 +123,7 @@ def test_nattype() -> None: def test_expanding() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "Expanding[Any]") -> None: + def f1(gb: Expanding) -> None: check(gb, Expanding) f1(df.expanding()) @@ -131,7 +132,7 @@ def f1(gb: "Expanding[Any]") -> None: def test_expanding_groubpy() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "ExpandingGroupby[Any]") -> None: + def f1(gb: ExpandingGroupby) -> None: check(gb, ExpandingGroupby) f1(df.groupby("B").expanding()) @@ -140,7 +141,7 @@ def f1(gb: "ExpandingGroupby[Any]") -> None: def test_ewm() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "ExponentialMovingWindow[Any]") -> None: + def f1(gb: ExponentialMovingWindow) -> None: check(gb, ExponentialMovingWindow) f1(df.ewm(2)) @@ -149,7 +150,7 @@ def f1(gb: "ExponentialMovingWindow[Any]") -> None: def test_ewm_groubpy() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "ExponentialMovingWindowGroupby[Any]") -> None: + def f1(gb: ExponentialMovingWindowGroupby) -> None: check(gb, ExponentialMovingWindowGroupby) f1(df.groupby("B").ewm(2)) @@ -171,7 +172,7 @@ def f1(gb: JsonReader[Any]) -> None: def test_resampler() -> None: s = pd.Series([1, 2, 3, 4, 5], index=pd.date_range("20130101", periods=5, freq="s")) - def f1(gb: "Resampler[Any]") -> None: + def f1(gb: Resampler) -> None: check(gb, Resampler) f1(s.resample("3min")) @@ -180,7 +181,7 @@ def f1(gb: "Resampler[Any]") -> None: def test_rolling() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "Rolling[Any]") -> None: + def f1(gb: Rolling) -> None: check(gb, Rolling) f1(df.rolling(2)) @@ -189,7 +190,7 @@ def f1(gb: "Rolling[Any]") -> None: def test_rolling_groupby() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: "RollingGroupby[Any]") -> None: + def f1(gb: RollingGroupby) -> None: check(gb, RollingGroupby) f1(df.groupby("B").rolling(2)) @@ -207,7 +208,7 @@ def f1(gb: TimeGrouper) -> None: def test_window() -> None: ser = pd.Series([0, 1, 5, 2, 8]) - def f1(gb: "Window[Any]") -> None: + def f1(gb: Window) -> None: check(gb, Window) f1(ser.rolling(2, win_type="gaussian")) From 02178a4d69f12765f896c19c9746b8351c321593 Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 16 Dec 2025 17:06:18 +0100 Subject: [PATCH 3/4] more reverts --- tests/test_api_typing.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/tests/test_api_typing.py b/tests/test_api_typing.py index 3288e8c55..4c5f0a038 100644 --- a/tests/test_api_typing.py +++ b/tests/test_api_typing.py @@ -1,11 +1,7 @@ # pyright: reportMissingTypeArgument=false """Test module for classes in pandas.api.typing.""" -from typing import ( - TYPE_CHECKING, - Any, - TypeAlias, -) +from typing import TypeAlias import numpy as np import pandas as pd @@ -38,18 +34,11 @@ ensure_clean, ) -if TYPE_CHECKING: - ResamplerGroupBy: TypeAlias = ( - DatetimeIndexResamplerGroupby[Any] - | PeriodIndexResamplerGroupby[Any] - | TimedeltaIndexResamplerGroupby[Any] - ) -else: - ResamplerGroupBy: TypeAlias = ( - DatetimeIndexResamplerGroupby - | PeriodIndexResamplerGroupby - | TimedeltaIndexResamplerGroupby - ) +ResamplerGroupBy: TypeAlias = ( + DatetimeIndexResamplerGroupby + | PeriodIndexResamplerGroupby + | TimedeltaIndexResamplerGroupby +) def test_dataframegroupby() -> None: @@ -159,7 +148,7 @@ def f1(gb: ExponentialMovingWindowGroupby) -> None: def test_json_reader() -> None: df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]}) - def f1(gb: JsonReader[Any]) -> None: + def f1(gb: JsonReader) -> None: check(gb, JsonReader) with ensure_clean() as path: From 30f1758418f8c000207c4a9c13939aedc8fb6cdd Mon Sep 17 00:00:00 2001 From: cmp0xff Date: Tue, 16 Dec 2025 18:01:06 +0100 Subject: [PATCH 4/4] furthre eliminate GroupBy[Any, Any] in tests https://github.com/pandas-dev/pandas-stubs/pull/1556/changes#r2624041237 --- tests/test_resampler.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_resampler.py b/tests/test_resampler.py index 3a5c21128..f9da71ebe 100644 --- a/tests/test_resampler.py +++ b/tests/test_resampler.py @@ -1,8 +1,8 @@ +# pyright: reportMissingTypeArgument=false from collections.abc import ( Hashable, Iterator, ) -from typing import Any # noqa: F401 from typing import TypeAlias import numpy as np @@ -142,7 +142,7 @@ def test_asfreq() -> None: def test_getattr() -> None: - check(assert_type(DF.resample("ME").col1, "SeriesGroupBy[Any, Any]"), SeriesGroupBy) + check(assert_type(DF.resample("ME").col1, SeriesGroupBy), SeriesGroupBy) def test_interpolate() -> None: @@ -467,10 +467,8 @@ def df2scalar(val: DataFrame) -> float: def test_getitem() -> None: + check(assert_type(DF.resample("ME")["col1"], SeriesGroupBy), SeriesGroupBy) check( - assert_type(DF.resample("ME")["col1"], "SeriesGroupBy[Any, Any]"), SeriesGroupBy - ) - check( - assert_type(DF.resample("ME")[["col1", "col2"]], "DataFrameGroupBy[Any, Any]"), + assert_type(DF.resample("ME")[["col1", "col2"]], DataFrameGroupBy), DataFrameGroupBy, )