Skip to content

Commit 19624de

Browse files
authored
Avoid bare pytest.raises in dtypes/test_dtypes.py (#32672)
1 parent 3df837a commit 19624de

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def validate_categories(categories, fastpath: bool = False):
558558
if not fastpath:
559559

560560
if categories.hasnans:
561-
raise ValueError("Categorial categories cannot be null")
561+
raise ValueError("Categorical categories cannot be null")
562562

563563
if not categories.is_unique:
564564
raise ValueError("Categorical categories must be unique")

pandas/tests/arrays/categorical/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def test_constructor_not_sequence(self):
252252
def test_constructor_with_null(self):
253253

254254
# Cannot have NaN in categories
255-
msg = "Categorial categories cannot be null"
255+
msg = "Categorical categories cannot be null"
256256
with pytest.raises(ValueError, match=msg):
257257
Categorical([np.nan, "a", "b", "c"], categories=[np.nan, "a", "b", "c"])
258258

@@ -500,7 +500,7 @@ def test_from_codes_non_unique_categories(self):
500500
Categorical.from_codes([0, 1, 2], categories=["a", "a", "b"])
501501

502502
def test_from_codes_nan_cat_included(self):
503-
with pytest.raises(ValueError, match="Categorial categories cannot be null"):
503+
with pytest.raises(ValueError, match="Categorical categories cannot be null"):
504504
Categorical.from_codes([0, 1, 2], categories=["a", "b", np.nan])
505505

506506
def test_from_codes_too_negative(self):

pandas/tests/dtypes/test_dtypes.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def test_hash_vs_equality(self, dtype):
361361
assert hash(dtype) == hash(dtype3)
362362

363363
def test_construction(self):
364-
with pytest.raises(ValueError):
364+
with pytest.raises(ValueError, match="Invalid frequency: xx"):
365365
PeriodDtype("xx")
366366

367367
for s in ["period[D]", "Period[D]", "D"]:
@@ -414,21 +414,25 @@ def test_construction_from_string(self, dtype):
414414
assert is_dtype_equal(dtype, result)
415415
result = PeriodDtype.construct_from_string("period[D]")
416416
assert is_dtype_equal(dtype, result)
417-
with pytest.raises(TypeError):
418-
PeriodDtype.construct_from_string("foo")
419-
with pytest.raises(TypeError):
420-
PeriodDtype.construct_from_string("period[foo]")
421-
with pytest.raises(TypeError):
422-
PeriodDtype.construct_from_string("foo[D]")
423-
424-
with pytest.raises(TypeError):
425-
PeriodDtype.construct_from_string("datetime64[ns]")
426-
with pytest.raises(TypeError):
427-
PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]")
428417

429418
with pytest.raises(TypeError, match="list"):
430419
PeriodDtype.construct_from_string([1, 2, 3])
431420

421+
@pytest.mark.parametrize(
422+
"string",
423+
[
424+
"foo",
425+
"period[foo]",
426+
"foo[D]",
427+
"datetime64[ns]",
428+
"datetime64[ns, US/Eastern]",
429+
],
430+
)
431+
def test_construct_dtype_from_string_invalid_raises(self, string):
432+
msg = f"Cannot construct a 'PeriodDtype' from '{string}'"
433+
with pytest.raises(TypeError, match=re.escape(msg)):
434+
PeriodDtype.construct_from_string(string)
435+
432436
def test_is_dtype(self, dtype):
433437
assert PeriodDtype.is_dtype(dtype)
434438
assert PeriodDtype.is_dtype("period[D]")
@@ -475,7 +479,9 @@ def test_basic(self, dtype):
475479

476480
def test_empty(self):
477481
dt = PeriodDtype()
478-
with pytest.raises(AttributeError):
482+
# https://github.com/pandas-dev/pandas/issues/27388
483+
msg = "object has no attribute 'freqstr'"
484+
with pytest.raises(AttributeError, match=msg):
479485
str(dt)
480486

481487
def test_not_string(self):
@@ -764,11 +770,13 @@ def test_order_hashes_different(self, v1, v2):
764770
assert c1 is not c3
765771

766772
def test_nan_invalid(self):
767-
with pytest.raises(ValueError):
773+
msg = "Categorical categories cannot be null"
774+
with pytest.raises(ValueError, match=msg):
768775
CategoricalDtype([1, 2, np.nan])
769776

770777
def test_non_unique_invalid(self):
771-
with pytest.raises(ValueError):
778+
msg = "Categorical categories must be unique"
779+
with pytest.raises(ValueError, match=msg):
772780
CategoricalDtype([1, 2, 1])
773781

774782
def test_same_categories_different_order(self):

0 commit comments

Comments
 (0)