From 1786b2dc5cba48c6c90f00ac36b034e7d99ae038 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 10 Mar 2020 21:05:51 -0400 Subject: [PATCH 1/4] 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/4] 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 20094d9ee6b27c29541bfa3fe6726c0412489217 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Tue, 17 Mar 2020 20:47:07 -0400 Subject: [PATCH 3/4] Fixed bare pytest raises in test_indexing.py. --- pandas/tests/indexes/categorical/test_indexing.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index 507e38d9acac2..d6ccfe50059c6 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -65,7 +65,8 @@ def test_take_fill_value(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - with pytest.raises(IndexError): + msg = "index -5 is out of bounds for size 3" + with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5])) def test_take_fill_value_datetime(self): @@ -104,7 +105,8 @@ def test_take_fill_value_datetime(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - with pytest.raises(IndexError): + msg = "index -5 is out of bounds for size 3" + with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5])) def test_take_invalid_kwargs(self): From 5bc868f7498b43931847cdc8d5786a3d4e76baf7 Mon Sep 17 00:00:00 2001 From: Derek McCammond Date: Wed, 18 Mar 2020 23:43:59 -0400 Subject: [PATCH 4/4] Changed two tests as the output appears to be different on some mac and linux builds. --- pandas/tests/indexes/categorical/test_indexing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index d6ccfe50059c6..1d41e17e327a8 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -65,7 +65,7 @@ def test_take_fill_value(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - msg = "index -5 is out of bounds for size 3" + msg = "index -5 is out of bounds for (axis 0 with )?size 3" with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5])) @@ -105,7 +105,7 @@ def test_take_fill_value_datetime(self): with pytest.raises(ValueError, match=msg): idx.take(np.array([1, 0, -5]), fill_value=True) - msg = "index -5 is out of bounds for size 3" + msg = "index -5 is out of bounds for (axis 0 with )?size 3" with pytest.raises(IndexError, match=msg): idx.take(np.array([1, -5]))