From f8fbef7b9d41d632f0651920464073ffafb3021f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 11 Dec 2019 15:28:24 -0800 Subject: [PATCH 1/2] CLN: no need for mixin, move non-index timedelta arithmetic test --- pandas/tests/indexes/test_frozen.py | 20 +++----- .../indexes/timedeltas/test_arithmetic.py | 51 ------------------- .../tests/scalar/timedelta/test_arithmetic.py | 51 +++++++++++++++++++ 3 files changed, 58 insertions(+), 64 deletions(-) diff --git a/pandas/tests/indexes/test_frozen.py b/pandas/tests/indexes/test_frozen.py index 40f69ee868a90..9a96ef6c169a9 100644 --- a/pandas/tests/indexes/test_frozen.py +++ b/pandas/tests/indexes/test_frozen.py @@ -5,14 +5,16 @@ from pandas.core.indexes.frozen import FrozenList -class CheckImmutableMixin: - mutable_regex = re.compile("does not support mutable operations") +class TestFrozenList: + + unicode_container = FrozenList(["\u05d0", "\u05d1", "c"]) def check_mutable_error(self, *args, **kwargs): # Pass whatever function you normally would to pytest.raises # (after the Exception kind). + mutable_regex = re.compile("does not support mutable operations") with pytest.raises(TypeError): - self.mutable_regex(*args, **kwargs) + mutable_regex(*args, **kwargs) def test_no_mutable_funcs(self): def setitem(): @@ -34,7 +36,8 @@ def delslice(): del self.container[0:3] self.check_mutable_error(delslice) - mutable_methods = getattr(self, "mutable_methods", []) + + mutable_methods = ("extend", "pop", "remove", "insert") for meth in mutable_methods: self.check_mutable_error(getattr(self.container, meth)) @@ -49,24 +52,15 @@ def check_result(self, result, expected, klass=None): assert isinstance(result, klass) assert result == expected - -class CheckStringMixin: def test_string_methods_dont_fail(self): repr(self.container) str(self.container) bytes(self.container) def test_tricky_container(self): - if not hasattr(self, "unicode_container"): - pytest.skip("Need unicode_container to test with this") repr(self.unicode_container) str(self.unicode_container) - -class TestFrozenList(CheckImmutableMixin, CheckStringMixin): - mutable_methods = ("extend", "pop", "remove", "insert") - unicode_container = FrozenList(["\u05d0", "\u05d1", "c"]) - def setup_method(self, _): self.lst = [1, 2, 3, 4, 5] self.container = FrozenList(self.lst) diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py index 3603719eab036..b35ad579b377d 100644 --- a/pandas/tests/indexes/timedeltas/test_arithmetic.py +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -173,57 +173,6 @@ def test_tdi_isub_timedeltalike(self, delta): # ------------------------------------------------------------- - # TODO: after #24365 this probably belongs in scalar tests - def test_ops_ndarray(self): - td = Timedelta("1 day") - - # timedelta, timedelta - other = pd.to_timedelta(["1 day"]).values - expected = pd.to_timedelta(["2 days"]).values - tm.assert_numpy_array_equal(td + other, expected) - tm.assert_numpy_array_equal(other + td, expected) - msg = r"unsupported operand type\(s\) for \+: 'Timedelta' and 'int'" - with pytest.raises(TypeError, match=msg): - td + np.array([1]) - msg = r"unsupported operand type\(s\) for \+: 'numpy.ndarray' and 'Timedelta'" - with pytest.raises(TypeError, match=msg): - np.array([1]) + td - - expected = pd.to_timedelta(["0 days"]).values - tm.assert_numpy_array_equal(td - other, expected) - tm.assert_numpy_array_equal(-other + td, expected) - msg = r"unsupported operand type\(s\) for -: 'Timedelta' and 'int'" - with pytest.raises(TypeError, match=msg): - td - np.array([1]) - msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timedelta'" - with pytest.raises(TypeError, match=msg): - np.array([1]) - td - - expected = pd.to_timedelta(["2 days"]).values - tm.assert_numpy_array_equal(td * np.array([2]), expected) - tm.assert_numpy_array_equal(np.array([2]) * td, expected) - msg = ( - "ufunc '?multiply'? cannot use operands with types" - r" dtype\(' Date: Thu, 12 Dec 2019 07:48:38 -0800 Subject: [PATCH 2/2] move setup_method up --- pandas/tests/indexes/test_frozen.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/test_frozen.py b/pandas/tests/indexes/test_frozen.py index 9a96ef6c169a9..2e53e29c3fab1 100644 --- a/pandas/tests/indexes/test_frozen.py +++ b/pandas/tests/indexes/test_frozen.py @@ -9,6 +9,10 @@ class TestFrozenList: unicode_container = FrozenList(["\u05d0", "\u05d1", "c"]) + def setup_method(self, _): + self.lst = [1, 2, 3, 4, 5] + self.container = FrozenList(self.lst) + def check_mutable_error(self, *args, **kwargs): # Pass whatever function you normally would to pytest.raises # (after the Exception kind). @@ -47,9 +51,8 @@ def test_slicing_maintains_type(self): expected = self.lst[1:2] self.check_result(result, expected) - def check_result(self, result, expected, klass=None): - klass = klass or self.klass - assert isinstance(result, klass) + def check_result(self, result, expected): + assert isinstance(result, FrozenList) assert result == expected def test_string_methods_dont_fail(self): @@ -61,11 +64,6 @@ def test_tricky_container(self): repr(self.unicode_container) str(self.unicode_container) - def setup_method(self, _): - self.lst = [1, 2, 3, 4, 5] - self.container = FrozenList(self.lst) - self.klass = FrozenList - def test_add(self): result = self.container + (1, 2, 3) expected = FrozenList(self.lst + [1, 2, 3])