diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 539f5515a2f8b..df7c27c1fc48c 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -14,6 +14,7 @@ Tuple, TypeVar, Union, + cast, ) import warnings @@ -102,7 +103,7 @@ ) if TYPE_CHECKING: - from pandas import RangeIndex, Series + from pandas import MultiIndex, RangeIndex, Series __all__ = ["Index"] @@ -1575,6 +1576,7 @@ def droplevel(self, level=0): "levels: at least one level must be left." ) # The two checks above guarantee that here self is a MultiIndex + self = cast("MultiIndex", self) new_levels = list(self.levels) new_codes = list(self.codes) @@ -3735,6 +3737,8 @@ def _get_leaf_sorter(labels): left, right = right, left how = {"right": "left", "left": "right"}.get(how, how) + assert isinstance(left, MultiIndex) + level = left._get_level_number(level) old_level = left.levels[level] @@ -4780,7 +4784,7 @@ def get_indexer_for(self, target, **kwargs): """ if self._index_as_unique: return self.get_indexer(target, **kwargs) - indexer, _ = self.get_indexer_non_unique(target, **kwargs) + indexer, _ = self.get_indexer_non_unique(target) return indexer @property @@ -5409,24 +5413,24 @@ def _add_numeric_methods_binary(cls): """ Add in numeric methods. """ - cls.__add__ = _make_arithmetic_op(operator.add, cls) - cls.__radd__ = _make_arithmetic_op(ops.radd, cls) - cls.__sub__ = _make_arithmetic_op(operator.sub, cls) - cls.__rsub__ = _make_arithmetic_op(ops.rsub, cls) - cls.__rpow__ = _make_arithmetic_op(ops.rpow, cls) - cls.__pow__ = _make_arithmetic_op(operator.pow, cls) - - cls.__truediv__ = _make_arithmetic_op(operator.truediv, cls) - cls.__rtruediv__ = _make_arithmetic_op(ops.rtruediv, cls) - - cls.__mod__ = _make_arithmetic_op(operator.mod, cls) - cls.__rmod__ = _make_arithmetic_op(ops.rmod, cls) - cls.__floordiv__ = _make_arithmetic_op(operator.floordiv, cls) - cls.__rfloordiv__ = _make_arithmetic_op(ops.rfloordiv, cls) - cls.__divmod__ = _make_arithmetic_op(divmod, cls) - cls.__rdivmod__ = _make_arithmetic_op(ops.rdivmod, cls) - cls.__mul__ = _make_arithmetic_op(operator.mul, cls) - cls.__rmul__ = _make_arithmetic_op(ops.rmul, cls) + setattr(cls, "__add__", _make_arithmetic_op(operator.add, cls)) + setattr(cls, "__radd__", _make_arithmetic_op(ops.radd, cls)) + setattr(cls, "__sub__", _make_arithmetic_op(operator.sub, cls)) + setattr(cls, "__rsub__", _make_arithmetic_op(ops.rsub, cls)) + setattr(cls, "__rpow__", _make_arithmetic_op(ops.rpow, cls)) + setattr(cls, "__pow__", _make_arithmetic_op(operator.pow, cls)) + + setattr(cls, "__truediv__", _make_arithmetic_op(operator.truediv, cls)) + setattr(cls, "__rtruediv__", _make_arithmetic_op(ops.rtruediv, cls)) + + setattr(cls, "__mod__", _make_arithmetic_op(operator.mod, cls)) + setattr(cls, "__rmod__", _make_arithmetic_op(ops.rmod, cls)) + setattr(cls, "__floordiv__", _make_arithmetic_op(operator.floordiv, cls)) + setattr(cls, "__rfloordiv__", _make_arithmetic_op(ops.rfloordiv, cls)) + setattr(cls, "__divmod__", _make_arithmetic_op(divmod, cls)) + setattr(cls, "__rdivmod__", _make_arithmetic_op(ops.rdivmod, cls)) + setattr(cls, "__mul__", _make_arithmetic_op(operator.mul, cls)) + setattr(cls, "__rmul__", _make_arithmetic_op(ops.rmul, cls)) @classmethod def _add_numeric_methods_unary(cls): @@ -5443,10 +5447,10 @@ def _evaluate_numeric_unary(self): _evaluate_numeric_unary.__name__ = opstr return _evaluate_numeric_unary - cls.__neg__ = _make_evaluate_unary(operator.neg, "__neg__") - cls.__pos__ = _make_evaluate_unary(operator.pos, "__pos__") - cls.__abs__ = _make_evaluate_unary(np.abs, "__abs__") - cls.__inv__ = _make_evaluate_unary(lambda x: -x, "__inv__") + setattr(cls, "__neg__", _make_evaluate_unary(operator.neg, "__neg__")) + setattr(cls, "__pos__", _make_evaluate_unary(operator.pos, "__pos__")) + setattr(cls, "__abs__", _make_evaluate_unary(np.abs, "__abs__")) + setattr(cls, "__inv__", _make_evaluate_unary(lambda x: -x, "__inv__")) @classmethod def _add_numeric_methods(cls): diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 0d2ca83f1012e..72b07000146b2 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -12,6 +12,7 @@ Mapping, Optional, Sequence, + Sized, Tuple, TypeVar, Union, @@ -503,7 +504,7 @@ def _justify( def format_object_attrs( - obj: Sequence, include_dtype: bool = True + obj: Sized, include_dtype: bool = True ) -> List[Tuple[str, Union[str, int]]]: """ Return a list of tuples of the (attr, formatted_value) @@ -512,7 +513,7 @@ def format_object_attrs( Parameters ---------- obj : object - must be iterable + Must be sized. include_dtype : bool If False, dtype won't be in the returned list @@ -523,16 +524,16 @@ def format_object_attrs( """ attrs: List[Tuple[str, Union[str, int]]] = [] if hasattr(obj, "dtype") and include_dtype: - # error: "Sequence[Any]" has no attribute "dtype" + # error: "Sized" has no attribute "dtype" attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined] if getattr(obj, "name", None) is not None: - # error: "Sequence[Any]" has no attribute "name" + # error: "Sized" has no attribute "name" attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined] - # error: "Sequence[Any]" has no attribute "names" + # error: "Sized" has no attribute "names" elif getattr(obj, "names", None) is not None and any( obj.names # type: ignore[attr-defined] ): - # error: "Sequence[Any]" has no attribute "names" + # error: "Sized" has no attribute "names" attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined] max_seq_items = get_option("display.max_seq_items") or len(obj) if len(obj) > max_seq_items: diff --git a/setup.cfg b/setup.cfg index 8d3d79789a252..b5dce84031516 100644 --- a/setup.cfg +++ b/setup.cfg @@ -172,9 +172,6 @@ check_untyped_defs=False [mypy-pandas.core.groupby.grouper] check_untyped_defs=False -[mypy-pandas.core.indexes.base] -check_untyped_defs=False - [mypy-pandas.core.indexes.category] check_untyped_defs=False