Skip to content

Commit 2f88321

Browse files
authored
TYP: @Final for more Index methods (#39333)
1 parent 5d9e37d commit 2f88321

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

pandas/core/indexes/base.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
822822
)
823823
return self._shallow_copy(taken)
824824

825+
@final
825826
def _maybe_disallow_fill(self, allow_fill: bool, fill_value, indices) -> bool:
826827
"""
827828
We only use pandas-style take when allow_fill is True _and_
@@ -960,6 +961,7 @@ def __deepcopy__(self, memo=None):
960961
# --------------------------------------------------------------------
961962
# Rendering Methods
962963

964+
@final
963965
def __repr__(self) -> str_t:
964966
"""
965967
Return a string representation for this object.
@@ -1710,6 +1712,7 @@ def droplevel(self, level=0):
17101712

17111713
return self._drop_level_numbers(levnums)
17121714

1715+
@final
17131716
def _drop_level_numbers(self, levnums: List[int]):
17141717
"""
17151718
Drop MultiIndex levels by level _number_, not name.
@@ -1827,6 +1830,7 @@ def is_monotonic_decreasing(self) -> bool:
18271830
"""
18281831
return self._engine.is_monotonic_decreasing
18291832

1833+
@final
18301834
@property
18311835
def _is_strictly_monotonic_increasing(self) -> bool:
18321836
"""
@@ -1844,6 +1848,7 @@ def _is_strictly_monotonic_increasing(self) -> bool:
18441848
"""
18451849
return self.is_unique and self.is_monotonic_increasing
18461850

1851+
@final
18471852
@property
18481853
def _is_strictly_monotonic_decreasing(self) -> bool:
18491854
"""
@@ -1868,6 +1873,7 @@ def is_unique(self) -> bool:
18681873
"""
18691874
return self._engine.is_unique
18701875

1876+
@final
18711877
@property
18721878
def has_duplicates(self) -> bool:
18731879
"""
@@ -2239,6 +2245,7 @@ def _is_all_dates(self) -> bool:
22392245
return is_datetime_array(ensure_object(self._values))
22402246

22412247
@cache_readonly
2248+
@final
22422249
def is_all_dates(self):
22432250
"""
22442251
Whether or not the index values only consist of dates.
@@ -3269,6 +3276,7 @@ def get_loc(self, key, method=None, tolerance=None):
32693276
"""
32703277

32713278
@Appender(_index_shared_docs["get_indexer"] % _index_doc_kwargs)
3279+
@final
32723280
def get_indexer(
32733281
self, target, method=None, limit=None, tolerance=None
32743282
) -> np.ndarray:
@@ -3328,6 +3336,7 @@ def _get_indexer(
33283336

33293337
return ensure_platform_int(indexer)
33303338

3339+
@final
33313340
def _check_indexing_method(self, method):
33323341
"""
33333342
Raise if we have a get_indexer `method` that is not supported or valid.
@@ -4542,7 +4551,7 @@ def putmask(self, mask, value):
45424551
np.putmask(values, mask, converted)
45434552
return self._shallow_copy(values)
45444553

4545-
def equals(self, other: object) -> bool:
4554+
def equals(self, other: Any) -> bool:
45464555
"""
45474556
Determine if two Index object are equal.
45484557
@@ -5104,6 +5113,7 @@ def get_indexer_for(self, target, **kwargs):
51045113
indexer, _ = self.get_indexer_non_unique(target)
51055114
return indexer
51065115

5116+
@final
51075117
def _get_indexer_non_comparable(self, target: Index, method, unique: bool = True):
51085118
"""
51095119
Called from get_indexer or get_indexer_non_unique when the target
@@ -5142,7 +5152,7 @@ def _get_indexer_non_comparable(self, target: Index, method, unique: bool = True
51425152
return no_matches, missing
51435153

51445154
@property
5145-
def _index_as_unique(self):
5155+
def _index_as_unique(self) -> bool:
51465156
"""
51475157
Whether we should treat this as unique for the sake of
51485158
get_indexer vs get_indexer_non_unique.
@@ -5178,6 +5188,7 @@ def _maybe_promote(self, other: Index):
51785188

51795189
return self, other
51805190

5191+
@final
51815192
def _should_compare(self, other: Index) -> bool:
51825193
"""
51835194
Check if `self == other` can ever have non-False entries.
@@ -5786,10 +5797,6 @@ def _cmp_method(self, other, op):
57865797
with np.errstate(all="ignore"):
57875798
result = ops.comp_method_OBJECT_ARRAY(op, self._values, other)
57885799

5789-
elif is_interval_dtype(self.dtype):
5790-
with np.errstate(all="ignore"):
5791-
result = op(self._values, np.asarray(other))
5792-
57935800
else:
57945801
with np.errstate(all="ignore"):
57955802
result = ops.comparison_op(self._values, other, op)
@@ -5808,6 +5815,7 @@ def _arith_method(self, other, op):
58085815
return (Index(result[0]), Index(result[1]))
58095816
return Index(result)
58105817

5818+
@final
58115819
def _unary_method(self, op):
58125820
result = op(self._values)
58135821
return Index(result, name=self.name)

pandas/core/indexes/datetimelike.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __array_wrap__(self, result, context=None):
162162

163163
# ------------------------------------------------------------------------
164164

165-
def equals(self, other: object) -> bool:
165+
def equals(self, other: Any) -> bool:
166166
"""
167167
Determines if two Index objects contain the same elements.
168168
"""
@@ -508,7 +508,7 @@ def _partial_date_slice(
508508
__truediv__ = make_wrapped_arith_op("__truediv__")
509509
__rtruediv__ = make_wrapped_arith_op("__rtruediv__")
510510

511-
def shift(self, periods=1, freq=None):
511+
def shift(self: _T, periods: int = 1, freq=None) -> _T:
512512
"""
513513
Shift index by desired number of time frequency increments.
514514
@@ -567,7 +567,7 @@ def _get_delete_freq(self, loc: int):
567567
freq = self.freq
568568
return freq
569569

570-
def _get_insert_freq(self, loc, item):
570+
def _get_insert_freq(self, loc: int, item):
571571
"""
572572
Find the `freq` for self.insert(loc, item).
573573
"""
@@ -593,7 +593,7 @@ def _get_insert_freq(self, loc, item):
593593
return freq
594594

595595
@doc(NDArrayBackedExtensionIndex.delete)
596-
def delete(self, loc):
596+
def delete(self: _T, loc) -> _T:
597597
result = super().delete(loc)
598598
result._data._freq = self._get_delete_freq(loc)
599599
return result
@@ -710,6 +710,7 @@ def _intersection(self, other: Index, sort=False) -> Index:
710710
return self._wrap_setop_result(other, result)
711711

712712
def _can_fast_intersect(self: _T, other: _T) -> bool:
713+
# Note: we only get here with len(self) > 0 and len(other) > 0
713714
if self.freq is None:
714715
return False
715716

@@ -725,9 +726,6 @@ def _can_fast_intersect(self: _T, other: _T) -> bool:
725726
# so intersection will preserve freq
726727
return True
727728

728-
elif not len(self) or not len(other):
729-
return False
730-
731729
elif isinstance(self.freq, Tick):
732730
# We "line up" if and only if the difference between two of our points
733731
# is a multiple of our freq
@@ -741,9 +739,6 @@ def _can_fast_union(self: _T, other: _T) -> bool:
741739
# Assumes that type(self) == type(other), as per the annotation
742740
# The ability to fast_union also implies that `freq` should be
743741
# retained on union.
744-
if not isinstance(other, type(self)):
745-
return False
746-
747742
freq = self.freq
748743

749744
if freq is None or freq != other.freq:
@@ -769,7 +764,7 @@ def _can_fast_union(self: _T, other: _T) -> bool:
769764
# Only need to "adjoin", not overlap
770765
return (right_start == left_end + freq) or right_start in left
771766

772-
def _fast_union(self, other, sort=None):
767+
def _fast_union(self: _T, other: _T, sort=None) -> _T:
773768
if len(other) == 0:
774769
return self.view(type(self))
775770

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def _get_indexer_pointwise(self, target: Index) -> Tuple[np.ndarray, np.ndarray]
729729
return ensure_platform_int(indexer), ensure_platform_int(missing)
730730

731731
@property
732-
def _index_as_unique(self):
732+
def _index_as_unique(self) -> bool:
733733
return not self.is_overlapping
734734

735735
_requires_unique_msg = (

pandas/core/indexes/range.py

-4
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,6 @@ def is_monotonic_increasing(self) -> bool:
337337
def is_monotonic_decreasing(self) -> bool:
338338
return self._range.step < 0 or len(self) <= 1
339339

340-
@property
341-
def has_duplicates(self) -> bool:
342-
return False
343-
344340
def __contains__(self, key: Any) -> bool:
345341
hash(key)
346342
try:

0 commit comments

Comments
 (0)