Skip to content

Commit 42192e0

Browse files
authored
refactor(ruff): comply to PTH and TD003 (#1433)
* PTH * TD003 * mypy happiness * mypy happiness * mypy happiness * Revert "mypy happiness" This reverts commit 9af8c9e. * mypy happiness * fix(comment): https://github.com/pandas-dev/pandas-stubs/pull/1433/files#r2443621649
1 parent 58de93c commit 42192e0

File tree

9 files changed

+74
-80
lines changed

9 files changed

+74
-80
lines changed

pandas-stubs/_libs/tslibs/period.pyi

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import datetime
2+
import sys
23
from typing import (
34
Literal,
45
TypeAlias,
56
overload,
67
)
78

89
import numpy as np
9-
from pandas import (
10-
Index,
11-
PeriodIndex,
12-
Series,
13-
Timedelta,
14-
TimedeltaIndex,
15-
)
10+
from pandas.core.indexes.base import Index
11+
from pandas.core.indexes.period import PeriodIndex
12+
from pandas.core.indexes.timedeltas import TimedeltaIndex
13+
from pandas.core.series import Series
1614
from typing_extensions import Self
1715

1816
from pandas._libs.tslibs import NaTType
19-
from pandas._libs.tslibs.offsets import (
20-
BaseOffset,
21-
)
17+
from pandas._libs.tslibs.offsets import BaseOffset
18+
from pandas._libs.tslibs.timedeltas import Timedelta
2219
from pandas._libs.tslibs.timestamps import Timestamp
2320
from pandas._typing import (
2421
PeriodFrequency,
@@ -79,7 +76,7 @@ class Period(PeriodMixin):
7976
@overload
8077
def __sub__(self, other: _PeriodAddSub) -> Period: ...
8178
@overload
82-
def __sub__(self, other: Period) -> BaseOffset: ...
79+
def __sub__(self, other: Self) -> BaseOffset: ...
8380
@overload
8481
def __sub__(self, other: NaTType) -> NaTType: ...
8582
@overload
@@ -98,8 +95,13 @@ class Period(PeriodMixin):
9895
def __add__(self, other: Index) -> PeriodIndex: ...
9996
# Ignored due to indecipherable error from mypy:
10097
# Forward operator "__add__" is not callable [misc]
101-
@overload
102-
def __radd__(self, other: _PeriodAddSub) -> Self: ... # type: ignore[misc]
98+
if sys.version_info >= (3, 11):
99+
@overload
100+
def __radd__(self, other: _PeriodAddSub) -> Self: ...
101+
else:
102+
@overload
103+
def __radd__(self, other: _PeriodAddSub) -> Self: ... # type: ignore[misc]
104+
103105
@overload
104106
def __radd__(self, other: NaTType) -> NaTType: ...
105107
# Real signature is -> PeriodIndex, but conflicts with Index.__add__
@@ -111,7 +113,7 @@ class Period(PeriodMixin):
111113
# ignore[misc] here because we know all other comparisons
112114
# are False, so we use Literal[False]
113115
@overload
114-
def __eq__(self, other: Period) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
116+
def __eq__(self, other: Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
115117
@overload
116118
def __eq__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
117119
@overload
@@ -121,7 +123,7 @@ class Period(PeriodMixin):
121123
@overload
122124
def __eq__(self, other: object) -> Literal[False]: ...
123125
@overload
124-
def __ge__(self, other: Period) -> bool: ...
126+
def __ge__(self, other: Self) -> bool: ...
125127
@overload
126128
def __ge__(self, other: PeriodIndex) -> np_1darray[np.bool]: ...
127129
@overload
@@ -133,7 +135,7 @@ class Period(PeriodMixin):
133135
self, other: np_ndarray[ShapeT, np.object_]
134136
) -> np_ndarray[ShapeT, np.bool]: ...
135137
@overload
136-
def __gt__(self, other: Period) -> bool: ...
138+
def __gt__(self, other: Self) -> bool: ...
137139
@overload
138140
def __gt__(self, other: PeriodIndex) -> np_1darray[np.bool]: ...
139141
@overload
@@ -145,7 +147,7 @@ class Period(PeriodMixin):
145147
self, other: np_ndarray[ShapeT, np.object_]
146148
) -> np_ndarray[ShapeT, np.bool]: ...
147149
@overload
148-
def __le__(self, other: Period) -> bool: ...
150+
def __le__(self, other: Self) -> bool: ...
149151
@overload
150152
def __le__(self, other: PeriodIndex) -> np_1darray[np.bool]: ...
151153
@overload
@@ -157,7 +159,7 @@ class Period(PeriodMixin):
157159
self, other: np_ndarray[ShapeT, np.object_]
158160
) -> np_ndarray[ShapeT, np.bool]: ...
159161
@overload
160-
def __lt__(self, other: Period) -> bool: ...
162+
def __lt__(self, other: Self) -> bool: ...
161163
@overload
162164
def __lt__(self, other: PeriodIndex) -> np_1darray[np.bool]: ...
163165
@overload
@@ -171,7 +173,7 @@ class Period(PeriodMixin):
171173
# ignore[misc] here because we know all other comparisons
172174
# are False, so we use Literal[False]
173175
@overload
174-
def __ne__(self, other: Period) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
176+
def __ne__(self, other: Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
175177
@overload
176178
def __ne__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
177179
@overload

pandas-stubs/_libs/tslibs/timedeltas.pyi

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@ from typing import (
1313
)
1414

1515
import numpy as np
16-
import pandas as pd
17-
from pandas import (
18-
DatetimeIndex,
19-
Index,
20-
PeriodIndex,
21-
Series,
22-
TimedeltaIndex,
23-
)
16+
from pandas.core.indexes.base import Index
17+
from pandas.core.indexes.datetimes import DatetimeIndex
18+
from pandas.core.indexes.period import PeriodIndex
19+
from pandas.core.indexes.timedeltas import TimedeltaIndex
20+
from pandas.core.series import Series
2421
from typing_extensions import Self
2522

26-
from pandas._libs.tslibs import (
27-
NaTType,
28-
)
23+
from pandas._libs.tslibs import NaTType
2924
from pandas._libs.tslibs.period import Period
3025
from pandas._libs.tslibs.timestamps import Timestamp
3126
from pandas._typing import (
@@ -131,7 +126,7 @@ class Timedelta(timedelta):
131126
def to_timedelta64(self) -> np.timedelta64: ...
132127
@property
133128
def asm8(self) -> np.timedelta64: ...
134-
# TODO: round/floor/ceil could return NaT?
129+
# TODO: pandas-dev/pandas-stubs#1432 round/floor/ceil could return NaT?
135130
def round(self, freq: Frequency) -> Self: ...
136131
def floor(self, freq: Frequency) -> Self: ...
137132
def ceil(self, freq: Frequency) -> Self: ...
@@ -174,17 +169,17 @@ class Timedelta(timedelta):
174169
) -> np_ndarray[ShapeT, np.datetime64]: ...
175170
# Override due to more types supported than timedelta
176171
@overload # type: ignore[override]
177-
def __sub__(self, other: timedelta | Timedelta | np.timedelta64) -> Self: ...
172+
def __sub__(self, other: timedelta | np.timedelta64 | Self) -> Self: ...
178173
@overload
179174
def __sub__(self, other: NaTType) -> NaTType: ...
180175
@overload
181176
def __sub__(
182177
self, other: np_ndarray[ShapeT, np.timedelta64]
183178
) -> np_ndarray[ShapeT, np.timedelta64]: ...
184179
@overload
185-
def __sub__(self, other: pd.TimedeltaIndex) -> TimedeltaIndex: ...
180+
def __sub__(self, other: TimedeltaIndex) -> TimedeltaIndex: ...
186181
@overload
187-
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Self: ...
182+
def __rsub__(self, other: timedelta | np.timedelta64 | Self) -> Self: ...
188183
@overload
189184
def __rsub__(self, other: datetime | Timestamp | np.datetime64) -> Timestamp: ... # type: ignore[misc]
190185
@overload
@@ -204,7 +199,7 @@ class Timedelta(timedelta):
204199
self, other: np_ndarray[ShapeT, np.timedelta64]
205200
) -> np_ndarray[ShapeT, np.timedelta64]: ...
206201
@overload
207-
def __rsub__(self, other: pd.TimedeltaIndex) -> pd.TimedeltaIndex: ...
202+
def __rsub__(self, other: TimedeltaIndex) -> TimedeltaIndex: ...
208203
def __neg__(self) -> Self: ...
209204
def __pos__(self) -> Self: ...
210205
def __abs__(self) -> Self: ...
@@ -224,7 +219,7 @@ class Timedelta(timedelta):
224219
# Override due to more types supported than timedelta
225220
# error: Signature of "__floordiv__" incompatible with supertype "timedelta"
226221
@overload # type: ignore[override]
227-
def __floordiv__(self, other: timedelta | Timedelta | np.timedelta64) -> int: ...
222+
def __floordiv__(self, other: timedelta | np.timedelta64 | Self) -> int: ...
228223
@overload
229224
def __floordiv__(self, other: float) -> Self: ...
230225
@overload
@@ -273,9 +268,9 @@ class Timedelta(timedelta):
273268
def __rtruediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
274269
# Override due to more types supported than timedelta
275270
@overload
276-
def __eq__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
271+
def __eq__(self, other: timedelta | np.timedelta64 | Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
277272
@overload
278-
def __eq__(self, other: Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
273+
def __eq__(self, other: Series[Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
279274
@overload
280275
def __eq__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
281276
@overload
@@ -286,9 +281,9 @@ class Timedelta(timedelta):
286281
def __eq__(self, other: object) -> Literal[False]: ...
287282
# Override due to more types supported than timedelta
288283
@overload
289-
def __ne__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
284+
def __ne__(self, other: timedelta | np.timedelta64 | Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
290285
@overload
291-
def __ne__(self, other: Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
286+
def __ne__(self, other: Series[Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
292287
@overload
293288
def __ne__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
294289
@overload
@@ -315,52 +310,50 @@ class Timedelta(timedelta):
315310
self, other: Series[int] | Series[float] | Series[Timedelta]
316311
) -> Series[Timedelta]: ...
317312
def __divmod__(self, other: timedelta) -> tuple[int, Timedelta]: ...
318-
# Mypy complains Forward operator "<inequality op>" is not callable, so ignore misc
319-
# for le, lt ge and gt
320313
# Override due to more types supported than timedelta
321314
@overload # type: ignore[override]
322-
def __le__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
315+
def __le__(self, other: timedelta | np.timedelta64 | Self) -> bool: ...
323316
@overload
324317
def __le__(self, other: TimedeltaIndex) -> np_1darray[np.bool]: ...
325318
@overload
326319
def __le__(
327320
self, other: np_ndarray[ShapeT, np.timedelta64]
328321
) -> np_ndarray[ShapeT, np.bool_]: ...
329322
@overload
330-
def __le__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
323+
def __le__(self, other: Series[Timedelta]) -> Series[bool]: ...
331324
# Override due to more types supported than timedelta
332325
@overload # type: ignore[override]
333-
def __lt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
326+
def __lt__(self, other: timedelta | np.timedelta64 | Self) -> bool: ...
334327
@overload
335328
def __lt__(self, other: TimedeltaIndex) -> np_1darray[np.bool]: ...
336329
@overload
337330
def __lt__(
338331
self, other: np_ndarray[ShapeT, np.timedelta64]
339332
) -> np_ndarray[ShapeT, np.bool_]: ...
340333
@overload
341-
def __lt__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
334+
def __lt__(self, other: Series[Timedelta]) -> Series[bool]: ...
342335
# Override due to more types supported than timedelta
343336
@overload # type: ignore[override]
344-
def __ge__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
337+
def __ge__(self, other: timedelta | np.timedelta64 | Self) -> bool: ...
345338
@overload
346339
def __ge__(self, other: TimedeltaIndex) -> np_1darray[np.bool]: ...
347340
@overload
348341
def __ge__(
349342
self, other: np_ndarray[ShapeT, np.timedelta64]
350343
) -> np_ndarray[ShapeT, np.bool_]: ...
351344
@overload
352-
def __ge__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
345+
def __ge__(self, other: Series[Timedelta]) -> Series[bool]: ...
353346
# Override due to more types supported than timedelta
354347
@overload # type: ignore[override]
355-
def __gt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
348+
def __gt__(self, other: timedelta | np.timedelta64 | Self) -> bool: ...
356349
@overload
357350
def __gt__(self, other: TimedeltaIndex) -> np_1darray[np.bool]: ...
358351
@overload
359352
def __gt__(
360353
self, other: np_ndarray[ShapeT, np.timedelta64]
361354
) -> np_ndarray[ShapeT, np.bool_]: ...
362355
@overload
363-
def __gt__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
356+
def __gt__(self, other: Series[Timedelta]) -> Series[bool]: ...
364357
def __hash__(self) -> int: ...
365358
def isoformat(self) -> str: ...
366359
def to_numpy(self) -> np.timedelta64: ...

pandas-stubs/_libs/tslibs/timestamps.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Timestamp(datetime, SupportsIndex):
175175
# Mypy complains Forward operator "<inequality op>" is not callable, so ignore misc
176176
# for le, lt ge and gt
177177
@overload # type: ignore[override]
178-
def __le__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
178+
def __le__(self, other: datetime | np.datetime64 | Self) -> bool: ...
179179
@overload
180180
def __le__(self, other: DatetimeIndex) -> np_1darray[np.bool]: ...
181181
@overload
@@ -185,7 +185,7 @@ class Timestamp(datetime, SupportsIndex):
185185
@overload
186186
def __le__(self, other: Series[Timestamp]) -> Series[bool]: ...
187187
@overload # type: ignore[override]
188-
def __lt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
188+
def __lt__(self, other: datetime | np.datetime64 | Self) -> bool: ...
189189
@overload
190190
def __lt__(self, other: DatetimeIndex) -> np_1darray[np.bool]: ...
191191
@overload
@@ -195,7 +195,7 @@ class Timestamp(datetime, SupportsIndex):
195195
@overload
196196
def __lt__(self, other: Series[Timestamp]) -> Series[bool]: ...
197197
@overload # type: ignore[override]
198-
def __ge__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
198+
def __ge__(self, other: datetime | np.datetime64 | Self) -> bool: ...
199199
@overload
200200
def __ge__(self, other: DatetimeIndex) -> np_1darray[np.bool]: ...
201201
@overload
@@ -205,7 +205,7 @@ class Timestamp(datetime, SupportsIndex):
205205
@overload
206206
def __ge__(self, other: Series[Timestamp]) -> Series[bool]: ...
207207
@overload # type: ignore[override]
208-
def __gt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
208+
def __gt__(self, other: datetime | np.datetime64 | Self) -> bool: ...
209209
@overload
210210
def __gt__(self, other: DatetimeIndex) -> np_1darray[np.bool]: ...
211211
@overload
@@ -231,7 +231,7 @@ class Timestamp(datetime, SupportsIndex):
231231
def __radd__(
232232
self, other: np_ndarray[ShapeT, np.timedelta64]
233233
) -> np_ndarray[ShapeT, np.datetime64]: ...
234-
# TODO: test dt64
234+
# TODO: pandas-dev/pandas-stubs#1432 test dt64
235235
@overload # type: ignore[override]
236236
def __sub__(self, other: datetime | np.datetime64) -> Timedelta: ...
237237
@overload
@@ -243,7 +243,7 @@ class Timestamp(datetime, SupportsIndex):
243243
self, other: np_ndarray[ShapeT, np.timedelta64]
244244
) -> np_ndarray[ShapeT, np.datetime64]: ...
245245
@overload
246-
def __eq__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
246+
def __eq__(self, other: datetime | np.datetime64 | Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
247247
@overload
248248
def __eq__(self, other: Series[Timestamp]) -> Series[bool]: ... # type: ignore[overload-overlap]
249249
@overload
@@ -253,7 +253,7 @@ class Timestamp(datetime, SupportsIndex):
253253
@overload
254254
def __eq__(self, other: object) -> Literal[False]: ...
255255
@overload
256-
def __ne__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
256+
def __ne__(self, other: datetime | np.datetime64 | Self) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
257257
@overload
258258
def __ne__(self, other: Series[Timestamp]) -> Series[bool]: ... # type: ignore[overload-overlap]
259259
@overload
@@ -287,15 +287,15 @@ class Timestamp(datetime, SupportsIndex):
287287
@property
288288
def asm8(self) -> np.datetime64: ...
289289
def tz_convert(self, tz: TimeZones) -> Self: ...
290-
# TODO: could return NaT?
290+
# TODO: pandas-dev/pandas-stubs#1432 could return NaT?
291291
def tz_localize(
292292
self,
293293
tz: TimeZones,
294294
ambiguous: _Ambiguous = "raise",
295295
nonexistent: TimestampNonexistent = "raise",
296296
) -> Self: ...
297297
def normalize(self) -> Self: ...
298-
# TODO: round/floor/ceil could return NaT?
298+
# TODO: pandas-dev/pandas-stubs#1432 round/floor/ceil could return NaT?
299299
def round(
300300
self,
301301
freq: str,

pandas-stubs/io/orc.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def read_orc(
1414
path: FilePath | ReadBuffer[bytes],
1515
columns: list[HashableT] | None = None,
1616
dtype_backend: DtypeBackend | _NoDefaultDoNotUse = "numpy_nullable",
17-
# TODO: type with the correct pyarrow types
17+
# TODO: pandas-dev/pandas-stubs#1432 type with the correct pyarrow types
1818
# filesystem: pyarrow.fs.FileSystem | fsspec.spec.AbstractFileSystem
1919
filesystem: Any | None = None,
2020
**kwargs: Any,

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ fix = true
178178
[tool.ruff.lint]
179179
extend-select = ["ALL"]
180180
ignore = [
181+
# The following rules are ignored permanently for good reasons
181182
"COM", # https://docs.astral.sh/ruff/rules/#flake8-commas-com
182183
"D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
183184
"E501", # handled by black https://docs.astral.sh/ruff/rules/line-too-long/
@@ -199,10 +200,10 @@ ignore = [
199200
"A004", # https://docs.astral.sh/ruff/rules/builtin-import-shadowing/
200201
"PYI001", # https://docs.astral.sh/ruff/rules/unprefixed-type-param/
201202
"PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/
202-
"TD003", # https://docs.astral.sh/ruff/rules/missing-todo-link/
203203
"ERA001", "ANN001", "ANN201", "ANN204", "ANN206", "ANN401", "PLR0402", "PLC0105"
204204
]
205205
"scripts/*" = [
206+
# The following rules are ignored permanently for good reasons
206207
"EM", # https://docs.astral.sh/ruff/rules/#flake8-errmsg-em
207208
"S603", # https://docs.astral.sh/ruff/rules/subprocess-without-shell-equals-true/
208209
]
@@ -220,7 +221,7 @@ ignore = [
220221
"A001", # https://docs.astral.sh/ruff/rules/builtin-variable-shadowing/
221222
"PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/
222223
"SLF001", # https://docs.astral.sh/ruff/rules/private-member-access/
223-
"ANN001", "ANN201", "ANN204", "ANN206", "ANN401", "ARG", "ERA", "RUF", "SIM", "TRY", "PT", "NPY", "N", "DTZ", "PLR", "TC", "PGH", "PTH", "S311", "C901"
224+
"ANN001", "ANN201", "ANN204", "ANN206", "ANN401", "ARG", "ERA", "RUF", "SIM", "TRY", "PT", "NPY", "N", "DTZ", "PLR", "TC", "PGH", "S311", "C901"
224225
]
225226
"tests/test_io.py" = [
226227
# The following rules are ignored permanently for good reasons

tests/test_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ def test_types_to_feather() -> None:
19921992
# See https://pandas.pydata.org/docs/whatsnew/v1.0.0.html
19931993
# Docstring and type were updated in 1.2.0.
19941994
# https://github.com/pandas-dev/pandas/pull/35408
1995-
with open(path, mode="wb") as file:
1995+
with Path(path).open("wb") as file:
19961996
df.to_feather(file)
19971997

19981998

0 commit comments

Comments
 (0)