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 1 commit
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
84 changes: 48 additions & 36 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Sequence,
TypeVar,
Union,
cast,
)
import warnings

Expand Down Expand Up @@ -100,7 +101,7 @@
)

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


__all__ = ["Index"]
Expand Down Expand Up @@ -1608,6 +1609,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 @@ -3759,6 +3761,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 @@ -4804,7 +4808,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 @@ -5400,36 +5404,36 @@ def _add_comparison_methods(cls):
"""
Add in comparison methods.
"""
cls.__eq__ = _make_comparison_op(operator.eq, cls)
cls.__ne__ = _make_comparison_op(operator.ne, cls)
cls.__lt__ = _make_comparison_op(operator.lt, cls)
cls.__gt__ = _make_comparison_op(operator.gt, cls)
cls.__le__ = _make_comparison_op(operator.le, cls)
cls.__ge__ = _make_comparison_op(operator.ge, cls)
setattr(cls, "__eq__", _make_comparison_op(operator.eq, cls))
setattr(cls, "__ne__", _make_comparison_op(operator.ne, cls))
setattr(cls, "__lt__", _make_comparison_op(operator.lt, cls))
setattr(cls, "__gt__", _make_comparison_op(operator.gt, cls))
setattr(cls, "__le__", _make_comparison_op(operator.le, cls))
setattr(cls, "__ge__", _make_comparison_op(operator.ge, cls))

@classmethod
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 @@ -5446,10 +5450,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 Expand Up @@ -5561,20 +5565,28 @@ def logical_func(self, *args, **kwargs):
logical_func.__name__ = name
return logical_func

cls.all = _make_logical_function(
"all", "Return whether all elements are True.", np.all
setattr(
cls,
"all",
_make_logical_function(
"all", "Return whether all elements are True.", np.all
),
)
cls.any = _make_logical_function(
"any", "Return whether any element is True.", np.any
setattr(
cls,
"any",
_make_logical_function(
"any", "Return whether any element is True.", np.any
),
)

@classmethod
def _add_logical_methods_disabled(cls):
"""
Add in logical methods to disable.
"""
cls.all = make_invalid_op("all")
cls.any = make_invalid_op("any")
setattr(cls, "all", make_invalid_op("all"))
setattr(cls, "any", make_invalid_op("any"))

@property
def shape(self):
Expand Down
12 changes: 7 additions & 5 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from pandas._config import get_option

from pandas._typing import AnyArrayLike

from pandas.core.dtypes.inference import is_sequence

EscapeChars = Union[Mapping[str, str], Iterable[str]]
Expand Down Expand Up @@ -503,7 +505,7 @@ def _justify(


def format_object_attrs(
obj: Sequence, include_dtype: bool = True
obj: Union[Sequence, AnyArrayLike], include_dtype: bool = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnyArrayLike doesnt satisfy Sequence?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at the moment, xref #28770

Copy link
Member Author

@simonjayhawkins simonjayhawkins Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the issue here actually is that the docstring states iterable, but len is used in the function code and is the only requirement.

>>> from pandas.io.formats.printing import format_object_attrs
>>>
>>> format_object_attrs(pd.array([1, 2, 3] * 100, dtype="Int64"))
[('dtype', "'Int64'"), ('length', 300)]
>>>
>>> format_object_attrs(pd.Index([1, 2, 3] * 100))
[('dtype', "'int64'"), ('length', 300)]
>>>
>>> format_object_attrs("foo" * 100)
[('length', 300)]
>>>
>>> class MyClass:
...     def __len__(self):
...         return 300
...
>>> format_object_attrs(MyClass())
[('length', 300)]
>>>
>>> format_object_attrs(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\Anaconda3\envs\pandas-1.1.2\lib\site-packages\pandas\io\formats\printing.py", line 536, in format_object_attrs
    if len(obj) > max_seq_items:
TypeError: object of type 'int' has no len()
>>>

I'll type as Sized for now to be as permissive as possible to faciliate re-usability, but AFAICT, format_object_attrs is only ever called with an Index.

(so alternatively could just use Index as type for obj, and maybe reduce the hasattr usages or replace with isinstance(obj, MultiIndex))

) -> List[Tuple[str, Union[str, int]]]:
"""
Return a list of tuples of the (attr, formatted_value)
Expand All @@ -524,16 +526,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"
attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[attr-defined]
attrs.append(("dtype", f"'{obj.dtype}'")) # type: ignore[union-attr]
if getattr(obj, "name", None) is not None:
# error: "Sequence[Any]" has no attribute "name"
attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined]
attrs.append(("name", default_pprint(obj.name))) # type: ignore[union-attr]
# error: "Sequence[Any]" has no attribute "names"
elif getattr(obj, "names", None) is not None and any(
obj.names # type: ignore[attr-defined]
obj.names # type: ignore[union-attr]
):
# error: "Sequence[Any]" has no attribute "names"
attrs.append(("names", default_pprint(obj.names))) # type: ignore[attr-defined]
attrs.append(("names", default_pprint(obj.names))) # type: ignore[union-attr]
max_seq_items = get_option("display.max_seq_items") or len(obj)
if len(obj) > max_seq_items:
attrs.append(("length", len(obj)))
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ check_untyped_defs=False
[mypy-pandas.core.groupby.ops]
check_untyped_defs=False

[mypy-pandas.core.indexes.base]
check_untyped_defs=False

[mypy-pandas.core.indexes.category]
check_untyped_defs=False

Expand Down