From 4b9cc76b07871da9c7153fcfebe47826339810fe Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sat, 23 Nov 2019 21:44:44 +0200 Subject: [PATCH 1/4] Added annotation to __eq__ --- pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/core/dtypes/dtypes.py | 6 +++--- pandas/core/indexes/frozen.py | 2 +- pandas/io/pytables.py | 2 +- pandas/io/stata.py | 2 +- pandas/tests/indexing/test_indexing.py | 2 +- pandas/tests/scalar/timestamp/test_comparisons.py | 2 +- pandas/tests/series/test_ufunc.py | 2 +- pandas/tests/test_algos.py | 2 +- pandas/tseries/offsets.py | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 68a0a4a403c81..327d1067dd17d 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -328,7 +328,7 @@ class _BaseOffset: def __setattr__(self, name, value): raise AttributeError("DateOffset objects are immutable.") - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, str): try: # GH#23524 if to_offset fails, we are dealing with an diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 4de958cc386b9..3b656705f5568 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -90,7 +90,7 @@ def __hash__(self): # __eq__, so we explicitly do it here. return super().__hash__() - def __eq__(self, other): + def __eq__(self, other) -> bool: # We have to override __eq__ to handle NA values in _metadata. # The base class does simple == checks, which fail for NA. if isinstance(other, str): diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 523c8e8bd02d0..2c601b01dbae5 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -765,7 +765,7 @@ def __hash__(self) -> int: # TODO: update this. return hash(str(self)) - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, str): return other == self.name @@ -904,7 +904,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, str): return other == self.name or other == self.name.title() @@ -1077,7 +1077,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, str): return other.lower() in (self.name.lower(), str(self).lower()) elif not isinstance(other, IntervalDtype): diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index 2c9521d23f71a..13c386187a9e5 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -77,7 +77,7 @@ def __radd__(self, other): other = list(other) return self.__class__(other + list(self)) - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, (tuple, FrozenList)): other = list(other) return super().__eq__(other) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8afbd293a095b..99cfb22b9921f 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2109,7 +2109,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other): + def __eq__(self, other) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 567eeb7f5cdc8..bd5e215730397 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -859,7 +859,7 @@ def __repr__(self) -> str: # not perfect :-/ return "{cls}({obj})".format(cls=self.__class__, obj=self) - def __eq__(self, other): + def __eq__(self, other) -> bool: return ( isinstance(other, self.__class__) and self.string == other.string diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index fc5753ec2955c..8e52d2cca7070 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -595,7 +595,7 @@ def __str__(self) -> str: __repr__ = __str__ - def __eq__(self, other): + def __eq__(self, other) -> bool: return self.value == other.value def view(self): diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index 4ff0f84327854..da0818c5dc596 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -179,7 +179,7 @@ def __gt__(self, o): def __ge__(self, o): return True - def __eq__(self, o): + def __eq__(self, o) -> bool: return isinstance(o, Inf) inf = Inf() diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index c8a127f89bf91..977e7ded1f1a7 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -282,7 +282,7 @@ def __add__(self, other): other = getattr(other, "value", other) return type(self)(self.value + other) - def __eq__(self, other): + def __eq__(self, other) -> bool: return type(other) is Thing and self.value == other.value def __repr__(self) -> str: diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 9e89a1b6f0467..02b50d84c6eca 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -767,7 +767,7 @@ def test_same_object_is_in(self): # with similar behavior, then we at least should # fall back to usual python's behavior: "a in [a] == True" class LikeNan: - def __eq__(self, other): + def __eq__(self, other) -> bool: return False def __hash__(self): diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index e516d30d5490f..0620f2b9aae49 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -2577,7 +2577,7 @@ def __add__(self, other): "will overflow".format(self=self, other=other) ) - def __eq__(self, other): + def __eq__(self, other) -> bool: if isinstance(other, str): from pandas.tseries.frequencies import to_offset From 489f757d89961dd48072d186ebbf9c845cb784dc Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 26 Nov 2019 01:13:50 +0200 Subject: [PATCH 2/4] Added 'object' annotation to 'other' --- pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/core/dtypes/base.py | 2 +- pandas/core/dtypes/dtypes.py | 8 ++++---- pandas/core/indexes/frozen.py | 2 +- pandas/io/pytables.py | 4 ++-- pandas/io/stata.py | 2 +- pandas/tests/groupby/test_function.py | 2 +- pandas/tests/indexing/test_indexing.py | 2 +- pandas/tests/scalar/timedelta/test_timedelta.py | 2 +- pandas/tests/scalar/timestamp/test_comparisons.py | 2 +- pandas/tests/series/test_ufunc.py | 2 +- pandas/tests/test_algos.py | 2 +- pandas/tseries/offsets.py | 2 +- 14 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 327d1067dd17d..9ea0f94c86e41 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -328,7 +328,7 @@ class _BaseOffset: def __setattr__(self, name, value): raise AttributeError("DateOffset objects are immutable.") - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): try: # GH#23524 if to_offset fails, we are dealing with an diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 3b656705f5568..994f210d0f017 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -90,7 +90,7 @@ def __hash__(self): # __eq__, so we explicitly do it here. return super().__hash__() - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: # We have to override __eq__ to handle NA values in _metadata. # The base class does simple == checks, which fail for NA. if isinstance(other, str): diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 8acdf32c8768e..157f990f08243 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -86,7 +86,7 @@ def __from_arrow__( def __str__(self) -> str: return self.name - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: """ Check whether 'other' is equal to self. diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 2c601b01dbae5..d758ce7267d06 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -370,7 +370,7 @@ def __hash__(self) -> int: # We *do* want to include the real self.ordered here return int(self._hash_categories(self.categories, self._ordered)) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other: object) -> bool: """ Rules for CDT equality: 1) Any CDT is equal to the string 'category' @@ -765,7 +765,7 @@ def __hash__(self) -> int: # TODO: update this. return hash(str(self)) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): return other == self.name @@ -904,7 +904,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): return other == self.name or other == self.name.title() @@ -1077,7 +1077,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): return other.lower() in (self.name.lower(), str(self).lower()) elif not isinstance(other, IntervalDtype): diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index 13c386187a9e5..e4e68f7fdfb72 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -77,7 +77,7 @@ def __radd__(self, other): other = list(other) return self.__class__(other + list(self)) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, (tuple, FrozenList)): other = list(other) return super().__eq__(other) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 99cfb22b9921f..e2a9ca299d799 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1773,7 +1773,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) @@ -2109,7 +2109,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index bd5e215730397..42c4f76c749d4 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -859,7 +859,7 @@ def __repr__(self) -> str: # not perfect :-/ return "{cls}({obj})".format(cls=self.__class__, obj=self) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: return ( isinstance(other, self.__class__) and self.string == other.string diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index c41f762e9128d..550954564ac0c 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -1230,7 +1230,7 @@ def __init__(self, msg="I will raise inside Cython"): super().__init__() self.msg = msg - def __eq__(self, other): + def __eq__(self, other: object): # gets called in Cython to check that raising calls the method raise RaisingObjectException(self.msg) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 8e52d2cca7070..25985c0004ef9 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -595,7 +595,7 @@ def __str__(self) -> str: __repr__ = __str__ - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: return self.value == other.value def view(self): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 9bb6c991a930a..db8a97c8adbb2 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -139,7 +139,7 @@ def generic_result(self): else: return self.cmp_result - def __eq__(self, other): + def __eq__(self, other: object): return self.generic_result() def __gt__(self, other): diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index da0818c5dc596..ea41e8ed76469 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -179,7 +179,7 @@ def __gt__(self, o): def __ge__(self, o): return True - def __eq__(self, o) -> bool: + def __eq__(self, o: object) -> bool: return isinstance(o, Inf) inf = Inf() diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index 977e7ded1f1a7..fe5e044f3b92e 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -282,7 +282,7 @@ def __add__(self, other): other = getattr(other, "value", other) return type(self)(self.value + other) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: return type(other) is Thing and self.value == other.value def __repr__(self) -> str: diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 02b50d84c6eca..62111be6a6d21 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -767,7 +767,7 @@ def test_same_object_is_in(self): # with similar behavior, then we at least should # fall back to usual python's behavior: "a in [a] == True" class LikeNan: - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: return False def __hash__(self): diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 0620f2b9aae49..28a5584b00cc3 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -2577,7 +2577,7 @@ def __add__(self, other): "will overflow".format(self=self, other=other) ) - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: if isinstance(other, str): from pandas.tseries.frequencies import to_offset From e69277ed2b7b4a10eb6eb0c9210faa6526272f34 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 26 Nov 2019 02:06:37 +0200 Subject: [PATCH 3/4] Added 'Any' type annotation to 'other' in __eq__ --- pandas/_libs/tslibs/offsets.pyx | 3 ++- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/core/dtypes/base.py | 4 ++-- pandas/core/dtypes/dtypes.py | 8 ++++---- pandas/core/indexes/frozen.py | 3 ++- pandas/io/pytables.py | 4 ++-- pandas/io/stata.py | 3 ++- pandas/tests/groupby/test_function.py | 3 ++- pandas/tests/indexing/test_indexing.py | 3 ++- pandas/tests/scalar/timedelta/test_timedelta.py | 3 ++- pandas/tests/scalar/timestamp/test_comparisons.py | 5 +++-- pandas/tests/series/test_ufunc.py | 3 ++- pandas/tests/test_algos.py | 3 ++- pandas/tseries/offsets.py | 4 ++-- 14 files changed, 30 insertions(+), 21 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 9ea0f94c86e41..0bf676b6cff63 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -1,6 +1,7 @@ import cython import time +from typing import Any from cpython.datetime cimport (PyDateTime_IMPORT, PyDateTime_Check, PyDelta_Check, @@ -328,7 +329,7 @@ class _BaseOffset: def __setattr__(self, name, value): raise AttributeError("DateOffset objects are immutable.") - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, str): try: # GH#23524 if to_offset fails, we are dealing with an diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 994f210d0f017..b53830a1eaeaa 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -90,7 +90,7 @@ def __hash__(self): # __eq__, so we explicitly do it here. return super().__hash__() - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: # We have to override __eq__ to handle NA values in _metadata. # The base class does simple == checks, which fail for NA. if isinstance(other, str): diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 157f990f08243..063014cbe970d 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -1,5 +1,5 @@ """Extend pandas with custom array types""" -from typing import List, Optional, Tuple, Type +from typing import Any, List, Optional, Tuple, Type import numpy as np @@ -86,7 +86,7 @@ def __from_arrow__( def __str__(self) -> str: return self.name - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: """ Check whether 'other' is equal to self. diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index d758ce7267d06..63e71183e51ef 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -370,7 +370,7 @@ def __hash__(self) -> int: # We *do* want to include the real self.ordered here return int(self._hash_categories(self.categories, self._ordered)) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: """ Rules for CDT equality: 1) Any CDT is equal to the string 'category' @@ -765,7 +765,7 @@ def __hash__(self) -> int: # TODO: update this. return hash(str(self)) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, str): return other == self.name @@ -904,7 +904,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, str): return other == self.name or other == self.name.title() @@ -1077,7 +1077,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, str): return other.lower() in (self.name.lower(), str(self).lower()) elif not isinstance(other, IntervalDtype): diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index e4e68f7fdfb72..e34af2ca2927c 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -7,6 +7,7 @@ - .levels & .codes (FrozenNDArray) """ +from typing import Any import warnings import numpy as np @@ -77,7 +78,7 @@ def __radd__(self, other): other = list(other) return self.__class__(other + list(self)) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, (tuple, FrozenList)): other = list(other) return super().__eq__(other) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index e2a9ca299d799..3fb5bb190b0b8 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1773,7 +1773,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) @@ -2109,7 +2109,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 42c4f76c749d4..70df87fe0c63e 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -16,6 +16,7 @@ import os import struct import sys +from typing import Any import warnings from dateutil.relativedelta import relativedelta @@ -859,7 +860,7 @@ def __repr__(self) -> str: # not perfect :-/ return "{cls}({obj})".format(cls=self.__class__, obj=self) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: return ( isinstance(other, self.__class__) and self.string == other.string diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index 550954564ac0c..a6643f239e83e 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -3,6 +3,7 @@ from io import StringIO from itertools import product from string import ascii_lowercase +from typing import Any import numpy as np import pytest @@ -1230,7 +1231,7 @@ def __init__(self, msg="I will raise inside Cython"): super().__init__() self.msg = msg - def __eq__(self, other: object): + def __eq__(self, other: Any): # gets called in Cython to check that raising calls the method raise RaisingObjectException(self.msg) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index 25985c0004ef9..09e19d7abc10d 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -2,6 +2,7 @@ from datetime import datetime import re +from typing import Any import weakref import numpy as np @@ -595,7 +596,7 @@ def __str__(self) -> str: __repr__ = __str__ - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: return self.value == other.value def view(self): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index db8a97c8adbb2..82b65fe114dc2 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -1,6 +1,7 @@ """ test the scalar Timedelta """ from datetime import timedelta import re +from typing import Any import numpy as np import pytest @@ -139,7 +140,7 @@ def generic_result(self): else: return self.cmp_result - def __eq__(self, other: object): + def __eq__(self, other: Any): return self.generic_result() def __gt__(self, other): diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index ea41e8ed76469..7de9d2636c6e3 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -1,5 +1,6 @@ from datetime import datetime import operator +from typing import Any import numpy as np import pytest @@ -179,8 +180,8 @@ def __gt__(self, o): def __ge__(self, o): return True - def __eq__(self, o: object) -> bool: - return isinstance(o, Inf) + def __eq__(self, other: Any) -> bool: + return isinstance(other, Inf) inf = Inf() timestamp = Timestamp("2018-11-30") diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index fe5e044f3b92e..82447a9d8cd01 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -1,5 +1,6 @@ from collections import deque import string +from typing import Any import numpy as np import pytest @@ -282,7 +283,7 @@ def __add__(self, other): other = getattr(other, "value", other) return type(self)(self.value + other) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: return type(other) is Thing and self.value == other.value def __repr__(self) -> str: diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 62111be6a6d21..570fa9f3a5c82 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1,6 +1,7 @@ from datetime import datetime from itertools import permutations import struct +from typing import Any import numpy as np from numpy.random import RandomState @@ -767,7 +768,7 @@ def test_same_object_is_in(self): # with similar behavior, then we at least should # fall back to usual python's behavior: "a in [a] == True" class LikeNan: - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: return False def __hash__(self): diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 28a5584b00cc3..b1af0dd0c7d50 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -1,7 +1,7 @@ from datetime import date, datetime, timedelta import functools import operator -from typing import Optional +from typing import Any, Optional from dateutil.easter import easter import numpy as np @@ -2577,7 +2577,7 @@ def __add__(self, other): "will overflow".format(self=self, other=other) ) - def __eq__(self, other: object) -> bool: + def __eq__(self, other: Any) -> bool: if isinstance(other, str): from pandas.tseries.frequencies import to_offset From 0bf439b45fce5c4203b735a9550c95f31d359480 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 27 Nov 2019 15:05:27 +0200 Subject: [PATCH 4/4] Annotations to __eq__ function --- pandas/_libs/tslibs/offsets.pyx | 3 +-- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/core/dtypes/base.py | 4 ++-- pandas/core/dtypes/dtypes.py | 6 +++--- pandas/core/indexes/frozen.py | 3 +-- pandas/io/pytables.py | 4 ++-- pandas/io/stata.py | 3 +-- pandas/tests/groupby/test_function.py | 3 +-- pandas/tests/indexing/test_indexing.py | 3 +-- pandas/tests/scalar/timedelta/test_timedelta.py | 3 +-- pandas/tests/scalar/timestamp/test_comparisons.py | 3 +-- pandas/tests/series/test_ufunc.py | 3 +-- pandas/tests/test_algos.py | 3 +-- pandas/tseries/offsets.py | 4 ++-- 14 files changed, 19 insertions(+), 28 deletions(-) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 0bf676b6cff63..327d1067dd17d 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -1,7 +1,6 @@ import cython import time -from typing import Any from cpython.datetime cimport (PyDateTime_IMPORT, PyDateTime_Check, PyDelta_Check, @@ -329,7 +328,7 @@ class _BaseOffset: def __setattr__(self, name, value): raise AttributeError("DateOffset objects are immutable.") - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, str): try: # GH#23524 if to_offset fails, we are dealing with an diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index b53830a1eaeaa..3b656705f5568 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -90,7 +90,7 @@ def __hash__(self): # __eq__, so we explicitly do it here. return super().__hash__() - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: # We have to override __eq__ to handle NA values in _metadata. # The base class does simple == checks, which fail for NA. if isinstance(other, str): diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 063014cbe970d..8acdf32c8768e 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -1,5 +1,5 @@ """Extend pandas with custom array types""" -from typing import Any, List, Optional, Tuple, Type +from typing import List, Optional, Tuple, Type import numpy as np @@ -86,7 +86,7 @@ def __from_arrow__( def __str__(self) -> str: return self.name - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: """ Check whether 'other' is equal to self. diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 63e71183e51ef..2c601b01dbae5 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -765,7 +765,7 @@ def __hash__(self) -> int: # TODO: update this. return hash(str(self)) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, str): return other == self.name @@ -904,7 +904,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, str): return other == self.name or other == self.name.title() @@ -1077,7 +1077,7 @@ def __hash__(self) -> int: # make myself hashable return hash(str(self)) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, str): return other.lower() in (self.name.lower(), str(self).lower()) elif not isinstance(other, IntervalDtype): diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index e34af2ca2927c..13c386187a9e5 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -7,7 +7,6 @@ - .levels & .codes (FrozenNDArray) """ -from typing import Any import warnings import numpy as np @@ -78,7 +77,7 @@ def __radd__(self, other): other = list(other) return self.__class__(other + list(self)) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, (tuple, FrozenList)): other = list(other) return super().__eq__(other) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 7ac3942ef0893..35205aa7dc267 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -1764,7 +1764,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) @@ -2092,7 +2092,7 @@ def __repr__(self) -> str: ) ) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: """ compare 2 col items """ return all( getattr(self, a, None) == getattr(other, a, None) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 70df87fe0c63e..bd5e215730397 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -16,7 +16,6 @@ import os import struct import sys -from typing import Any import warnings from dateutil.relativedelta import relativedelta @@ -860,7 +859,7 @@ def __repr__(self) -> str: # not perfect :-/ return "{cls}({obj})".format(cls=self.__class__, obj=self) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: return ( isinstance(other, self.__class__) and self.string == other.string diff --git a/pandas/tests/groupby/test_function.py b/pandas/tests/groupby/test_function.py index a6643f239e83e..c41f762e9128d 100644 --- a/pandas/tests/groupby/test_function.py +++ b/pandas/tests/groupby/test_function.py @@ -3,7 +3,6 @@ from io import StringIO from itertools import product from string import ascii_lowercase -from typing import Any import numpy as np import pytest @@ -1231,7 +1230,7 @@ def __init__(self, msg="I will raise inside Cython"): super().__init__() self.msg = msg - def __eq__(self, other: Any): + def __eq__(self, other): # gets called in Cython to check that raising calls the method raise RaisingObjectException(self.msg) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index ba41c9d41431d..09a66efb6a312 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -2,7 +2,6 @@ from datetime import datetime import re -from typing import Any import weakref import numpy as np @@ -596,7 +595,7 @@ def __str__(self) -> str: __repr__ = __str__ - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: return self.value == other.value def view(self): diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index e03719f02481b..d4881ff0e1747 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -1,7 +1,6 @@ """ test the scalar Timedelta """ from datetime import timedelta import re -from typing import Any import numpy as np import pytest @@ -134,7 +133,7 @@ def generic_result(self): else: return self.cmp_result - def __eq__(self, other: Any): + def __eq__(self, other): return self.generic_result() def __gt__(self, other): diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index 7de9d2636c6e3..fce4fa6eb1eaa 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -1,6 +1,5 @@ from datetime import datetime import operator -from typing import Any import numpy as np import pytest @@ -180,7 +179,7 @@ def __gt__(self, o): def __ge__(self, o): return True - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: return isinstance(other, Inf) inf = Inf() diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index 82447a9d8cd01..977e7ded1f1a7 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -1,6 +1,5 @@ from collections import deque import string -from typing import Any import numpy as np import pytest @@ -283,7 +282,7 @@ def __add__(self, other): other = getattr(other, "value", other) return type(self)(self.value + other) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: return type(other) is Thing and self.value == other.value def __repr__(self) -> str: diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 570fa9f3a5c82..02b50d84c6eca 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1,7 +1,6 @@ from datetime import datetime from itertools import permutations import struct -from typing import Any import numpy as np from numpy.random import RandomState @@ -768,7 +767,7 @@ def test_same_object_is_in(self): # with similar behavior, then we at least should # fall back to usual python's behavior: "a in [a] == True" class LikeNan: - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: return False def __hash__(self): diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index b1af0dd0c7d50..0620f2b9aae49 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -1,7 +1,7 @@ from datetime import date, datetime, timedelta import functools import operator -from typing import Any, Optional +from typing import Optional from dateutil.easter import easter import numpy as np @@ -2577,7 +2577,7 @@ def __add__(self, other): "will overflow".format(self=self, other=other) ) - def __eq__(self, other: Any) -> bool: + def __eq__(self, other) -> bool: if isinstance(other, str): from pandas.tseries.frequencies import to_offset