Skip to content

Commit 3c658b8

Browse files
twoertweinmroeschke
authored andcommitted
ruff 0.0.284 (pandas-dev#54548)
* ruff 0.0.284 * fix strict type check * missing space
1 parent bc0002c commit 3c658b8

24 files changed

+91
-60
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
@@ -457,7 +457,7 @@ def _can_hold_strings(self) -> bool:
457457
@property
458458
def _engine_type(
459459
self,
460-
) -> type[libindex.IndexEngine] | type[libindex.ExtensionEngine]:
460+
) -> type[libindex.IndexEngine | libindex.ExtensionEngine]:
461461
return self._engine_types.get(self.dtype, libindex.ObjectEngine)
462462

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

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

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

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

545551
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/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
)
@@ -329,7 +330,7 @@ def __new__(
329330
copy: bool = False,
330331
name=None,
331332
verify_integrity: bool = True,
332-
) -> MultiIndex:
333+
) -> Self:
333334
# compat with Index
334335
if name is not None:
335336
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__(

pandas/io/pytables.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ def _create_storer(
17311731
errors: str = "strict",
17321732
) -> GenericFixed | Table:
17331733
"""return a suitable class to operate"""
1734-
cls: type[GenericFixed] | type[Table]
1734+
cls: type[GenericFixed | Table]
17351735

17361736
if value is not None and not isinstance(value, (Series, DataFrame)):
17371737
raise TypeError("value must be None, Series, or DataFrame")
@@ -2119,7 +2119,7 @@ def __repr__(self) -> str:
21192119
]
21202120
)
21212121

2122-
def __eq__(self, other: Any) -> bool:
2122+
def __eq__(self, other: object) -> bool:
21232123
"""compare 2 col items"""
21242124
return all(
21252125
getattr(self, a, None) == getattr(other, a, None)
@@ -2160,7 +2160,7 @@ def convert(
21602160
if self.freq is not None:
21612161
kwargs["freq"] = _ensure_decoded(self.freq)
21622162

2163-
factory: type[Index] | type[DatetimeIndex] = Index
2163+
factory: type[Index | DatetimeIndex] = Index
21642164
if lib.is_np_dtype(values.dtype, "M") or isinstance(
21652165
values.dtype, DatetimeTZDtype
21662166
):
@@ -2426,7 +2426,7 @@ def __repr__(self) -> str:
24262426
]
24272427
)
24282428

2429-
def __eq__(self, other: Any) -> bool:
2429+
def __eq__(self, other: object) -> bool:
24302430
"""compare 2 col items"""
24312431
return all(
24322432
getattr(self, a, None) == getattr(other, a, None)

0 commit comments

Comments
 (0)