Skip to content

Avoid bare pytest.raises in dtypes/test_dtypes.py #32672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 changes: 1 addition & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down Expand Up @@ -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):
Expand Down
38 changes: 23 additions & 15 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand Down Expand Up @@ -414,21 +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)
with pytest.raises(TypeError):
PeriodDtype.construct_from_string("foo")
with pytest.raises(TypeError):
PeriodDtype.construct_from_string("period[foo]")
with pytest.raises(TypeError):
PeriodDtype.construct_from_string("foo[D]")

with pytest.raises(TypeError):
PeriodDtype.construct_from_string("datetime64[ns]")
with pytest.raises(TypeError):
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]")
Expand Down Expand Up @@ -475,7 +479,9 @@ def test_basic(self, dtype):

def test_empty(self):
dt = PeriodDtype()
with pytest.raises(AttributeError):
# https://github.com/pandas-dev/pandas/issues/27388
msg = "object has no attribute 'freqstr'"
with pytest.raises(AttributeError, match=msg):
str(dt)

def test_not_string(self):
Expand Down Expand Up @@ -764,11 +770,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):
Expand Down