Skip to content

Commit 7424d91

Browse files
topper-123yeshsurya
authored andcommitted
TYP: cleanup typing in core.indexes.range.py (pandas-dev#41179)
1 parent 1f92c47 commit 7424d91

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
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

+15-29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Any,
99
Callable,
1010
Hashable,
11+
List,
12+
cast,
1113
)
1214
import warnings
1315

@@ -110,13 +112,7 @@ def __new__(
110112
copy: bool = False,
111113
name: Hashable = None,
112114
) -> 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]
115+
cls._validate_dtype(dtype)
120116
name = maybe_extract_name(name, start, cls)
121117

122118
# RangeIndex
@@ -159,13 +155,7 @@ def from_range(
159155
f"{cls.__name__}(...) must be called with object coercible to a "
160156
f"range, {repr(data)} was passed"
161157
)
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]
158+
cls._validate_dtype(dtype)
169159
return cls._simple_new(data, name=name)
170160

171161
@classmethod
@@ -439,9 +429,8 @@ def _get_indexer(
439429
def repeat(self, repeats, axis=None) -> Int64Index:
440430
return self._int64index.repeat(repeats, axis=axis)
441431

442-
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+
def delete(self, loc) -> Int64Index: # type: ignore[override]
433+
return self._int64index.delete(loc)
445434

446435
def take(
447436
self, indices, axis: int = 0, allow_fill: bool = True, fill_value=None, **kwargs
@@ -761,7 +750,7 @@ def symmetric_difference(self, other, result_name: Hashable = None, sort=None):
761750

762751
# --------------------------------------------------------------------
763752

764-
def _concat(self, indexes: list[Index], name: Hashable):
753+
def _concat(self, indexes: list[Index], name: Hashable) -> Index:
765754
"""
766755
Overriding parent method for the case of all RangeIndex instances.
767756
@@ -776,14 +765,15 @@ def _concat(self, indexes: list[Index], name: Hashable):
776765
elif len(indexes) == 1:
777766
return indexes[0]
778767

768+
rng_indexes = cast(List[RangeIndex], indexes)
769+
779770
start = step = next_ = None
780771

781772
# Filter the empty indexes
782-
non_empty_indexes = [obj for obj in indexes if len(obj)]
773+
non_empty_indexes = [obj for obj in rng_indexes if len(obj)]
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
@@ -793,7 +783,8 @@ def _concat(self, indexes: list[Index], name: Hashable):
793783
elif step is None:
794784
# First non-empty index had only one element
795785
if rng.start == start:
796-
result = Int64Index(np.concatenate([x._values for x in indexes]))
786+
values = np.concatenate([x._values for x in rng_indexes])
787+
result = Int64Index(values)
797788
return result.rename(name)
798789

799790
step = rng.start - start
@@ -802,7 +793,7 @@ def _concat(self, indexes: list[Index], name: Hashable):
802793
next_ is not None and rng.start != next_
803794
)
804795
if non_consecutive:
805-
result = Int64Index(np.concatenate([x._values for x in indexes]))
796+
result = Int64Index(np.concatenate([x._values for x in rng_indexes]))
806797
return result.rename(name)
807798

808799
if step is not None:
@@ -811,12 +802,7 @@ def _concat(self, indexes: list[Index], name: Hashable):
811802
if non_empty_indexes:
812803
# Get the stop value from "next" or alternatively
813804
# 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-
)
805+
stop = non_empty_indexes[-1].stop if next_ is None else next_
820806
return RangeIndex(start, stop, step).rename(name)
821807

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

0 commit comments

Comments
 (0)