Skip to content

Commit f572faf

Browse files
TYP: check_untyped_defs core.indexes.base (pandas-dev#36924)
1 parent 3a043f2 commit f572faf

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

pandas/core/indexes/base.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Tuple,
1515
TypeVar,
1616
Union,
17+
cast,
1718
)
1819
import warnings
1920

@@ -102,7 +103,7 @@
102103
)
103104

104105
if TYPE_CHECKING:
105-
from pandas import RangeIndex, Series
106+
from pandas import MultiIndex, RangeIndex, Series
106107

107108

108109
__all__ = ["Index"]
@@ -1575,6 +1576,7 @@ def droplevel(self, level=0):
15751576
"levels: at least one level must be left."
15761577
)
15771578
# The two checks above guarantee that here self is a MultiIndex
1579+
self = cast("MultiIndex", self)
15781580

15791581
new_levels = list(self.levels)
15801582
new_codes = list(self.codes)
@@ -3735,6 +3737,8 @@ def _get_leaf_sorter(labels):
37353737
left, right = right, left
37363738
how = {"right": "left", "left": "right"}.get(how, how)
37373739

3740+
assert isinstance(left, MultiIndex)
3741+
37383742
level = left._get_level_number(level)
37393743
old_level = left.levels[level]
37403744

@@ -4780,7 +4784,7 @@ def get_indexer_for(self, target, **kwargs):
47804784
"""
47814785
if self._index_as_unique:
47824786
return self.get_indexer(target, **kwargs)
4783-
indexer, _ = self.get_indexer_non_unique(target, **kwargs)
4787+
indexer, _ = self.get_indexer_non_unique(target)
47844788
return indexer
47854789

47864790
@property
@@ -5409,24 +5413,24 @@ def _add_numeric_methods_binary(cls):
54095413
"""
54105414
Add in numeric methods.
54115415
"""
5412-
cls.__add__ = _make_arithmetic_op(operator.add, cls)
5413-
cls.__radd__ = _make_arithmetic_op(ops.radd, cls)
5414-
cls.__sub__ = _make_arithmetic_op(operator.sub, cls)
5415-
cls.__rsub__ = _make_arithmetic_op(ops.rsub, cls)
5416-
cls.__rpow__ = _make_arithmetic_op(ops.rpow, cls)
5417-
cls.__pow__ = _make_arithmetic_op(operator.pow, cls)
5418-
5419-
cls.__truediv__ = _make_arithmetic_op(operator.truediv, cls)
5420-
cls.__rtruediv__ = _make_arithmetic_op(ops.rtruediv, cls)
5421-
5422-
cls.__mod__ = _make_arithmetic_op(operator.mod, cls)
5423-
cls.__rmod__ = _make_arithmetic_op(ops.rmod, cls)
5424-
cls.__floordiv__ = _make_arithmetic_op(operator.floordiv, cls)
5425-
cls.__rfloordiv__ = _make_arithmetic_op(ops.rfloordiv, cls)
5426-
cls.__divmod__ = _make_arithmetic_op(divmod, cls)
5427-
cls.__rdivmod__ = _make_arithmetic_op(ops.rdivmod, cls)
5428-
cls.__mul__ = _make_arithmetic_op(operator.mul, cls)
5429-
cls.__rmul__ = _make_arithmetic_op(ops.rmul, cls)
5416+
setattr(cls, "__add__", _make_arithmetic_op(operator.add, cls))
5417+
setattr(cls, "__radd__", _make_arithmetic_op(ops.radd, cls))
5418+
setattr(cls, "__sub__", _make_arithmetic_op(operator.sub, cls))
5419+
setattr(cls, "__rsub__", _make_arithmetic_op(ops.rsub, cls))
5420+
setattr(cls, "__rpow__", _make_arithmetic_op(ops.rpow, cls))
5421+
setattr(cls, "__pow__", _make_arithmetic_op(operator.pow, cls))
5422+
5423+
setattr(cls, "__truediv__", _make_arithmetic_op(operator.truediv, cls))
5424+
setattr(cls, "__rtruediv__", _make_arithmetic_op(ops.rtruediv, cls))
5425+
5426+
setattr(cls, "__mod__", _make_arithmetic_op(operator.mod, cls))
5427+
setattr(cls, "__rmod__", _make_arithmetic_op(ops.rmod, cls))
5428+
setattr(cls, "__floordiv__", _make_arithmetic_op(operator.floordiv, cls))
5429+
setattr(cls, "__rfloordiv__", _make_arithmetic_op(ops.rfloordiv, cls))
5430+
setattr(cls, "__divmod__", _make_arithmetic_op(divmod, cls))
5431+
setattr(cls, "__rdivmod__", _make_arithmetic_op(ops.rdivmod, cls))
5432+
setattr(cls, "__mul__", _make_arithmetic_op(operator.mul, cls))
5433+
setattr(cls, "__rmul__", _make_arithmetic_op(ops.rmul, cls))
54305434

54315435
@classmethod
54325436
def _add_numeric_methods_unary(cls):
@@ -5443,10 +5447,10 @@ def _evaluate_numeric_unary(self):
54435447
_evaluate_numeric_unary.__name__ = opstr
54445448
return _evaluate_numeric_unary
54455449

5446-
cls.__neg__ = _make_evaluate_unary(operator.neg, "__neg__")
5447-
cls.__pos__ = _make_evaluate_unary(operator.pos, "__pos__")
5448-
cls.__abs__ = _make_evaluate_unary(np.abs, "__abs__")
5449-
cls.__inv__ = _make_evaluate_unary(lambda x: -x, "__inv__")
5450+
setattr(cls, "__neg__", _make_evaluate_unary(operator.neg, "__neg__"))
5451+
setattr(cls, "__pos__", _make_evaluate_unary(operator.pos, "__pos__"))
5452+
setattr(cls, "__abs__", _make_evaluate_unary(np.abs, "__abs__"))
5453+
setattr(cls, "__inv__", _make_evaluate_unary(lambda x: -x, "__inv__"))
54505454

54515455
@classmethod
54525456
def _add_numeric_methods(cls):

pandas/io/formats/printing.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Mapping,
1313
Optional,
1414
Sequence,
15+
Sized,
1516
Tuple,
1617
TypeVar,
1718
Union,
@@ -503,7 +504,7 @@ def _justify(
503504

504505

505506
def format_object_attrs(
506-
obj: Sequence, include_dtype: bool = True
507+
obj: Sized, include_dtype: bool = True
507508
) -> List[Tuple[str, Union[str, int]]]:
508509
"""
509510
Return a list of tuples of the (attr, formatted_value)
@@ -512,7 +513,7 @@ def format_object_attrs(
512513
Parameters
513514
----------
514515
obj : object
515-
must be iterable
516+
Must be sized.
516517
include_dtype : bool
517518
If False, dtype won't be in the returned list
518519
@@ -523,16 +524,16 @@ def format_object_attrs(
523524
"""
524525
attrs: List[Tuple[str, Union[str, int]]] = []
525526
if hasattr(obj, "dtype") and include_dtype:
526-
# error: "Sequence[Any]" has no attribute "dtype"
527+
# error: "Sized" has no attribute "dtype"
527528
attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined]
528529
if getattr(obj, "name", None) is not None:
529-
# error: "Sequence[Any]" has no attribute "name"
530+
# error: "Sized" has no attribute "name"
530531
attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined]
531-
# error: "Sequence[Any]" has no attribute "names"
532+
# error: "Sized" has no attribute "names"
532533
elif getattr(obj, "names", None) is not None and any(
533534
obj.names # type: ignore[attr-defined]
534535
):
535-
# error: "Sequence[Any]" has no attribute "names"
536+
# error: "Sized" has no attribute "names"
536537
attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined]
537538
max_seq_items = get_option("display.max_seq_items") or len(obj)
538539
if len(obj) > max_seq_items:

setup.cfg

-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ check_untyped_defs=False
172172
[mypy-pandas.core.groupby.grouper]
173173
check_untyped_defs=False
174174

175-
[mypy-pandas.core.indexes.base]
176-
check_untyped_defs=False
177-
178175
[mypy-pandas.core.indexes.category]
179176
check_untyped_defs=False
180177

0 commit comments

Comments
 (0)