From 3d3d78a897043d41fcce205e9c4d7515a01f1dbf Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Sat, 28 Mar 2020 23:06:32 +0530 Subject: [PATCH 1/5] Replace pytest.raises with external_error_raised for non panda specific error --- pandas/tests/indexes/timedeltas/test_delete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/timedeltas/test_delete.py b/pandas/tests/indexes/timedeltas/test_delete.py index 593ed7bb0a1ac..1740995d7d309 100644 --- a/pandas/tests/indexes/timedeltas/test_delete.py +++ b/pandas/tests/indexes/timedeltas/test_delete.py @@ -30,7 +30,7 @@ def test_delete(self): assert result.name == expected.name assert result.freq == expected.freq - with pytest.raises((IndexError, ValueError)): + with tm.external_error_raised((IndexError, ValueError)): # either depending on numpy version idx.delete(5) From 9a41a10282fd452689e47bad995427b9b42e6e88 Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Sat, 28 Mar 2020 23:11:39 +0530 Subject: [PATCH 2/5] Match error message for pytest.raises --- pandas/core/arrays/period.py | 3 ++- pandas/tests/indexes/categorical/test_category.py | 2 +- pandas/tests/indexes/period/test_constructors.py | 3 ++- pandas/tests/indexes/period/test_join.py | 3 ++- pandas/tests/indexes/period/test_partial_slicing.py | 8 +++++--- pandas/tests/indexes/period/test_setops.py | 9 ++++++--- 6 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index c24b0b5fa64b8..3422c2004c20a 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -166,7 +166,8 @@ def __init__(self, values, freq=None, dtype=None, copy=False): @classmethod def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray": # alias for PeriodArray.__init__ - assert isinstance(values, np.ndarray) and values.dtype == "i8" + assertion_msg = f"Should be numpy array of type i8" + assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg return cls(values, freq=freq, **kwargs) @classmethod diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 6e8e81230b2bb..954601ad423bf 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -192,7 +192,7 @@ def test_delete(self): expected = CategoricalIndex(list("aabbc"), categories=categories) tm.assert_index_equal(result, expected, exact=True) - with pytest.raises((IndexError, ValueError)): + with tm.external_error_raised((IndexError, ValueError)): # Either depending on NumPy version ci.delete(10) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index cb2140d0b4025..c26175f797f92 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -327,7 +327,8 @@ def test_constructor_simple_new(self): result = idx._simple_new(idx._data, name="p") tm.assert_index_equal(result, idx) - with pytest.raises(AssertionError): + msg = f"Should be numpy array of type i8" + with pytest.raises(AssertionError, match=msg): # Need ndarray, not Int64Index type(idx._data)._simple_new(idx.astype("i8"), freq=idx.freq) diff --git a/pandas/tests/indexes/period/test_join.py b/pandas/tests/indexes/period/test_join.py index 9e3df0c32d6d5..2147580fe9d5b 100644 --- a/pandas/tests/indexes/period/test_join.py +++ b/pandas/tests/indexes/period/test_join.py @@ -39,5 +39,6 @@ def test_join_does_not_recur(self): def test_join_mismatched_freq_raises(self): index = period_range("1/1/2000", "1/20/2000", freq="D") index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - with pytest.raises(IncompatibleFrequency): + msg = fr".*Input has different freq=2D from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.join(index3) diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 833901ea7ba22..841bb8d63435d 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -59,6 +59,7 @@ def test_range_slice_day(self): didx = pd.date_range(start="2013/01/01", freq="D", periods=400) pidx = period_range(start="2013/01/01", freq="D", periods=400) + msg = f"slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError values = [ @@ -69,7 +70,7 @@ def test_range_slice_day(self): "2013/02/01 09:00", ] for v in values: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] s = Series(np.random.rand(len(idx)), index=idx) @@ -81,13 +82,14 @@ def test_range_slice_day(self): invalid = ["2013/02/01 9H", "2013/02/01 09:00"] for v in invalid: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] def test_range_slice_seconds(self): # GH#6716 didx = pd.date_range(start="2013/01/01 09:00:00", freq="S", periods=4000) pidx = period_range(start="2013/01/01 09:00:00", freq="S", periods=4000) + msg = f"slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError @@ -99,7 +101,7 @@ def test_range_slice_seconds(self): "2013/02/01 09:00", ] for v in values: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): idx[v:] s = Series(np.random.rand(len(idx)), index=idx) diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 88f1687b8bb10..a869af5d9ce42 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -148,7 +148,8 @@ def test_union_misc(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - with pytest.raises(IncompatibleFrequency): + msg = fr".*Input has different freq=W-WED from PeriodIndex/(freq=D/)" + with pytest.raises(IncompatibleFrequency, match=msg): index.union(index2, sort=sort) # TODO: belongs elsewhere @@ -180,11 +181,13 @@ def test_intersection(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - with pytest.raises(IncompatibleFrequency): + msg = fr".*Input has different freq=W-WED from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index2, sort=sort) index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - with pytest.raises(IncompatibleFrequency): + msg = fr".*Input has different freq=2D from PeriodIndex\(freq=D\)" + with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index3, sort=sort) def test_intersection_cases(self, sort): From 283cc74beb4c75cd3f67de60f02de3ad503c8c87 Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Sun, 29 Mar 2020 11:28:07 +0530 Subject: [PATCH 3/5] Update regex pattern to match error message correctly in test_setops.py --- pandas/tests/indexes/period/test_setops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index a869af5d9ce42..057300138a8d2 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -148,7 +148,7 @@ def test_union_misc(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - msg = fr".*Input has different freq=W-WED from PeriodIndex/(freq=D/)" + msg = fr"Input has different freq=W-WED from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.union(index2, sort=sort) @@ -181,12 +181,12 @@ def test_intersection(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - msg = fr".*Input has different freq=W-WED from PeriodIndex\(freq=D\)" + msg = fr"Input has different freq=W-WED from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index2, sort=sort) index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - msg = fr".*Input has different freq=2D from PeriodIndex\(freq=D\)" + msg = fr"Input has different freq=2D from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index3, sort=sort) From 2ff3f41143ed39c8cf84f6633d67d9b2a1dae5bb Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Sun, 29 Mar 2020 12:13:47 +0530 Subject: [PATCH 4/5] Remove unused imports --- pandas/tests/indexes/timedeltas/test_delete.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/indexes/timedeltas/test_delete.py b/pandas/tests/indexes/timedeltas/test_delete.py index 1740995d7d309..63f2b450aa818 100644 --- a/pandas/tests/indexes/timedeltas/test_delete.py +++ b/pandas/tests/indexes/timedeltas/test_delete.py @@ -1,5 +1,3 @@ -import pytest - from pandas import TimedeltaIndex, timedelta_range import pandas._testing as tm From 0930fd098ea7514608d132692c9d6d1ad420848f Mon Sep 17 00:00:00 2001 From: Sumanau Sareen Date: Sun, 29 Mar 2020 15:34:35 +0530 Subject: [PATCH 5/5] Replace f string with string whereever not required --- pandas/core/arrays/period.py | 2 +- pandas/tests/indexes/period/test_constructors.py | 2 +- pandas/tests/indexes/period/test_join.py | 2 +- pandas/tests/indexes/period/test_partial_slicing.py | 4 ++-- pandas/tests/indexes/period/test_setops.py | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 3422c2004c20a..be9cc53d33d6f 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -166,7 +166,7 @@ def __init__(self, values, freq=None, dtype=None, copy=False): @classmethod def _simple_new(cls, values: np.ndarray, freq=None, **kwargs) -> "PeriodArray": # alias for PeriodArray.__init__ - assertion_msg = f"Should be numpy array of type i8" + assertion_msg = "Should be numpy array of type i8" assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg return cls(values, freq=freq, **kwargs) diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index c26175f797f92..4ec7ef64e2272 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -327,7 +327,7 @@ def test_constructor_simple_new(self): result = idx._simple_new(idx._data, name="p") tm.assert_index_equal(result, idx) - msg = f"Should be numpy array of type i8" + msg = "Should be numpy array of type i8" with pytest.raises(AssertionError, match=msg): # Need ndarray, not Int64Index type(idx._data)._simple_new(idx.astype("i8"), freq=idx.freq) diff --git a/pandas/tests/indexes/period/test_join.py b/pandas/tests/indexes/period/test_join.py index 2147580fe9d5b..8a68561dd5819 100644 --- a/pandas/tests/indexes/period/test_join.py +++ b/pandas/tests/indexes/period/test_join.py @@ -39,6 +39,6 @@ def test_join_does_not_recur(self): def test_join_mismatched_freq_raises(self): index = period_range("1/1/2000", "1/20/2000", freq="D") index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - msg = fr".*Input has different freq=2D from PeriodIndex\(freq=D\)" + msg = r".*Input has different freq=2D from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.join(index3) diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 841bb8d63435d..c0597180184a6 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -59,7 +59,7 @@ def test_range_slice_day(self): didx = pd.date_range(start="2013/01/01", freq="D", periods=400) pidx = period_range(start="2013/01/01", freq="D", periods=400) - msg = f"slice indices must be integers or None or have an __index__ method" + msg = "slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError values = [ @@ -89,7 +89,7 @@ def test_range_slice_seconds(self): # GH#6716 didx = pd.date_range(start="2013/01/01 09:00:00", freq="S", periods=4000) pidx = period_range(start="2013/01/01 09:00:00", freq="S", periods=4000) - msg = f"slice indices must be integers or None or have an __index__ method" + msg = "slice indices must be integers or None or have an __index__ method" for idx in [didx, pidx]: # slices against index should raise IndexError diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 057300138a8d2..71b827d83b836 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -148,7 +148,7 @@ def test_union_misc(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - msg = fr"Input has different freq=W-WED from PeriodIndex\(freq=D\)" + msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.union(index2, sort=sort) @@ -181,12 +181,12 @@ def test_intersection(self, sort): # raise if different frequencies index = period_range("1/1/2000", "1/20/2000", freq="D") index2 = period_range("1/1/2000", "1/20/2000", freq="W-WED") - msg = fr"Input has different freq=W-WED from PeriodIndex\(freq=D\)" + msg = r"Input has different freq=W-WED from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index2, sort=sort) index3 = period_range("1/1/2000", "1/20/2000", freq="2D") - msg = fr"Input has different freq=2D from PeriodIndex\(freq=D\)" + msg = r"Input has different freq=2D from PeriodIndex\(freq=D\)" with pytest.raises(IncompatibleFrequency, match=msg): index.intersection(index3, sort=sort)