Skip to content

Commit 630efce

Browse files
authored
Support for pyright 1.1.310 (#716)
* Use Self with __new__ * change S1 to use Union, add __new__ to test_pandera_generic
1 parent 6d21d7c commit 630efce

File tree

7 files changed

+54
-43
lines changed

7 files changed

+54
-43
lines changed

pandas-stubs/_typing.pyi

+18-17
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,26 @@ UsecolsArgType: TypeAlias = (
310310
| None
311311
)
312312
# Scratch types for generics
313+
313314
S1 = TypeVar(
314315
"S1",
315-
str,
316-
bytes,
317-
datetime.date,
318-
datetime.time,
319-
bool,
320-
int,
321-
float,
322-
complex,
323-
Dtype,
324-
Timestamp,
325-
Timedelta,
326-
Period,
327-
Interval[int],
328-
Interval[float],
329-
Interval[Timestamp],
330-
Interval[Timedelta],
331-
CategoricalDtype,
316+
bound=str
317+
| bytes
318+
| datetime.date
319+
| datetime.time
320+
| bool
321+
| int
322+
| float
323+
| complex
324+
| Dtype
325+
| Timestamp
326+
| Timedelta
327+
| Period
328+
| Interval[int]
329+
| Interval[float]
330+
| Interval[Timestamp]
331+
| Interval[Timedelta]
332+
| CategoricalDtype,
332333
)
333334
T1 = TypeVar(
334335
"T1", str, int, np.int64, np.uint64, np.float64, float, np.dtype[np.generic]

pandas-stubs/core/frame.pyi

+6-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ from pandas.core.window.rolling import (
5353
Rolling,
5454
Window,
5555
)
56+
from typing_extensions import Self
5657
import xarray as xr
5758

5859
from pandas._libs.missing import NAType
@@ -155,7 +156,7 @@ class _iLocIndexerFrame(_iLocIndexer):
155156
| tuple[IndexType, int]
156157
| tuple[IndexType, IndexType]
157158
| tuple[int, IndexType],
158-
value: S1 | Series | DataFrame | np.ndarray | None,
159+
value: Scalar | Series | DataFrame | np.ndarray | None,
159160
) -> None: ...
160161

161162
class _LocIndexerFrame(_LocIndexer):
@@ -204,13 +205,13 @@ class _LocIndexerFrame(_LocIndexer):
204205
def __setitem__(
205206
self,
206207
idx: MaskType | StrLike | _IndexSliceTuple | list[ScalarT],
207-
value: S1 | ArrayLike | Series | DataFrame | list | None,
208+
value: Scalar | ArrayLike | Series | DataFrame | list | None,
208209
) -> None: ...
209210
@overload
210211
def __setitem__(
211212
self,
212213
idx: tuple[_IndexSliceTuple, HashableT],
213-
value: S1 | ArrayLike | Series[S1] | list | None,
214+
value: Scalar | ArrayLike | Series | list | None,
214215
) -> None: ...
215216

216217
class DataFrame(NDFrame, OpsMixin):
@@ -228,7 +229,7 @@ class DataFrame(NDFrame, OpsMixin):
228229
columns: Axes | None = ...,
229230
dtype=...,
230231
copy: _bool = ...,
231-
) -> DataFrame: ...
232+
) -> Self: ...
232233
@overload
233234
def __new__(
234235
cls,
@@ -237,7 +238,7 @@ class DataFrame(NDFrame, OpsMixin):
237238
columns: Axes,
238239
dtype=...,
239240
copy: _bool = ...,
240-
) -> DataFrame: ...
241+
) -> Self: ...
241242
def __dataframe__(
242243
self, nan_as_null: bool = ..., allow_copy: bool = ...
243244
) -> DataFrameXchg: ...

pandas-stubs/core/indexes/base.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class _IndexGetitemMixin(Generic[S1]):
7070
class Index(IndexOpsMixin, PandasObject):
7171
__hash__: ClassVar[None] # type: ignore[assignment]
7272
@overload
73-
def __new__(
73+
def __new__( # type: ignore[misc]
7474
cls,
7575
data: Iterable,
7676
dtype: Literal["int"] | type_t[int] | type_t[np.integer],
@@ -80,7 +80,7 @@ class Index(IndexOpsMixin, PandasObject):
8080
**kwargs,
8181
) -> _IntIndexType: ...
8282
@overload
83-
def __new__(
83+
def __new__( # type: ignore[misc]
8484
cls,
8585
data: Iterable,
8686
dtype: Literal["float"]
@@ -93,7 +93,7 @@ class Index(IndexOpsMixin, PandasObject):
9393
**kwargs,
9494
) -> _FloatIndexType: ...
9595
@overload
96-
def __new__(
96+
def __new__( # type: ignore[misc]
9797
cls,
9898
data: Iterable,
9999
dtype: Literal["complex"] | type_t[complex],
@@ -111,7 +111,7 @@ class Index(IndexOpsMixin, PandasObject):
111111
name=...,
112112
tupleize_cols: bool = ...,
113113
**kwargs,
114-
) -> Index: ...
114+
) -> Self: ...
115115
@property
116116
def str(self) -> StringMethods[Index, MultiIndex]: ...
117117
@property

pandas-stubs/core/series.pyi

+17-11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ from pandas.core.window.rolling import (
7676
)
7777
from typing_extensions import (
7878
Never,
79+
Self,
7980
TypeAlias,
8081
)
8182
import xarray as xr
@@ -302,16 +303,21 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
302303
name: Hashable | None = ...,
303304
copy: bool = ...,
304305
fastpath: bool = ...,
305-
) -> Series[S1]: ...
306+
) -> Self: ...
306307
@overload
307308
def __new__(
308309
cls,
309-
data: object
310-
| _ListLike
311-
| Series[S1]
312-
| dict[int, S1]
313-
| dict[_str, S1]
314-
| None = ...,
310+
data: Series[S1] | dict[int, S1] | dict[_str, S1] = ...,
311+
index: Axes | None = ...,
312+
dtype=...,
313+
name: Hashable | None = ...,
314+
copy: bool = ...,
315+
fastpath: bool = ...,
316+
) -> Self: ...
317+
@overload
318+
def __new__(
319+
cls,
320+
data: object | _ListLike | None = ...,
315321
index: Axes | None = ...,
316322
dtype=...,
317323
name: Hashable | None = ...,
@@ -1923,7 +1929,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
19231929
# ignore needed because of mypy, for using `Never` as type-var.
19241930
@overload
19251931
def sum(
1926-
self: Series[Never], # type: ignore[type-var]
1932+
self: Series[Never],
19271933
axis: AxisIndex | None = ...,
19281934
skipna: _bool | None = ...,
19291935
level: None = ...,
@@ -2008,16 +2014,16 @@ class TimestampSeries(Series[Timestamp]):
20082014
# ignore needed because of mypy
20092015
@property
20102016
def dt(self) -> TimestampProperties: ... # type: ignore[override]
2011-
def __add__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override]
2012-
def __radd__(self, other: TimedeltaSeries | np.timedelta64) -> TimestampSeries: ... # type: ignore[override]
2017+
def __add__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override]
2018+
def __radd__(self, other: TimedeltaSeries | np.timedelta64 | timedelta) -> TimestampSeries: ... # type: ignore[override]
20132019
@overload # type: ignore[override]
20142020
def __sub__(
20152021
self, other: Timestamp | datetime | TimestampSeries
20162022
) -> TimedeltaSeries: ...
20172023
@overload
20182024
def __sub__(
20192025
self,
2020-
other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
2026+
other: timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64,
20212027
) -> TimestampSeries: ...
20222028
def __mul__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override]
20232029
def __truediv__(self, other: float | Series[int] | Series[float] | Sequence[float]) -> TimestampSeries: ... # type: ignore[override]

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mypy = "1.3.0"
4040
pandas = "2.0.1"
4141
pyarrow = ">=10.0.1"
4242
pytest = ">=7.1.2"
43-
pyright = "<= 1.1.308"
43+
pyright = ">= 1.1.310"
4444
poethepoet = ">=0.16.5"
4545
loguru = ">=0.6.0"
4646
typing-extensions = ">=4.4.0"

tests/test_frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2598,15 +2598,15 @@ def test_align() -> None:
25982598
columns=["A", "B", "C"],
25992599
)
26002600

2601-
s0 = pd.Series(data={1: "1", 3: "3", 5: "5"})
2601+
s0 = pd.Series(data={0: "1", 3: "3", 5: "5"})
26022602
aligned_df0, aligned_s0 = df0.align(s0, axis="index")
26032603
check(assert_type(aligned_df0, pd.DataFrame), pd.DataFrame)
2604-
check(assert_type(aligned_s0, pd.Series), pd.Series)
2604+
check(assert_type(aligned_s0, "pd.Series[str]"), pd.Series, str)
26052605

26062606
s1 = pd.Series(data={"A": "A", "D": "D"})
26072607
aligned_df0, aligned_s1 = df0.align(s1, axis="columns")
26082608
check(assert_type(aligned_df0, pd.DataFrame), pd.DataFrame)
2609-
check(assert_type(aligned_s1, pd.Series), pd.Series)
2609+
check(assert_type(aligned_s1, "pd.Series[str]"), pd.Series, str)
26102610

26112611
df1 = pd.DataFrame(
26122612
data=np.array(

tests/test_series.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pandas.core.window import ExponentialMovingWindow
3030
import pytest
3131
from typing_extensions import (
32+
Self,
3233
TypeAlias,
3334
assert_type,
3435
)
@@ -1604,12 +1605,14 @@ def test_pandera_generic() -> None:
16041605
T = TypeVar("T")
16051606

16061607
class MySeries(pd.Series, Generic[T]):
1607-
...
1608+
def __new__(cls, *args, **kwargs) -> Self:
1609+
return object.__new__(cls)
16081610

16091611
def func() -> MySeries[float]:
16101612
return MySeries[float]([1, 2, 3])
16111613

1612-
func()
1614+
result = func()
1615+
assert result.iloc[1] == 2
16131616

16141617

16151618
def test_change_to_dict_return_type() -> None:

0 commit comments

Comments
 (0)