Skip to content

Commit a5e661a

Browse files
committed
TYP: cleanup typing in core.indexes.range.py
1 parent 0158382 commit a5e661a

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5874,7 +5874,7 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
58745874

58755875
return start_slice, end_slice
58765876

5877-
def delete(self, loc) -> Index:
5877+
def delete(self: _IndexT, loc) -> _IndexT:
58785878
"""
58795879
Make new Index with passed location(-s) deleted.
58805880

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
9898
return subarr
9999

100100
@classmethod
101-
def _validate_dtype(cls, dtype: Dtype) -> None:
101+
def _validate_dtype(cls, dtype: Dtype | None) -> None:
102102
if dtype is None:
103103
return
104104

pandas/core/indexes/range.py

+10-27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Any,
99
Callable,
1010
Hashable,
11+
cast,
1112
)
1213
import warnings
1314

@@ -110,13 +111,7 @@ def __new__(
110111
copy: bool = False,
111112
name: Hashable = None,
112113
) -> RangeIndex:
113-
114-
# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
115-
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
116-
# Type[complex], Type[bool], Type[object], None]"; expected
117-
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
118-
# Type[int], Type[complex], Type[bool], Type[object]]"
119-
cls._validate_dtype(dtype) # type: ignore[arg-type]
114+
cls._validate_dtype(dtype)
120115
name = maybe_extract_name(name, start, cls)
121116

122117
# RangeIndex
@@ -159,13 +154,7 @@ def from_range(
159154
f"{cls.__name__}(...) must be called with object coercible to a "
160155
f"range, {repr(data)} was passed"
161156
)
162-
163-
# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
164-
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
165-
# Type[complex], Type[bool], Type[object], None]"; expected
166-
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
167-
# Type[int], Type[complex], Type[bool], Type[object]]"
168-
cls._validate_dtype(dtype) # type: ignore[arg-type]
157+
cls._validate_dtype(dtype)
169158
return cls._simple_new(data, name=name)
170159

171160
@classmethod
@@ -440,8 +429,7 @@ def repeat(self, repeats, axis=None) -> Int64Index:
440429
return self._int64index.repeat(repeats, axis=axis)
441430

442431
def delete(self, loc) -> Int64Index:
443-
# error: Incompatible return value type (got "Index", expected "Int64Index")
444-
return self._int64index.delete(loc) # type: ignore[return-value]
432+
return self._int64index.delete(loc)
445433

446434
def take(
447435
self, indices, axis: int = 0, allow_fill: bool = True, fill_value=None, **kwargs
@@ -779,11 +767,13 @@ def _concat(self, indexes: list[Index], name: Hashable):
779767
start = step = next_ = None
780768

781769
# Filter the empty indexes
782-
non_empty_indexes = [obj for obj in indexes if len(obj)]
770+
assert all(isinstance(x, RangeIndex) for x in indexes)
771+
non_empty_indexes: list[RangeIndex] = [
772+
cast(RangeIndex, obj) for obj in indexes if len(obj)
773+
]
783774

784775
for obj in non_empty_indexes:
785-
# error: "Index" has no attribute "_range"
786-
rng: range = obj._range # type: ignore[attr-defined]
776+
rng = obj._range
787777

788778
if start is None:
789779
# This is set by the first non-empty index
@@ -809,14 +799,7 @@ def _concat(self, indexes: list[Index], name: Hashable):
809799
next_ = rng[-1] + step
810800

811801
if non_empty_indexes:
812-
# Get the stop value from "next" or alternatively
813-
# from the last non-empty index
814-
# error: "Index" has no attribute "stop"
815-
stop = (
816-
non_empty_indexes[-1].stop # type: ignore[attr-defined]
817-
if next_ is None
818-
else next_
819-
)
802+
stop = non_empty_indexes[-1].stop if next_ is None else next_
820803
return RangeIndex(start, stop, step).rename(name)
821804

822805
# Here all "indexes" had 0 length, i.e. were empty.

0 commit comments

Comments
 (0)