Skip to content

TYP: check_untyped_defs core.indexes.base #36924

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 6 commits into from
Oct 10, 2020
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
52 changes: 28 additions & 24 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Tuple,
TypeVar,
Union,
cast,
)
import warnings

Expand Down Expand Up @@ -102,7 +103,7 @@
)

if TYPE_CHECKING:
from pandas import RangeIndex, Series
from pandas import MultiIndex, RangeIndex, Series


__all__ = ["Index"]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
13 changes: 7 additions & 6 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Mapping,
Optional,
Sequence,
Sized,
Tuple,
TypeVar,
Union,
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down