From 820da56aabce4598f5b3f70a493d0bab6b1b969e Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 6 Oct 2020 19:49:11 +0100 Subject: [PATCH 1/2] TYP: check_untyped_defs core.indexes.base --- pandas/core/indexes/base.py | 84 ++++++++++++++++++++--------------- pandas/io/formats/printing.py | 12 ++--- setup.cfg | 3 -- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d603797370ce3..f961fab078c1b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -13,6 +13,7 @@ Sequence, TypeVar, Union, + cast, ) import warnings @@ -100,7 +101,7 @@ ) if TYPE_CHECKING: - from pandas import RangeIndex, Series + from pandas import MultiIndex, RangeIndex, Series __all__ = ["Index"] @@ -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) @@ -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] @@ -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 @@ -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): @@ -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): @@ -5561,11 +5565,19 @@ 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 @@ -5573,8 +5585,8 @@ 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): diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 0d2ca83f1012e..ef97b71cc3898 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -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]] @@ -503,7 +505,7 @@ def _justify( def format_object_attrs( - obj: Sequence, include_dtype: bool = True + obj: Union[Sequence, AnyArrayLike], include_dtype: bool = True ) -> List[Tuple[str, Union[str, int]]]: """ Return a list of tuples of the (attr, formatted_value) @@ -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))) diff --git a/setup.cfg b/setup.cfg index e125eea226b10..8d2885fc7cb3a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 From 161815725a7ff2bfeb5aacce9e0dababfc7f555f Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Wed, 7 Oct 2020 11:34:36 +0100 Subject: [PATCH 2/2] Union[Sequence, AnyArrayLike] -> Sized --- pandas/io/formats/printing.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index ef97b71cc3898..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, @@ -19,8 +20,6 @@ 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]] @@ -505,7 +504,7 @@ def _justify( def format_object_attrs( - obj: Union[Sequence, AnyArrayLike], 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) @@ -514,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 @@ -525,17 +524,17 @@ 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[union-attr] + # 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" - attrs.append(("name", default_pprint(obj.name))) # type: ignore[union-attr] - # error: "Sequence[Any]" has no attribute "names" + # error: "Sized" has no attribute "name" + attrs.append(("name", default_pprint(obj.name))) # type: ignore[attr-defined] + # error: "Sized" has no attribute "names" elif getattr(obj, "names", None) is not None and any( - obj.names # type: ignore[union-attr] + obj.names # type: ignore[attr-defined] ): - # error: "Sequence[Any]" has no attribute "names" - attrs.append(("names", default_pprint(obj.names))) # type: ignore[union-attr] + # 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: attrs.append(("length", len(obj)))