Skip to content

Commit 2d6fbd7

Browse files
authored
CLN: dont return anything from _validate_indexer; annotations (#31321)
1 parent 5fa77ae commit 2d6fbd7

File tree

8 files changed

+39
-40
lines changed

8 files changed

+39
-40
lines changed

pandas/core/indexes/base.py

+20-22
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,8 @@ def _convert_scalar_indexer(self, key, kind=None):
31363136
assert kind in ["loc", "getitem", "iloc", None]
31373137

31383138
if kind == "iloc":
3139-
return self._validate_indexer("positional", key, kind)
3139+
self._validate_indexer("positional", key, "iloc")
3140+
return key
31403141

31413142
if len(self) and not isinstance(self, ABCMultiIndex):
31423143

@@ -3145,11 +3146,11 @@ def _convert_scalar_indexer(self, key, kind=None):
31453146
# or label indexing if we are using a type able
31463147
# to be represented in the index
31473148

3148-
if kind in ["getitem"] and is_float(key):
3149+
if kind == "getitem" and is_float(key):
31493150
if not self.is_floating():
31503151
self._invalid_indexer("label", key)
31513152

3152-
elif kind in ["loc"] and is_float(key):
3153+
elif kind == "loc" and is_float(key):
31533154

31543155
# we want to raise KeyError on string/mixed here
31553156
# technically we *could* raise a TypeError
@@ -3163,7 +3164,7 @@ def _convert_scalar_indexer(self, key, kind=None):
31633164
]:
31643165
self._invalid_indexer("label", key)
31653166

3166-
elif kind in ["loc"] and is_integer(key):
3167+
elif kind == "loc" and is_integer(key):
31673168
if not self.holds_integer():
31683169
self._invalid_indexer("label", key)
31693170

@@ -3189,11 +3190,10 @@ def _convert_slice_indexer(self, key: slice, kind=None):
31893190

31903191
# validate iloc
31913192
if kind == "iloc":
3192-
return slice(
3193-
self._validate_indexer("slice", key.start, kind),
3194-
self._validate_indexer("slice", key.stop, kind),
3195-
self._validate_indexer("slice", key.step, kind),
3196-
)
3193+
self._validate_indexer("slice", key.start, "iloc")
3194+
self._validate_indexer("slice", key.stop, "iloc")
3195+
self._validate_indexer("slice", key.step, "iloc")
3196+
return key
31973197

31983198
# potentially cast the bounds to integers
31993199
start, stop, step = key.start, key.stop, key.step
@@ -3214,11 +3214,10 @@ def is_int(v):
32143214
integers
32153215
"""
32163216
if self.is_integer() or is_index_slice:
3217-
return slice(
3218-
self._validate_indexer("slice", key.start, kind),
3219-
self._validate_indexer("slice", key.stop, kind),
3220-
self._validate_indexer("slice", key.step, kind),
3221-
)
3217+
self._validate_indexer("slice", key.start, "getitem")
3218+
self._validate_indexer("slice", key.stop, "getitem")
3219+
self._validate_indexer("slice", key.step, "getitem")
3220+
return key
32223221

32233222
# convert the slice to an indexer here
32243223

@@ -3348,7 +3347,7 @@ def _convert_list_indexer(self, keyarr, kind=None):
33483347

33493348
return None
33503349

3351-
def _invalid_indexer(self, form, key):
3350+
def _invalid_indexer(self, form: str_t, key):
33523351
"""
33533352
Consistent invalid indexer message.
33543353
"""
@@ -5006,20 +5005,19 @@ def _maybe_cast_indexer(self, key):
50065005
pass
50075006
return key
50085007

5009-
def _validate_indexer(self, form, key, kind: str_t):
5008+
def _validate_indexer(self, form: str_t, key, kind: str_t):
50105009
"""
50115010
If we are positional indexer, validate that we have appropriate
50125011
typed bounds must be an integer.
50135012
"""
5014-
assert kind in ["loc", "getitem", "iloc"]
5013+
assert kind in ["getitem", "iloc"]
50155014

50165015
if key is None:
50175016
pass
50185017
elif is_integer(key):
50195018
pass
5020-
elif kind in ["iloc", "getitem"]:
5019+
else:
50215020
self._invalid_indexer(form, key)
5022-
return key
50235021

50245022
_index_shared_docs[
50255023
"_maybe_cast_slice_bound"
@@ -5044,7 +5042,7 @@ def _validate_indexer(self, form, key, kind: str_t):
50445042
"""
50455043

50465044
@Appender(_index_shared_docs["_maybe_cast_slice_bound"])
5047-
def _maybe_cast_slice_bound(self, label, side, kind):
5045+
def _maybe_cast_slice_bound(self, label, side: str_t, kind):
50485046
assert kind in ["loc", "getitem", None]
50495047

50505048
# We are a plain index here (sub-class override this method if they
@@ -5075,7 +5073,7 @@ def _searchsorted_monotonic(self, label, side="left"):
50755073

50765074
raise ValueError("index must be monotonic increasing or decreasing")
50775075

5078-
def get_slice_bound(self, label, side, kind) -> int:
5076+
def get_slice_bound(self, label, side: str_t, kind) -> int:
50795077
"""
50805078
Calculate slice bound that corresponds to given label.
50815079
@@ -5260,7 +5258,7 @@ def insert(self, loc: int, item):
52605258
idx = np.concatenate((_self[:loc], item, _self[loc:]))
52615259
return self._shallow_copy_with_infer(idx)
52625260

5263-
def drop(self, labels, errors="raise"):
5261+
def drop(self, labels, errors: str_t = "raise"):
52645262
"""
52655263
Make new Index with passed list of labels deleted.
52665264

pandas/core/indexes/datetimes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ def snap(self, freq="S"):
470470
dta = DatetimeArray(snapped, dtype=self.dtype)
471471
return DatetimeIndex._simple_new(dta, name=self.name)
472472

473-
def _parsed_string_to_bounds(self, reso, parsed):
473+
def _parsed_string_to_bounds(self, reso: str, parsed: datetime):
474474
"""
475475
Calculate datetime bounds for parsed time string and its resolution.
476476
@@ -562,7 +562,7 @@ def _parsed_string_to_bounds(self, reso, parsed):
562562
return start, end
563563

564564
def _partial_date_slice(
565-
self, reso: str, parsed, use_lhs: bool = True, use_rhs: bool = True
565+
self, reso: str, parsed: datetime, use_lhs: bool = True, use_rhs: bool = True
566566
):
567567
"""
568568
Parameters
@@ -679,7 +679,7 @@ def get_loc(self, key, method=None, tolerance=None):
679679

680680
return Index.get_loc(self, key, method, tolerance)
681681

682-
def _maybe_cast_for_get_loc(self, key):
682+
def _maybe_cast_for_get_loc(self, key) -> Timestamp:
683683
# needed to localize naive datetimes
684684
key = Timestamp(key)
685685
if key.tzinfo is None:
@@ -688,7 +688,7 @@ def _maybe_cast_for_get_loc(self, key):
688688
key = key.tz_convert(self.tz)
689689
return key
690690

691-
def _maybe_cast_slice_bound(self, label, side, kind):
691+
def _maybe_cast_slice_bound(self, label, side: str, kind):
692692
"""
693693
If label is a string, cast it to datetime according to resolution.
694694

pandas/core/indexes/extension.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def wrapper(cls):
112112
return wrapper
113113

114114

115-
def _make_wrapped_comparison_op(opname):
115+
def _make_wrapped_comparison_op(opname: str):
116116
"""
117117
Create a comparison method that dispatches to ``._data``.
118118
"""
@@ -132,7 +132,7 @@ def wrapper(self, other):
132132
return wrapper
133133

134134

135-
def make_wrapped_arith_op(opname):
135+
def make_wrapped_arith_op(opname: str):
136136
def method(self, other):
137137
if (
138138
isinstance(other, Index)

pandas/core/indexes/interval.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class IntervalIndex(IntervalMixin, ExtensionIndex):
216216
# Immutable, so we are able to cache computations like isna in '_mask'
217217
_mask = None
218218

219+
_data: IntervalArray
219220
# --------------------------------------------------------------------
220221
# Constructors
221222

@@ -394,11 +395,11 @@ def __contains__(self, key: Any) -> bool:
394395
return False
395396

396397
@cache_readonly
397-
def _multiindex(self):
398+
def _multiindex(self) -> MultiIndex:
398399
return MultiIndex.from_arrays([self.left, self.right], names=["left", "right"])
399400

400401
@cache_readonly
401-
def values(self):
402+
def values(self) -> IntervalArray:
402403
"""
403404
Return the IntervalIndex's data as an IntervalArray.
404405
"""

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def _convert_scalar_indexer(self, key, kind=None):
393393
assert kind in ["loc", "getitem", "iloc", None]
394394

395395
if kind == "iloc":
396-
return self._validate_indexer("positional", key, kind)
396+
self._validate_indexer("positional", key, "iloc")
397397

398398
return key
399399

pandas/core/indexes/period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def __contains__(self, key: Any) -> bool:
371371
return False
372372

373373
@cache_readonly
374-
def _int64index(self):
374+
def _int64index(self) -> Int64Index:
375375
return Int64Index._simple_new(self.asi8, name=self.name)
376376

377377
# ------------------------------------------------------------------------
@@ -594,7 +594,7 @@ def get_loc(self, key, method=None, tolerance=None):
594594
except KeyError:
595595
raise KeyError(key)
596596

597-
def _maybe_cast_slice_bound(self, label, side, kind):
597+
def _maybe_cast_slice_bound(self, label, side: str, kind: str):
598598
"""
599599
If label is a string or a datetime, cast it to Period.ordinal according
600600
to resolution.
@@ -798,7 +798,7 @@ def _union(self, other, sort):
798798

799799
# ------------------------------------------------------------------------
800800

801-
def _apply_meta(self, rawarr):
801+
def _apply_meta(self, rawarr) -> "PeriodIndex":
802802
if not isinstance(rawarr, PeriodIndex):
803803
if not isinstance(rawarr, PeriodArray):
804804
rawarr = PeriodArray(rawarr, freq=self.freq)

pandas/core/indexes/range.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def copy(self, name=None, deep=False, dtype=None, **kwargs):
400400
name = self.name
401401
return self.from_range(self._range, name=name)
402402

403-
def _minmax(self, meth):
403+
def _minmax(self, meth: str):
404404
no_steps = len(self) - 1
405405
if no_steps == -1:
406406
return np.nan
@@ -409,13 +409,13 @@ def _minmax(self, meth):
409409

410410
return self.start + self.step * no_steps
411411

412-
def min(self, axis=None, skipna=True, *args, **kwargs):
412+
def min(self, axis=None, skipna=True, *args, **kwargs) -> int:
413413
"""The minimum value of the RangeIndex"""
414414
nv.validate_minmax_axis(axis)
415415
nv.validate_min(args, kwargs)
416416
return self._minmax("min")
417417

418-
def max(self, axis=None, skipna=True, *args, **kwargs):
418+
def max(self, axis=None, skipna=True, *args, **kwargs) -> int:
419419
"""The maximum value of the RangeIndex"""
420420
nv.validate_minmax_axis(axis)
421421
nv.validate_max(args, kwargs)
@@ -519,12 +519,12 @@ def intersection(self, other, sort=False):
519519
new_index = new_index.sort_values()
520520
return new_index
521521

522-
def _min_fitting_element(self, lower_limit):
522+
def _min_fitting_element(self, lower_limit: int) -> int:
523523
"""Returns the smallest element greater than or equal to the limit"""
524524
no_steps = -(-(lower_limit - self.start) // abs(self.step))
525525
return self.start + abs(self.step) * no_steps
526526

527-
def _max_fitting_element(self, upper_limit):
527+
def _max_fitting_element(self, upper_limit: int) -> int:
528528
"""Returns the largest element smaller than or equal to the limit"""
529529
no_steps = (upper_limit - self.start) // abs(self.step)
530530
return self.start + abs(self.step) * no_steps

pandas/core/indexes/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def get_loc(self, key, method=None, tolerance=None):
274274

275275
return Index.get_loc(self, key, method, tolerance)
276276

277-
def _maybe_cast_slice_bound(self, label, side, kind):
277+
def _maybe_cast_slice_bound(self, label, side: str, kind):
278278
"""
279279
If label is a string, cast it to timedelta according to resolution.
280280

0 commit comments

Comments
 (0)