From 1786b2dc5cba48c6c90f00ac36b034e7d99ae038 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:05:51 -0400 Subject: [PATCH 1/6] Adding message check to pytest.raises for test_dtypes.py Found a type-o in one of the error messages and corrected it. --- pandas/core/dtypes/dtypes.py | 2 +- pandas/tests/dtypes/test_dtypes.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index d00b46700981c..c47806513baca 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -541,7 +541,7 @@ def validate_categories(categories, fastpath: bool = False): if not fastpath: if categories.hasnans: - raise ValueError("Categorial categories cannot be null") + raise ValueError("Categorical categories cannot be null") if not categories.is_unique: raise ValueError("Categorical categories must be unique") diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index a599a086ae92b..07347e2841c54 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -344,7 +344,7 @@ def test_hash_vs_equality(self): assert hash(dtype) == hash(dtype3) def test_construction(self): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid frequency: xx"): PeriodDtype("xx") for s in ["period[D]", "Period[D]", "D"]: @@ -397,16 +397,17 @@ def test_construction_from_string(self): assert is_dtype_equal(self.dtype, result) result = PeriodDtype.construct_from_string("period[D]") assert is_dtype_equal(self.dtype, result) - with pytest.raises(TypeError): + msg = "Cannot construct a 'PeriodDtype' from " + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("period[foo]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo[D]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match="list"): @@ -458,7 +459,8 @@ def test_basic(self): def test_empty(self): dt = PeriodDtype() - with pytest.raises(AttributeError): + msg = "object has no attribute 'freqstr'" + with pytest.raises(AttributeError, match=msg): str(dt) def test_not_string(self): @@ -744,11 +746,13 @@ def test_order_hashes_different(self, v1, v2): assert c1 is not c3 def test_nan_invalid(self): - with pytest.raises(ValueError): + msg = "Categorial categories cannot be null" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, np.nan]) def test_non_unique_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories must be unique" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, 1]) def test_same_categories_different_order(self): From bf777e83d1708b5810657273912422d2dd971feb Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:16:25 -0400 Subject: [PATCH 2/6] Readding message check to pytest.raises for test_dtypes.py They got undone due to new commits to pandas. --- pandas/tests/dtypes/test_dtypes.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 55b1ac819049d..e27a6b2102052 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -361,7 +361,7 @@ def test_hash_vs_equality(self, dtype): assert hash(dtype) == hash(dtype3) def test_construction(self): - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Invalid frequency: xx"): PeriodDtype("xx") for s in ["period[D]", "Period[D]", "D"]: @@ -414,16 +414,17 @@ def test_construction_from_string(self, dtype): assert is_dtype_equal(dtype, result) result = PeriodDtype.construct_from_string("period[D]") assert is_dtype_equal(dtype, result) - with pytest.raises(TypeError): + msg = "Cannot construct a 'PeriodDtype' from " + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("period[foo]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("foo[D]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns]") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match="list"): @@ -475,7 +476,8 @@ def test_basic(self, dtype): def test_empty(self): dt = PeriodDtype() - with pytest.raises(AttributeError): + msg = "object has no attribute 'freqstr'" + with pytest.raises(AttributeError, match=msg): str(dt) def test_not_string(self): @@ -764,11 +766,13 @@ def test_order_hashes_different(self, v1, v2): assert c1 is not c3 def test_nan_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories cannot be null" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, np.nan]) def test_non_unique_invalid(self): - with pytest.raises(ValueError): + msg = "Categorical categories must be unique" + with pytest.raises(ValueError, match=msg): CategoricalDtype([1, 2, 1]) def test_same_categories_different_order(self): From 1c049235a2fae6b963239459f0b4782aaf4f804f Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 20 Mar 2020 22:51:48 -0400 Subject: [PATCH 3/6] Added message to a bare pytest raises in test_series.py. It showed that the message was one of the not so great ones that I had fixed before with direction from jorisvandenbossche. --- pandas/core/generic.py | 4 ++-- pandas/tests/generic/test_series.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 83df09d6b2cf3..9d9eee5070377 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -354,7 +354,7 @@ def _get_axis_number(cls, axis): return cls._AXIS_NUMBERS[axis] except KeyError: pass - raise ValueError(f"No axis named {axis} for object type {cls}") + raise ValueError(f"No axis named {axis} for object type {cls.__name__}") @classmethod def _get_axis_name(cls, axis): @@ -367,7 +367,7 @@ def _get_axis_name(cls, axis): return cls._AXIS_NAMES[axis] except KeyError: pass - raise ValueError(f"No axis named {axis} for object type {cls}") + raise ValueError(f"No axis named {axis} for object type {cls.__name__}") def _get_axis(self, axis): name = self._get_axis_name(axis) diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 388bb8e3f636d..62c714a322f95 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -57,7 +57,8 @@ def test_set_axis_name_mi(self, func): def test_set_axis_name_raises(self): s = pd.Series([1]) - with pytest.raises(ValueError): + msg = "No axis named 1 for object type Series" + with pytest.raises(ValueError, match=msg): s._set_axis_name(name="a", axis=1) def test_get_numeric_data_preserve_dtype(self): From 8eeba97daf99ec0c7ffe2e351f09150cdb3ad805 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 20 Mar 2020 23:35:58 -0400 Subject: [PATCH 4/6] Fixed numerous error messages to use the new message format. --- pandas/tests/frame/methods/test_quantile.py | 2 +- pandas/tests/frame/methods/test_sort_values.py | 2 +- pandas/tests/frame/methods/test_to_period.py | 2 +- pandas/tests/frame/test_analytics.py | 4 ++-- pandas/tests/frame/test_api.py | 5 +---- pandas/tests/frame/test_missing.py | 2 +- pandas/tests/generic/test_generic.py | 4 ++-- pandas/tests/series/methods/test_between_time.py | 2 +- pandas/tests/series/methods/test_rank.py | 4 +--- pandas/tests/series/methods/test_sort_index.py | 2 +- pandas/tests/series/test_missing.py | 2 +- 11 files changed, 13 insertions(+), 18 deletions(-) diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 9c52e8ec5620f..49960671248e4 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -100,7 +100,7 @@ def test_quantile_axis_parameter(self): result = df.quantile(0.5, axis="columns") tm.assert_series_equal(result, expected) - msg = "No axis named -1 for object type " + msg = "No axis named -1 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis=-1) msg = ( diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 5a25d1c2c0894..3d3bb98f80ac5 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -43,7 +43,7 @@ def test_sort_values(self): sorted_df = frame.sort_values(by=["B", "A"], ascending=[True, False]) tm.assert_frame_equal(sorted_df, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.sort_values(by=["A", "B"], axis=2, inplace=True) diff --git a/pandas/tests/frame/methods/test_to_period.py b/pandas/tests/frame/methods/test_to_period.py index eac78e611b008..051461b6c554d 100644 --- a/pandas/tests/frame/methods/test_to_period.py +++ b/pandas/tests/frame/methods/test_to_period.py @@ -31,6 +31,6 @@ def test_frame_to_period(self): pts = df.to_period("M", axis=1) tm.assert_index_equal(pts.columns, exp.columns.asfreq("M")) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.to_period(axis=2) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 3964e790c7c12..3a7df29ae9091 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -919,7 +919,7 @@ def test_idxmin(self, float_frame, int_frame): expected = df.apply(Series.idxmin, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.idxmin(axis=2) @@ -934,7 +934,7 @@ def test_idxmax(self, float_frame, int_frame): expected = df.apply(Series.idxmax, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): frame.idxmax(axis=2) diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index 940a76601b75e..91627b46c2fee 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -371,10 +371,7 @@ def test_swapaxes(self): tm.assert_frame_equal(df.T, df.swapaxes(0, 1)) tm.assert_frame_equal(df.T, df.swapaxes(1, 0)) tm.assert_frame_equal(df, df.swapaxes(0, 0)) - msg = ( - "No axis named 2 for object type " - r"" - ) + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.swapaxes(2, 5) diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 196df8ba00476..470da25a922a1 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -114,7 +114,7 @@ def test_dropna(self): tm.assert_frame_equal(dropped, expected) # bad input - msg = "No axis named 3 for object type " + msg = "No axis named 3 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.dropna(axis=3) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 6999dea6adfa3..f6005a0f839a3 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -692,10 +692,10 @@ def test_squeeze(self): tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0]) tm.assert_series_equal(df.squeeze(axis="columns"), df.iloc[:, 0]) assert df.squeeze() == df.iloc[0, 0] - msg = "No axis named 2 for object type " + msg = "No axis named 2 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.squeeze(axis=2) - msg = "No axis named x for object type " + msg = "No axis named x for object type DataFrame" with pytest.raises(ValueError, match=msg): df.squeeze(axis="x") diff --git a/pandas/tests/series/methods/test_between_time.py b/pandas/tests/series/methods/test_between_time.py index 3fa26afe77a1d..e9d2f8e6f1637 100644 --- a/pandas/tests/series/methods/test_between_time.py +++ b/pandas/tests/series/methods/test_between_time.py @@ -139,6 +139,6 @@ def test_between_time_axis(self): assert len(ts.between_time(stime, etime)) == expected_length assert len(ts.between_time(stime, etime, axis=0)) == expected_length - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): ts.between_time(stime, etime, axis=1) diff --git a/pandas/tests/series/methods/test_rank.py b/pandas/tests/series/methods/test_rank.py index 3d4688c8274f9..caaffb7d5b61f 100644 --- a/pandas/tests/series/methods/test_rank.py +++ b/pandas/tests/series/methods/test_rank.py @@ -202,9 +202,7 @@ def test_rank_categorical(self): def test_rank_signature(self): s = Series([0, 1]) s.rank(method="average") - msg = ( - "No axis named average for object type " - ) + msg = "No axis named average for object type Series" with pytest.raises(ValueError, match=msg): s.rank("average") diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index 6fa4eeaee34c0..d4ebc9062a0c9 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -30,7 +30,7 @@ def test_sort_index(self, datetime_series): sorted_series = random_order.sort_index(axis=0) tm.assert_series_equal(sorted_series, datetime_series) - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): random_order.sort_values(axis=1) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index bac005465034f..15f1bc8941d47 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -823,7 +823,7 @@ def test_dropna_empty(self): assert len(s) == 0 # invalid axis - msg = "No axis named 1 for object type " + msg = "No axis named 1 for object type Series" with pytest.raises(ValueError, match=msg): s.dropna(axis=1) From 48a94c7692b827bb3ff27f38c1ee601a76d1ec31 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Sat, 21 Mar 2020 00:08:45 -0400 Subject: [PATCH 5/6] Fixing the last test! --- pandas/tests/frame/methods/test_quantile.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index 49960671248e4..0eec30cbc5c67 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -103,10 +103,7 @@ def test_quantile_axis_parameter(self): msg = "No axis named -1 for object type DataFrame" with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis=-1) - msg = ( - "No axis named column for object type " - "" - ) + msg = "No axis named column for object type DataFrame" with pytest.raises(ValueError, match=msg): df.quantile(0.1, axis="column") From 77b885fa09d44af168fb8300dbc78ee6aab06539 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Sat, 21 Mar 2020 18:54:00 -0400 Subject: [PATCH 6/6] Re-adding parameterized dtypes construction test. --- pandas/tests/dtypes/test_dtypes.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index e27a6b2102052..658d27160e3e1 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -414,22 +414,25 @@ def test_construction_from_string(self, dtype): assert is_dtype_equal(dtype, result) result = PeriodDtype.construct_from_string("period[D]") assert is_dtype_equal(dtype, result) - msg = "Cannot construct a 'PeriodDtype' from " - with pytest.raises(TypeError, match=msg): - PeriodDtype.construct_from_string("foo") - with pytest.raises(TypeError, match=msg): - PeriodDtype.construct_from_string("period[foo]") - with pytest.raises(TypeError, match=msg): - PeriodDtype.construct_from_string("foo[D]") - - with pytest.raises(TypeError, match=msg): - PeriodDtype.construct_from_string("datetime64[ns]") - with pytest.raises(TypeError, match=msg): - PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]") with pytest.raises(TypeError, match="list"): PeriodDtype.construct_from_string([1, 2, 3]) + @pytest.mark.parametrize( + "string", + [ + "foo", + "period[foo]", + "foo[D]", + "datetime64[ns]", + "datetime64[ns, US/Eastern]", + ], + ) + def test_construct_dtype_from_string_invalid_raises(self, string): + msg = f"Cannot construct a 'PeriodDtype' from '{string}'" + with pytest.raises(TypeError, match=re.escape(msg)): + PeriodDtype.construct_from_string(string) + def test_is_dtype(self, dtype): assert PeriodDtype.is_dtype(dtype) assert PeriodDtype.is_dtype("period[D]")