diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 8eca9d0c5..0caf99b53 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -310,25 +310,26 @@ UsecolsArgType: TypeAlias = ( | None ) # Scratch types for generics + S1 = TypeVar( "S1", - str, - bytes, - datetime.date, - datetime.time, - bool, - int, - float, - complex, - Dtype, - Timestamp, - Timedelta, - Period, - Interval[int], - Interval[float], - Interval[Timestamp], - Interval[Timedelta], - CategoricalDtype, + bound=str + | bytes + | datetime.date + | datetime.time + | bool + | int + | float + | complex + | Dtype + | Timestamp + | Timedelta + | Period + | Interval[int] + | Interval[float] + | Interval[Timestamp] + | Interval[Timedelta] + | CategoricalDtype, ) T1 = TypeVar( "T1", str, int, np.int64, np.uint64, np.float64, float, np.dtype[np.generic] diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index f2a80f1fc..34bb83d63 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -52,6 +52,7 @@ from pandas.core.window.rolling import ( Rolling, Window, ) +from typing_extensions import Self import xarray as xr from pandas._libs.missing import NAType @@ -154,7 +155,7 @@ class _iLocIndexerFrame(_iLocIndexer): | tuple[IndexType, int] | tuple[IndexType, IndexType] | tuple[int, IndexType], - value: S1 | Series | DataFrame | np.ndarray | None, + value: Scalar | Series | DataFrame | np.ndarray | None, ) -> None: ... class _LocIndexerFrame(_LocIndexer): @@ -203,13 +204,13 @@ class _LocIndexerFrame(_LocIndexer): def __setitem__( self, idx: MaskType | StrLike | _IndexSliceTuple | list[ScalarT], - value: S1 | ArrayLike | Series | DataFrame | list | None, + value: Scalar | ArrayLike | Series | DataFrame | list | None, ) -> None: ... @overload def __setitem__( self, idx: tuple[_IndexSliceTuple, HashableT], - value: S1 | ArrayLike | Series[S1] | list | None, + value: Scalar | ArrayLike | Series | list | None, ) -> None: ... class DataFrame(NDFrame, OpsMixin): @@ -227,7 +228,7 @@ class DataFrame(NDFrame, OpsMixin): columns: Axes | None = ..., dtype=..., copy: _bool = ..., - ) -> DataFrame: ... + ) -> Self: ... @overload def __new__( cls, @@ -236,7 +237,7 @@ class DataFrame(NDFrame, OpsMixin): columns: Axes, dtype=..., copy: _bool = ..., - ) -> DataFrame: ... + ) -> Self: ... def __dataframe__( self, nan_as_null: bool = ..., allow_copy: bool = ... ) -> DataFrameXchg: ... diff --git a/pandas-stubs/core/indexes/base.pyi b/pandas-stubs/core/indexes/base.pyi index fa27a5235..b9ad3a1f7 100644 --- a/pandas-stubs/core/indexes/base.pyi +++ b/pandas-stubs/core/indexes/base.pyi @@ -70,7 +70,7 @@ class _IndexGetitemMixin(Generic[S1]): class Index(IndexOpsMixin, PandasObject): __hash__: ClassVar[None] # type: ignore[assignment] @overload - def __new__( + def __new__( # type: ignore[misc] cls, data: Iterable, dtype: Literal["int"] | type_t[int] | type_t[np.integer], @@ -80,7 +80,7 @@ class Index(IndexOpsMixin, PandasObject): **kwargs, ) -> _IntIndexType: ... @overload - def __new__( + def __new__( # type: ignore[misc] cls, data: Iterable, dtype: Literal["float"] @@ -93,7 +93,7 @@ class Index(IndexOpsMixin, PandasObject): **kwargs, ) -> _FloatIndexType: ... @overload - def __new__( + def __new__( # type: ignore[misc] cls, data: Iterable, dtype: Literal["complex"] | type_t[complex], @@ -111,7 +111,7 @@ class Index(IndexOpsMixin, PandasObject): name=..., tupleize_cols: bool = ..., **kwargs, - ) -> Index: ... + ) -> Self: ... @property def str(self) -> StringMethods[Index, MultiIndex]: ... @property diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index b97b4a8d0..07ada220c 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -76,6 +76,7 @@ from pandas.core.window.rolling import ( ) from typing_extensions import ( Never, + Self, TypeAlias, ) import xarray as xr @@ -302,16 +303,21 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): name: Hashable | None = ..., copy: bool = ..., fastpath: bool = ..., - ) -> Series[S1]: ... + ) -> Self: ... @overload def __new__( cls, - data: object - | _ListLike - | Series[S1] - | dict[int, S1] - | dict[_str, S1] - | None = ..., + data: Series[S1] | dict[int, S1] | dict[_str, S1] = ..., + index: Axes | None = ..., + dtype=..., + name: Hashable | None = ..., + copy: bool = ..., + fastpath: bool = ..., + ) -> Self: ... + @overload + def __new__( + cls, + data: object | _ListLike | None = ..., index: Axes | None = ..., dtype=..., name: Hashable | None = ..., @@ -1923,7 +1929,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): # ignore needed because of mypy, for using `Never` as type-var. @overload def sum( - self: Series[Never], # type: ignore[type-var] + self: Series[Never], axis: AxisIndex | None = ..., skipna: _bool | None = ..., level: None = ..., @@ -2008,8 +2014,8 @@ class TimestampSeries(Series[Timestamp]): # ignore needed because of mypy @property def dt(self) -> TimestampProperties: ... # type: ignore[override] - def __add__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override] - def __radd__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override] + def __add__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override] + def __radd__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override] @overload # type: ignore[override] def __sub__( self, other: Timestamp | datetime | TimestampSeries @@ -2017,7 +2023,7 @@ class TimestampSeries(Series[Timestamp]): @overload def __sub__( self, - other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64, + other: timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64, ) -> TimestampSeries: ... def __mul__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override] def __truediv__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override] diff --git a/pyproject.toml b/pyproject.toml index 780f39401..f84095e12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ mypy = "1.3.0" pandas = "2.0.1" pyarrow = ">=10.0.1" pytest = ">=7.1.2" -pyright = "<= 1.1.308" +pyright = ">= 1.1.310" poethepoet = ">=0.16.5" loguru = ">=0.6.0" typing-extensions = ">=4.4.0" diff --git a/tests/test_frame.py b/tests/test_frame.py index 01367bb09..6af41830b 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -2598,15 +2598,15 @@ def test_align() -> None: columns=["A", "B", "C"], ) - s0 = pd.Series(data={1: "1", 3: "3", 5: "5"}) + s0 = pd.Series(data={0: "1", 3: "3", 5: "5"}) aligned_df0, aligned_s0 = df0.align(s0, axis="index") check(assert_type(aligned_df0, pd.DataFrame), pd.DataFrame) - check(assert_type(aligned_s0, pd.Series), pd.Series) + check(assert_type(aligned_s0, "pd.Series[str]"), pd.Series, str) s1 = pd.Series(data={"A": "A", "D": "D"}) aligned_df0, aligned_s1 = df0.align(s1, axis="columns") check(assert_type(aligned_df0, pd.DataFrame), pd.DataFrame) - check(assert_type(aligned_s1, pd.Series), pd.Series) + check(assert_type(aligned_s1, "pd.Series[str]"), pd.Series, str) df1 = pd.DataFrame( data=np.array( diff --git a/tests/test_series.py b/tests/test_series.py index 16bceb09b..2a9c82fcf 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -29,6 +29,7 @@ from pandas.core.window import ExponentialMovingWindow import pytest from typing_extensions import ( + Self, TypeAlias, assert_type, ) @@ -1604,12 +1605,14 @@ def test_pandera_generic() -> None: T = TypeVar("T") class MySeries(pd.Series, Generic[T]): - ... + def __new__(cls, *args, **kwargs) -> Self: + return object.__new__(cls) def func() -> MySeries[float]: return MySeries[float]([1, 2, 3]) - func() + result = func() + assert result.iloc[1] == 2 def test_change_to_dict_return_type() -> None: