From 1786b2dc5cba48c6c90f00ac36b034e7d99ae038 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:05:51 -0400 Subject: [PATCH 1/7] 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/7] 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 2de67aa295d3233eef52d032b7a199d923979555 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:05:51 -0400 Subject: [PATCH 3/7] 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 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 181f0c8906853..d29102cbd4604 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -558,7 +558,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") From e0dc7acbe7489525ba295ca4b3d3a2a3be9558e8 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:16:25 -0400 Subject: [PATCH 4/7] 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 ef61889958a43bf415eaefde98a85edd2ed0c5c5 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Fri, 13 Mar 2020 13:52:39 -0400 Subject: [PATCH 5/7] Added comment referencing an issue that may be related Co-Authored-By: Simon Hawkins --- pandas/tests/dtypes/test_dtypes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index e27a6b2102052..664edad34caad 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -476,6 +476,7 @@ def test_basic(self, dtype): def test_empty(self): dt = PeriodDtype() + # https://github.com/pandas-dev/pandas/issues/27388 msg = "object has no attribute 'freqstr'" with pytest.raises(AttributeError, match=msg): str(dt) From 1c17b5d3313156032514869404d1c95604bc3ce4 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Mon, 16 Mar 2020 18:49:22 -0400 Subject: [PATCH 6/7] Parametrized the PeriodDType tests based on feedback given by simonjayhawkins. --- 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]") From 89683377aa786a560a1e9daab895d94d23bb34ab Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 19:25:04 -0400 Subject: [PATCH 7/7] Fixed the wording of the other two tests that used "categorial" --- pandas/tests/arrays/categorical/test_constructors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index c6b4c4904735c..3e31c1acbe09d 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -252,7 +252,7 @@ def test_constructor_not_sequence(self): def test_constructor_with_null(self): # Cannot have NaN in categories - msg = "Categorial categories cannot be null" + msg = "Categorical categories cannot be null" with pytest.raises(ValueError, match=msg): Categorical([np.nan, "a", "b", "c"], categories=[np.nan, "a", "b", "c"]) @@ -500,7 +500,7 @@ def test_from_codes_non_unique_categories(self): Categorical.from_codes([0, 1, 2], categories=["a", "a", "b"]) def test_from_codes_nan_cat_included(self): - with pytest.raises(ValueError, match="Categorial categories cannot be null"): + with pytest.raises(ValueError, match="Categorical categories cannot be null"): Categorical.from_codes([0, 1, 2], categories=["a", "b", np.nan]) def test_from_codes_too_negative(self):