Skip to content

TYP: cleanup typing in core.indexes.range.py #41179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5874,7 +5874,7 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):

return start_slice, end_slice

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
return subarr

@classmethod
def _validate_dtype(cls, dtype: Dtype) -> None:
def _validate_dtype(cls, dtype: Dtype | None) -> None:
if dtype is None:
return

Expand Down
44 changes: 15 additions & 29 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Any,
Callable,
Hashable,
List,
cast,
)
import warnings

Expand Down Expand Up @@ -110,13 +112,7 @@ def __new__(
copy: bool = False,
name: Hashable = None,
) -> RangeIndex:

# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
# Type[int], Type[complex], Type[bool], Type[object]]"
cls._validate_dtype(dtype) # type: ignore[arg-type]
cls._validate_dtype(dtype)
name = maybe_extract_name(name, start, cls)

# RangeIndex
Expand Down Expand Up @@ -159,13 +155,7 @@ def from_range(
f"{cls.__name__}(...) must be called with object coercible to a "
f"range, {repr(data)} was passed"
)

# error: Argument 1 to "_validate_dtype" of "NumericIndex" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int],
# Type[complex], Type[bool], Type[object], None]"; expected
# "Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], Type[float],
# Type[int], Type[complex], Type[bool], Type[object]]"
cls._validate_dtype(dtype) # type: ignore[arg-type]
cls._validate_dtype(dtype)
return cls._simple_new(data, name=name)

@classmethod
Expand Down Expand Up @@ -439,9 +429,8 @@ def _get_indexer(
def repeat(self, repeats, axis=None) -> Int64Index:
return self._int64index.repeat(repeats, axis=axis)

def delete(self, loc) -> Int64Index:
# error: Incompatible return value type (got "Index", expected "Int64Index")
return self._int64index.delete(loc) # type: ignore[return-value]
def delete(self, loc) -> Int64Index: # type: ignore[override]
return self._int64index.delete(loc)

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

# --------------------------------------------------------------------

def _concat(self, indexes: list[Index], name: Hashable):
def _concat(self, indexes: list[Index], name: Hashable) -> Index:
"""
Overriding parent method for the case of all RangeIndex instances.

Expand All @@ -776,14 +765,15 @@ def _concat(self, indexes: list[Index], name: Hashable):
elif len(indexes) == 1:
return indexes[0]

rng_indexes = cast(List[RangeIndex], indexes)

start = step = next_ = None

# Filter the empty indexes
non_empty_indexes = [obj for obj in indexes if len(obj)]
non_empty_indexes = [obj for obj in rng_indexes if len(obj)]

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

if start is None:
# This is set by the first non-empty index
Expand All @@ -793,7 +783,8 @@ def _concat(self, indexes: list[Index], name: Hashable):
elif step is None:
# First non-empty index had only one element
if rng.start == start:
result = Int64Index(np.concatenate([x._values for x in indexes]))
values = np.concatenate([x._values for x in rng_indexes])
result = Int64Index(values)
return result.rename(name)

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

if step is not None:
Expand All @@ -811,12 +802,7 @@ def _concat(self, indexes: list[Index], name: Hashable):
if non_empty_indexes:
# Get the stop value from "next" or alternatively
# from the last non-empty index
# error: "Index" has no attribute "stop"
stop = (
non_empty_indexes[-1].stop # type: ignore[attr-defined]
if next_ is None
else next_
)
stop = non_empty_indexes[-1].stop if next_ is None else next_
return RangeIndex(start, stop, step).rename(name)

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