Skip to content

Commit 5e16b86

Browse files
authored
refactor(ruff): comply to ANN206, PLC0105, etc. (pandas-dev#1445)
* ANN001 in tests * all ANN in tests * ignore ARG * ANN206 for pyi * PLC0105 * fix: mypy and pytest
1 parent 143bab4 commit 5e16b86

File tree

11 files changed

+149
-82
lines changed

11 files changed

+149
-82
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ci:
33
autofix_prs: false
44
repos:
55
- repo: https://github.com/astral-sh/ruff-pre-commit
6-
rev: v0.14.0
6+
rev: v0.14.1
77
hooks:
88
- id: ruff-check
99
args: [--exit-non-zero-on-fix]

pandas-stubs/_typing.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,10 @@ SeriesDType: TypeAlias = (
911911
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
912912
# Like S1, but without `default=Any`.
913913
S2 = TypeVar("S2", bound=SeriesDType)
914-
S2_CT = TypeVar("S2_CT", bound=SeriesDType, contravariant=True)
915-
S2_CT_NDT = TypeVar("S2_CT_NDT", bound=SeriesDTypeNoDateTime, contravariant=True)
914+
S2_contra = TypeVar("S2_contra", bound=SeriesDType, contravariant=True)
915+
S2_NDT_contra = TypeVar(
916+
"S2_NDT_contra", bound=SeriesDTypeNoDateTime, contravariant=True
917+
)
916918
S2_NSDT = TypeVar("S2_NSDT", bound=SeriesDTypeNoStrDateTime)
917919
S3 = TypeVar("S3", bound=SeriesDType)
918920

pandas-stubs/core/arrays/interval.pyi

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ from typing import (
55
)
66

77
import numpy as np
8-
from pandas import (
9-
Index,
10-
Series,
11-
)
128
from pandas.core.arrays.base import ExtensionArray as ExtensionArray
9+
from pandas.core.indexes.base import Index
10+
from pandas.core.series import Series
1311
from typing_extensions import Self
1412

1513
from pandas._libs.interval import (
@@ -32,15 +30,15 @@ class IntervalArray(IntervalMixin, ExtensionArray):
3230
can_hold_na: bool = ...
3331
def __new__(
3432
cls, data, closed=..., dtype=..., copy: bool = ..., verify_integrity: bool = ...
35-
): ...
33+
) -> Self: ...
3634
@classmethod
3735
def from_breaks(
3836
cls,
3937
breaks,
4038
closed: str = "right",
4139
copy: bool = False,
4240
dtype=None,
43-
): ...
41+
) -> Self: ...
4442
@classmethod
4543
def from_arrays(
4644
cls,
@@ -49,15 +47,15 @@ class IntervalArray(IntervalMixin, ExtensionArray):
4947
closed: str = "right",
5048
copy: bool = False,
5149
dtype=...,
52-
): ...
50+
) -> Self: ...
5351
@classmethod
5452
def from_tuples(
5553
cls,
5654
data,
5755
closed: str = "right",
5856
copy: bool = False,
5957
dtype=None,
60-
): ...
58+
) -> Self: ...
6159
def __array__(
6260
self, dtype: NpDtype | None = None, copy: bool | None = None
6361
) -> np_1darray: ...

pandas-stubs/core/arrays/sparse/accessor.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from pandas import Series
21
from pandas.core.accessor import PandasDelegate
2+
from pandas.core.series import Series
3+
from typing_extensions import Self
34

45
class BaseAccessor:
56
def __init__(self, data=...) -> None: ...
@@ -12,7 +13,7 @@ class SparseAccessor(BaseAccessor, PandasDelegate):
1213

1314
class SparseFrameAccessor(BaseAccessor, PandasDelegate):
1415
@classmethod
15-
def from_spmatrix(cls, data, index=..., columns=...): ...
16+
def from_spmatrix(cls, data, index=..., columns=...) -> Self: ...
1617
def to_dense(self): ...
1718
def to_coo(self): ...
1819
@property

pandas-stubs/core/arrays/sparse/array.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SparseArray(ExtensionArray, ExtensionOpsMixin):
3333
copy: bool = ...,
3434
) -> None: ...
3535
@classmethod
36-
def from_spmatrix(cls, data): ...
36+
def from_spmatrix(cls, data) -> Self: ...
3737
def __array__(
3838
self, dtype: NpDtype | None = None, copy: bool | None = None
3939
) -> np_1darray: ...

pandas-stubs/core/frame.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ from pandas._typing import (
169169
from pandas.io.formats.style import Styler
170170
from pandas.plotting import PlotAccessor
171171

172-
_T_MUTABLE_MAPPING = TypeVar("_T_MUTABLE_MAPPING", bound=MutableMapping, covariant=True)
172+
_T_MUTABLE_MAPPING_co = TypeVar(
173+
"_T_MUTABLE_MAPPING_co", bound=MutableMapping, covariant=True
174+
)
173175

174176
class _iLocIndexerFrame(_iLocIndexer, Generic[_T]):
175177
@overload
@@ -463,9 +465,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
463465
self,
464466
orient: Literal["records"],
465467
*,
466-
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
468+
into: _T_MUTABLE_MAPPING_co | type[_T_MUTABLE_MAPPING_co],
467469
index: Literal[True] = ...,
468-
) -> list[_T_MUTABLE_MAPPING]: ...
470+
) -> list[_T_MUTABLE_MAPPING_co]: ...
469471
@overload
470472
def to_dict(
471473
self,
@@ -511,9 +513,9 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
511513
self,
512514
orient: Literal["dict", "list", "series"] = ...,
513515
*,
514-
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING],
516+
into: _T_MUTABLE_MAPPING_co | type[_T_MUTABLE_MAPPING_co],
515517
index: Literal[True] = ...,
516-
) -> _T_MUTABLE_MAPPING: ...
518+
) -> _T_MUTABLE_MAPPING_co: ...
517519
@overload
518520
def to_dict(
519521
self,

pandas-stubs/core/indexes/base.pyi

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ from pandas._typing import (
6363
C2,
6464
S1,
6565
S2,
66-
S2_CT,
6766
S2_NSDT,
6867
T_COMPLEX,
6968
AnyAll,
@@ -85,6 +84,7 @@ from pandas._typing import (
8584
MaskType,
8685
NaPosition,
8786
ReindexMethod,
87+
S2_contra,
8888
Scalar,
8989
SequenceNotStr,
9090
SliceType,
@@ -520,8 +520,8 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
520520
) -> Index[S2]: ...
521521
@overload
522522
def __add__(
523-
self: Index[S2_CT],
524-
other: SupportsRAdd[S2_CT, S2] | Sequence[SupportsRAdd[S2_CT, S2]],
523+
self: Index[S2_contra],
524+
other: SupportsRAdd[S2_contra, S2] | Sequence[SupportsRAdd[S2_contra, S2]],
525525
) -> Index[S2]: ...
526526
@overload
527527
def __add__(
@@ -573,8 +573,8 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
573573
) -> Index[S2]: ...
574574
@overload
575575
def __radd__(
576-
self: Index[S2_CT],
577-
other: SupportsAdd[S2_CT, S2] | Sequence[SupportsAdd[S2_CT, S2]],
576+
self: Index[S2_contra],
577+
other: SupportsAdd[S2_contra, S2] | Sequence[SupportsAdd[S2_contra, S2]],
578578
) -> Index[S2]: ...
579579
@overload
580580
def __radd__(
@@ -789,8 +789,11 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
789789
) -> Index[S2]: ...
790790
@overload
791791
def __mul__(
792-
self: Index[S2_CT],
793-
other: SupportsRMul[S2_CT, S2_NSDT] | Sequence[SupportsRMul[S2_CT, S2_NSDT]],
792+
self: Index[S2_contra],
793+
other: (
794+
SupportsRMul[S2_contra, S2_NSDT]
795+
| Sequence[SupportsRMul[S2_contra, S2_NSDT]]
796+
),
794797
) -> Index[S2_NSDT]: ...
795798
@overload
796799
def __mul__(
@@ -853,8 +856,10 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
853856
) -> Index[S2]: ...
854857
@overload
855858
def __rmul__(
856-
self: Index[S2_CT],
857-
other: SupportsMul[S2_CT, S2_NSDT] | Sequence[SupportsMul[S2_CT, S2_NSDT]],
859+
self: Index[S2_contra],
860+
other: (
861+
SupportsMul[S2_contra, S2_NSDT] | Sequence[SupportsMul[S2_contra, S2_NSDT]]
862+
),
858863
) -> Index[S2_NSDT]: ...
859864
@overload
860865
def __rmul__(

pandas-stubs/core/indexes/range.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from pandas.core.indexes.base import (
1313
Index,
1414
_IndexSubclassBase,
1515
)
16+
from typing_extensions import Self
1617

1718
from pandas._typing import (
1819
HashableT,
@@ -32,7 +33,7 @@ class RangeIndex(_IndexSubclassBase[int, np.int64]):
3233
name: Hashable = ...,
3334
): ...
3435
@classmethod
35-
def from_range(cls, data, name: Hashable = ..., dtype=...): ...
36+
def from_range(cls, data, name: Hashable = ..., dtype=...) -> Self: ...
3637
def __reduce__(self): ...
3738
@property
3839
def start(self) -> int: ...

pandas-stubs/core/series.pyi

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ from pandas._libs.tslibs.nattype import NaTType
120120
from pandas._typing import (
121121
S1,
122122
S2,
123-
S2_CT,
124-
S2_CT_NDT,
125123
S2_NSDT,
126124
T_COMPLEX,
127125
AggFuncTypeBase,
@@ -180,6 +178,8 @@ from pandas._typing import (
180178
ReindexMethod,
181179
Renamer,
182180
ReplaceValue,
181+
S2_contra,
182+
S2_NDT_contra,
183183
Scalar,
184184
ScalarT,
185185
SequenceNotStr,
@@ -1725,14 +1725,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
17251725
) -> Series[Timedelta]: ...
17261726
@overload
17271727
def __add__(
1728-
self: Supports_ProtoAdd[S2_CT, S2], other: S2_CT | Sequence[S2_CT]
1728+
self: Supports_ProtoAdd[S2_contra, S2], other: S2_contra | Sequence[S2_contra]
17291729
) -> Series[S2]: ...
17301730
@overload
1731-
def __add__(self: Series[S2_CT], other: SupportsRAdd[S2_CT, S2]) -> Series[S2]: ...
1731+
def __add__(
1732+
self: Series[S2_contra], other: SupportsRAdd[S2_contra, S2]
1733+
) -> Series[S2]: ...
17321734
# pandas-dev/pandas#62353
17331735
@overload
17341736
def __add__(
1735-
self: Series[S2_CT_NDT], other: Sequence[SupportsRAdd[S2_CT_NDT, S2]]
1737+
self: Series[S2_NDT_contra], other: Sequence[SupportsRAdd[S2_NDT_contra, S2]]
17361738
) -> Series[S2]: ...
17371739
@overload
17381740
def __add__(
@@ -1835,16 +1837,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
18351837
) -> Series[Timedelta]: ...
18361838
@overload
18371839
def add(
1838-
self: Supports_ProtoAdd[S2_CT, S2],
1839-
other: S2_CT | Sequence[S2_CT],
1840+
self: Supports_ProtoAdd[S2_contra, S2],
1841+
other: S2_contra | Sequence[S2_contra],
18401842
level: Level | None = None,
18411843
fill_value: float | None = None,
18421844
axis: int = 0,
18431845
) -> Series[S2]: ...
18441846
@overload
18451847
def add(
1846-
self: Series[S2_CT],
1847-
other: SupportsRAdd[S2_CT, S2] | Sequence[SupportsRAdd[S2_CT, S2]],
1848+
self: Series[S2_contra],
1849+
other: SupportsRAdd[S2_contra, S2] | Sequence[SupportsRAdd[S2_contra, S2]],
18481850
level: Level | None = None,
18491851
fill_value: float | None = None,
18501852
axis: int = 0,
@@ -1952,14 +1954,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
19521954
# pyright is unhappy without the above 3 overloads
19531955
@overload
19541956
def __radd__(
1955-
self: Supports_ProtoRAdd[S2_CT, S2], other: S2_CT | Sequence[S2_CT]
1957+
self: Supports_ProtoRAdd[S2_contra, S2], other: S2_contra | Sequence[S2_contra]
19561958
) -> Series[S2]: ...
19571959
@overload
1958-
def __radd__(self: Series[S2_CT], other: SupportsAdd[S2_CT, S2]) -> Series[S2]: ...
1960+
def __radd__(
1961+
self: Series[S2_contra], other: SupportsAdd[S2_contra, S2]
1962+
) -> Series[S2]: ...
19591963
# pandas-dev/pandas#62353
19601964
@overload
19611965
def __radd__(
1962-
self: Series[S2_CT_NDT], other: Sequence[SupportsAdd[S2_CT_NDT, S2]]
1966+
self: Series[S2_NDT_contra], other: Sequence[SupportsAdd[S2_NDT_contra, S2]]
19631967
) -> Series[S2]: ...
19641968
@overload
19651969
def __radd__(
@@ -2066,16 +2070,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
20662070
) -> Series[Timedelta]: ...
20672071
@overload
20682072
def radd(
2069-
self: Supports_ProtoRAdd[S2_CT, S2],
2070-
other: S2_CT | Sequence[S2_CT],
2073+
self: Supports_ProtoRAdd[S2_contra, S2],
2074+
other: S2_contra | Sequence[S2_contra],
20712075
level: Level | None = None,
20722076
fill_value: float | None = None,
20732077
axis: int = 0,
20742078
) -> Series[S2]: ...
20752079
@overload
20762080
def radd(
2077-
self: Series[S2_CT],
2078-
other: SupportsAdd[S2_CT, S2] | Sequence[SupportsAdd[S2_CT, S2]],
2081+
self: Series[S2_contra],
2082+
other: SupportsAdd[S2_contra, S2] | Sequence[SupportsAdd[S2_contra, S2]],
20792083
level: Level | None = None,
20802084
fill_value: float | None = None,
20812085
axis: int = 0,
@@ -2532,8 +2536,11 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
25322536
) -> Series[S2]: ...
25332537
@overload
25342538
def __mul__(
2535-
self: Series[S2_CT],
2536-
other: SupportsRMul[S2_CT, S2_NSDT] | Sequence[SupportsRMul[S2_CT, S2_NSDT]],
2539+
self: Series[S2_contra],
2540+
other: (
2541+
SupportsRMul[S2_contra, S2_NSDT]
2542+
| Sequence[SupportsRMul[S2_contra, S2_NSDT]]
2543+
),
25372544
) -> Series[S2_NSDT]: ...
25382545
@overload
25392546
def __mul__(
@@ -2625,8 +2632,11 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
26252632
) -> Series[S2]: ...
26262633
@overload
26272634
def mul(
2628-
self: Series[S2_CT],
2629-
other: SupportsRMul[S2_CT, S2_NSDT] | Sequence[SupportsRMul[S2_CT, S2_NSDT]],
2635+
self: Series[S2_contra],
2636+
other: (
2637+
SupportsRMul[S2_contra, S2_NSDT]
2638+
| Sequence[SupportsRMul[S2_contra, S2_NSDT]]
2639+
),
26302640
level: Level | None = None,
26312641
fill_value: float | None = None,
26322642
axis: int = 0,
@@ -2742,8 +2752,10 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
27422752
) -> Series[S2]: ...
27432753
@overload
27442754
def __rmul__(
2745-
self: Series[S2_CT],
2746-
other: SupportsMul[S2_CT, S2_NSDT] | Sequence[SupportsMul[S2_CT, S2_NSDT]],
2755+
self: Series[S2_contra],
2756+
other: (
2757+
SupportsMul[S2_contra, S2_NSDT] | Sequence[SupportsMul[S2_contra, S2_NSDT]]
2758+
),
27472759
) -> Series[S2_NSDT]: ...
27482760
@overload
27492761
def __rmul__(
@@ -2835,8 +2847,10 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
28352847
) -> Series[S2]: ...
28362848
@overload
28372849
def rmul(
2838-
self: Series[S2_CT],
2839-
other: SupportsMul[S2_CT, S2_NSDT] | Sequence[SupportsMul[S2_CT, S2_NSDT]],
2850+
self: Series[S2_contra],
2851+
other: (
2852+
SupportsMul[S2_contra, S2_NSDT] | Sequence[SupportsMul[S2_contra, S2_NSDT]]
2853+
),
28402854
level: Level | None = None,
28412855
fill_value: float | None = None,
28422856
axis: int = 0,

0 commit comments

Comments
 (0)