Skip to content

TYP: @final for some Index methods #36232

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

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 23 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import numpy as np

from pandas._vendored.typing_extensions import final

from pandas._libs import algos as libalgos, index as libindex, lib
import pandas._libs.join as libjoin
from pandas._libs.lib import is_datetime_array, no_default
Expand Down Expand Up @@ -2476,6 +2478,7 @@ def __nonzero__(self):
# --------------------------------------------------------------------
# Set Operation Methods

@final
def _get_reconciled_name_object(self, other):
"""
If the result of a set operation will be self,
Expand All @@ -2487,6 +2490,7 @@ def _get_reconciled_name_object(self, other):
return self._shallow_copy(name=name)
return self

@final
def _union_incompatible_dtypes(self, other, sort):
"""
Casts this and other index to object dtype to allow the formation
Expand Down Expand Up @@ -2527,6 +2531,7 @@ def _is_compatible_with_other(self, other) -> bool:
"""
return type(self) is type(other) and is_dtype_equal(self.dtype, other.dtype)

@final
def _validate_sort_keyword(self, sort):
if sort not in [None, False]:
raise ValueError(
Expand Down Expand Up @@ -2661,6 +2666,7 @@ def _union(self, other, sort):
# for subclasses
return self._wrap_setop_result(other, result)

@final
def _wrap_setop_result(self, other, result):
name = get_op_result_name(self, other)
return self._shallow_copy(result, name=name)
Expand Down Expand Up @@ -3064,6 +3070,7 @@ def _convert_tolerance(self, tolerance, target):
raise ValueError("list-like tolerance size must match target index size")
return tolerance

@final
def _get_fill_indexer(
self, target: "Index", method: str_t, limit=None, tolerance=None
) -> np.ndarray:
Expand All @@ -3083,6 +3090,7 @@ def _get_fill_indexer(
indexer = self._filter_indexer_tolerance(target_values, indexer, tolerance)
return indexer

@final
def _get_fill_indexer_searchsorted(
self, target: "Index", method: str_t, limit=None
) -> np.ndarray:
Expand Down Expand Up @@ -3116,6 +3124,7 @@ def _get_fill_indexer_searchsorted(
indexer[indexer == len(self)] = -1
return indexer

@final
def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray:
"""
Get the indexer for the nearest index labels; requires an index with
Expand All @@ -3139,6 +3148,7 @@ def _get_nearest_indexer(self, target: "Index", limit, tolerance) -> np.ndarray:
indexer = self._filter_indexer_tolerance(target_values, indexer, tolerance)
return indexer

@final
def _filter_indexer_tolerance(
self,
target: Union["Index", np.ndarray, ExtensionArray],
Expand All @@ -3162,6 +3172,7 @@ def _get_partial_string_timestamp_match_key(self, key):
# GH#10331
return key

@final
def _validate_positional_slice(self, key: slice):
"""
For positional indexing, a slice must have either int or None
Expand Down Expand Up @@ -3310,6 +3321,7 @@ def _convert_list_indexer(self, keyarr):
"""
return None

@final
def _invalid_indexer(self, form: str_t, key):
"""
Consistent invalid indexer message.
Expand Down Expand Up @@ -3584,6 +3596,7 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
else:
return join_index

@final
def _join_multi(self, other, how, return_indexers=True):
from pandas.core.indexes.multi import MultiIndex
from pandas.core.reshape.merge import restore_dropped_levels_multijoin
Expand Down Expand Up @@ -3659,6 +3672,7 @@ def _join_multi(self, other, how, return_indexers=True):
return result[0], result[2], result[1]
return result

@final
def _join_non_unique(self, other, how="left", return_indexers=False):
from pandas.core.reshape.merge import _get_join_indexers

Expand Down Expand Up @@ -3686,6 +3700,7 @@ def _join_non_unique(self, other, how="left", return_indexers=False):
else:
return join_index

@final
def _join_level(
self, other, level, how="left", return_indexers=False, keep_order=True
):
Expand Down Expand Up @@ -3827,6 +3842,7 @@ def _get_leaf_sorter(labels):
else:
return join_index

@final
def _join_monotonic(self, other, how="left", return_indexers=False):
# We only get here with matching dtypes
assert other.dtype == self.dtype
Expand Down Expand Up @@ -4009,6 +4025,7 @@ def where(self, cond, other=None):

# construction helpers
@classmethod
@final
def _scalar_data_error(cls, data):
# We return the TypeError so that we can raise it from the constructor
# in order to keep mypy happy
Expand All @@ -4018,12 +4035,14 @@ def _scalar_data_error(cls, data):
)

@classmethod
@final
def _string_data_error(cls, data):
raise TypeError(
"String dtype not supported, you may need "
"to explicitly cast to a numeric type"
)

@final
def _coerce_scalar_to_index(self, item):
"""
We need to coerce a scalar to a compat for our index type.
Expand Down Expand Up @@ -4053,6 +4072,7 @@ def _convert_for_op(self, value):
"""
return value

@final
def _assert_can_do_op(self, value):
"""
Check value is valid for scalar op.
Expand Down Expand Up @@ -4164,6 +4184,7 @@ def __getitem__(self, key):
else:
return result

@final
def _can_hold_identifiers_and_holds_name(self, name) -> bool:
"""
Faster check for ``name in self`` when we know `name` is a Python
Expand Down Expand Up @@ -4777,6 +4798,7 @@ def get_indexer_for(self, target, **kwargs):
indexer, _ = self.get_indexer_non_unique(target, **kwargs)
return indexer

@final
def _maybe_promote(self, other: "Index"):
"""
When dealing with an object-dtype Index and a non-object Index, see
Expand Down Expand Up @@ -5051,6 +5073,7 @@ def _maybe_cast_indexer(self, key):
return com.cast_scalar_indexer(key)
return key

@final
def _validate_indexer(self, form: str_t, key, kind: str_t):
"""
If we are positional indexer, validate that we have appropriate
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class TestPDApi(Base):
"_testing",
"_tslib",
"_typing",
"_vendored",
"_version",
]

Expand Down