Skip to content

Commit 3e562dd

Browse files
authored
ruff 0.0.284 (#54548)
* ruff 0.0.284 * fix strict type check * missing space
1 parent fc30823 commit 3e562dd

25 files changed

+93
-65
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
hooks:
2525
- id: black
2626
- repo: https://github.com/astral-sh/ruff-pre-commit
27-
rev: v0.0.282
27+
rev: v0.0.284
2828
hooks:
2929
- id: ruff
3030
args: [--exit-non-zero-on-fix]

pandas/core/arrays/base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def __contains__(self, item: object) -> bool | np.bool_:
479479
return (item == self).any() # type: ignore[union-attr]
480480

481481
# error: Signature of "__eq__" incompatible with supertype "object"
482-
def __eq__(self, other: Any) -> ArrayLike: # type: ignore[override]
482+
def __eq__(self, other: object) -> ArrayLike: # type: ignore[override]
483483
"""
484484
Return for `self == other` (element-wise equality).
485485
"""
@@ -492,11 +492,12 @@ def __eq__(self, other: Any) -> ArrayLike: # type: ignore[override]
492492
raise AbstractMethodError(self)
493493

494494
# error: Signature of "__ne__" incompatible with supertype "object"
495-
def __ne__(self, other: Any) -> ArrayLike: # type: ignore[override]
495+
def __ne__(self, other: object) -> ArrayLike: # type: ignore[override]
496496
"""
497497
Return for `self != other` (element-wise in-equality).
498498
"""
499-
return ~(self == other)
499+
# error: Unsupported operand type for ~ ("ExtensionArray")
500+
return ~(self == other) # type: ignore[operator]
500501

501502
def to_numpy(
502503
self,

pandas/core/arrays/string_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def _str_map(
574574
arr = np.asarray(self)
575575

576576
if is_integer_dtype(dtype) or is_bool_dtype(dtype):
577-
constructor: type[IntegerArray] | type[BooleanArray]
577+
constructor: type[IntegerArray | BooleanArray]
578578
if is_integer_dtype(dtype):
579579
constructor = IntegerArray
580580
else:

pandas/core/arrays/string_arrow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def _str_map(
262262
arr = np.asarray(self)
263263

264264
if is_integer_dtype(dtype) or is_bool_dtype(dtype):
265-
constructor: type[IntegerArray] | type[BooleanArray]
265+
constructor: type[IntegerArray | BooleanArray]
266266
if is_integer_dtype(dtype):
267267
constructor = IntegerArray
268268
else:

pandas/core/dtypes/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class property**.
110110
def __str__(self) -> str:
111111
return self.name
112112

113-
def __eq__(self, other: Any) -> bool:
113+
def __eq__(self, other: object) -> bool:
114114
"""
115115
Check whether 'other' is equal to self.
116116
@@ -144,7 +144,7 @@ def __hash__(self) -> int:
144144
# we need to avoid that and thus use hash function with old behavior
145145
return object_hash(tuple(getattr(self, attr) for attr in self._metadata))
146146

147-
def __ne__(self, other: Any) -> bool:
147+
def __ne__(self, other: object) -> bool:
148148
return not self.__eq__(other)
149149

150150
@property
@@ -422,7 +422,7 @@ def __repr__(self) -> str:
422422
def __str__(self) -> str:
423423
return self.name
424424

425-
def __eq__(self, other: Any) -> bool:
425+
def __eq__(self, other: object) -> bool:
426426
if isinstance(other, str) and other == self.name:
427427
return True
428428
return super().__eq__(other)

pandas/core/dtypes/dtypes.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def __hash__(self) -> int:
388388
# We *do* want to include the real self.ordered here
389389
return int(self._hash_categories)
390390

391-
def __eq__(self, other: Any) -> bool:
391+
def __eq__(self, other: object) -> bool:
392392
"""
393393
Rules for CDT equality:
394394
1) Any CDT is equal to the string 'category'
@@ -860,7 +860,7 @@ def __hash__(self) -> int:
860860
# TODO: update this.
861861
return hash(str(self))
862862

863-
def __eq__(self, other: Any) -> bool:
863+
def __eq__(self, other: object) -> bool:
864864
if isinstance(other, str):
865865
if other.startswith("M8["):
866866
other = f"datetime64[{other[3:]}"
@@ -1052,13 +1052,13 @@ def name(self) -> str_type:
10521052
def na_value(self) -> NaTType:
10531053
return NaT
10541054

1055-
def __eq__(self, other: Any) -> bool:
1055+
def __eq__(self, other: object) -> bool:
10561056
if isinstance(other, str):
10571057
return other in [self.name, self.name.title()]
10581058

10591059
return super().__eq__(other)
10601060

1061-
def __ne__(self, other: Any) -> bool:
1061+
def __ne__(self, other: object) -> bool:
10621062
return not self.__eq__(other)
10631063

10641064
@classmethod
@@ -1301,7 +1301,7 @@ def __hash__(self) -> int:
13011301
# make myself hashable
13021302
return hash(str(self))
13031303

1304-
def __eq__(self, other: Any) -> bool:
1304+
def __eq__(self, other: object) -> bool:
13051305
if isinstance(other, str):
13061306
return other.lower() in (self.name.lower(), str(self).lower())
13071307
elif not isinstance(other, IntervalDtype):
@@ -1647,7 +1647,7 @@ def __hash__(self) -> int:
16471647
# __eq__, so we explicitly do it here.
16481648
return super().__hash__()
16491649

1650-
def __eq__(self, other: Any) -> bool:
1650+
def __eq__(self, other: object) -> bool:
16511651
# We have to override __eq__ to handle NA values in _metadata.
16521652
# The base class does simple == checks, which fail for NA.
16531653
if isinstance(other, str):
@@ -2062,7 +2062,7 @@ def __hash__(self) -> int:
20622062
# make myself hashable
20632063
return hash(str(self))
20642064

2065-
def __eq__(self, other: Any) -> bool:
2065+
def __eq__(self, other: object) -> bool:
20662066
if not isinstance(other, type(self)):
20672067
return super().__eq__(other)
20682068
return self.pyarrow_dtype == other.pyarrow_dtype

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3416,7 +3416,7 @@ def to_xml(
34163416

34173417
lxml = import_optional_dependency("lxml.etree", errors="ignore")
34183418

3419-
TreeBuilder: type[EtreeXMLFormatter] | type[LxmlXMLFormatter]
3419+
TreeBuilder: type[EtreeXMLFormatter | LxmlXMLFormatter]
34203420

34213421
if parser == "lxml":
34223422
if lxml is not None:

pandas/core/indexes/base.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def _can_hold_strings(self) -> bool:
458458
@property
459459
def _engine_type(
460460
self,
461-
) -> type[libindex.IndexEngine] | type[libindex.ExtensionEngine]:
461+
) -> type[libindex.IndexEngine | libindex.ExtensionEngine]:
462462
return self._engine_types.get(self.dtype, libindex.ObjectEngine)
463463

464464
# whether we support partial string indexing. Overridden
@@ -481,7 +481,7 @@ def __new__(
481481
copy: bool = False,
482482
name=None,
483483
tupleize_cols: bool = True,
484-
) -> Index:
484+
) -> Self:
485485
from pandas.core.indexes.range import RangeIndex
486486

487487
name = maybe_extract_name(name, data, cls)
@@ -500,7 +500,9 @@ def __new__(
500500
result = RangeIndex(start=data, copy=copy, name=name)
501501
if dtype is not None:
502502
return result.astype(dtype, copy=False)
503-
return result
503+
# error: Incompatible return value type (got "MultiIndex",
504+
# expected "Self")
505+
return result # type: ignore[return-value]
504506

505507
elif is_ea_or_datetimelike_dtype(dtype):
506508
# non-EA dtype indexes have special casting logic, so we punt here
@@ -523,7 +525,7 @@ def __new__(
523525
elif is_scalar(data):
524526
raise cls._raise_scalar_data_error(data)
525527
elif hasattr(data, "__array__"):
526-
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name)
528+
return cls(np.asarray(data), dtype=dtype, copy=copy, name=name)
527529
elif not is_list_like(data) and not isinstance(data, memoryview):
528530
# 2022-11-16 the memoryview check is only necessary on some CI
529531
# builds, not clear why
@@ -540,7 +542,11 @@ def __new__(
540542
# 10697
541543
from pandas.core.indexes.multi import MultiIndex
542544

543-
return MultiIndex.from_tuples(data, names=name)
545+
# error: Incompatible return value type (got "MultiIndex",
546+
# expected "Self")
547+
return MultiIndex.from_tuples( # type: ignore[return-value]
548+
data, names=name
549+
)
544550
# other iterable of some kind
545551

546552
if not isinstance(data, (list, tuple)):

pandas/core/indexes/category.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from pandas._typing import (
4747
Dtype,
4848
DtypeObj,
49+
Self,
4950
npt,
5051
)
5152

@@ -210,7 +211,7 @@ def __new__(
210211
dtype: Dtype | None = None,
211212
copy: bool = False,
212213
name: Hashable | None = None,
213-
) -> CategoricalIndex:
214+
) -> Self:
214215
name = maybe_extract_name(name, data, cls)
215216

216217
if is_scalar(data):

pandas/core/indexes/frozen.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
"""
99
from __future__ import annotations
1010

11-
from typing import (
12-
Any,
13-
NoReturn,
14-
)
11+
from typing import NoReturn
1512

1613
from pandas.core.base import PandasObject
1714

@@ -80,7 +77,7 @@ def __radd__(self, other):
8077
other = list(other)
8178
return type(self)(other + list(self))
8279

83-
def __eq__(self, other: Any) -> bool:
80+
def __eq__(self, other: object) -> bool:
8481
if isinstance(other, (tuple, FrozenList)):
8582
other = list(other)
8683
return super().__eq__(other)

pandas/core/indexes/interval.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
Dtype,
9494
DtypeObj,
9595
IntervalClosedType,
96+
Self,
9697
npt,
9798
)
9899
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
@@ -225,7 +226,7 @@ def __new__(
225226
copy: bool = False,
226227
name: Hashable | None = None,
227228
verify_integrity: bool = True,
228-
) -> IntervalIndex:
229+
) -> Self:
229230
name = maybe_extract_name(name, data, cls)
230231

231232
with rewrite_exception("IntervalArray", cls.__name__):

pandas/core/indexes/multi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
IgnoreRaise,
3939
IndexLabel,
4040
Scalar,
41+
Self,
4142
Shape,
4243
npt,
4344
)
@@ -330,7 +331,7 @@ def __new__(
330331
copy: bool = False,
331332
name=None,
332333
verify_integrity: bool = True,
333-
) -> MultiIndex:
334+
) -> Self:
334335
# compat with Index
335336
if name is not None:
336337
names = name

pandas/core/indexes/range.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ def __new__(
139139
dtype: Dtype | None = None,
140140
copy: bool = False,
141141
name: Hashable | None = None,
142-
) -> RangeIndex:
142+
) -> Self:
143143
cls._validate_dtype(dtype)
144144
name = maybe_extract_name(name, start, cls)
145145

146146
# RangeIndex
147-
if isinstance(start, RangeIndex):
147+
if isinstance(start, cls):
148148
return start.copy(name=name)
149149
elif isinstance(start, range):
150150
return cls._simple_new(start, name=name)

pandas/core/tools/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class FulldatetimeDict(YearMonthDayDict, total=False):
133133
def _guess_datetime_format_for_array(arr, dayfirst: bool | None = False) -> str | None:
134134
# Try to guess the format based on the first non-NaN element, return None if can't
135135
if (first_non_null := tslib.first_non_null(arr)) != -1:
136-
if type(first_non_nan_element := arr[first_non_null]) is str:
136+
if type(first_non_nan_element := arr[first_non_null]) is str: # noqa: E721
137137
# GH#32264 np.str_ object
138138
guessed_format = guess_datetime_format(
139139
first_non_nan_element, dayfirst=dayfirst

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def to_numeric(
291291
IntegerArray,
292292
)
293293

294-
klass: type[IntegerArray] | type[BooleanArray] | type[FloatingArray]
294+
klass: type[IntegerArray | BooleanArray | FloatingArray]
295295
if is_integer_dtype(data.dtype):
296296
klass = IntegerArray
297297
elif is_bool_dtype(data.dtype):

pandas/io/common.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import tarfile
3131
from typing import (
3232
IO,
33+
TYPE_CHECKING,
3334
Any,
3435
AnyStr,
3536
DefaultDict,
@@ -51,13 +52,7 @@
5152

5253
from pandas._typing import (
5354
BaseBuffer,
54-
CompressionDict,
55-
CompressionOptions,
56-
FilePath,
57-
ReadBuffer,
5855
ReadCsvBuffer,
59-
StorageOptions,
60-
WriteBuffer,
6156
)
6257
from pandas.compat import (
6358
get_bz2_file,
@@ -84,6 +79,19 @@
8479
BaseBufferT = TypeVar("BaseBufferT", bound=BaseBuffer)
8580

8681

82+
if TYPE_CHECKING:
83+
from types import TracebackType
84+
85+
from pandas._typing import (
86+
CompressionDict,
87+
CompressionOptions,
88+
FilePath,
89+
ReadBuffer,
90+
StorageOptions,
91+
WriteBuffer,
92+
)
93+
94+
8795
@dataclasses.dataclass
8896
class IOArgs:
8997
"""
@@ -138,7 +146,12 @@ def close(self) -> None:
138146
def __enter__(self) -> IOHandles[AnyStr]:
139147
return self
140148

141-
def __exit__(self, *args: Any) -> None:
149+
def __exit__(
150+
self,
151+
exc_type: type[BaseException] | None,
152+
exc_value: BaseException | None,
153+
traceback: TracebackType | None,
154+
) -> None:
142155
self.close()
143156

144157

pandas/io/parsers/readers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
HashableT,
8080
IndexLabel,
8181
ReadCsvBuffer,
82+
Self,
8283
StorageOptions,
8384
)
8485
_doc_read_csv_and_table = (
@@ -1776,7 +1777,7 @@ def get_chunk(self, size: int | None = None) -> DataFrame:
17761777
size = min(size, self.nrows - self._currow)
17771778
return self.read(nrows=size)
17781779

1779-
def __enter__(self) -> TextFileReader:
1780+
def __enter__(self) -> Self:
17801781
return self
17811782

17821783
def __exit__(

0 commit comments

Comments
 (0)