Skip to content

TST: Added 'match=' to some bare pytest.raises #30825

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ def test_df_numeric_cmp_dt64_raises(self):
# GH#8932, GH#22163
ts = pd.Timestamp.now()
df = pd.DataFrame({"x": range(5)})
with pytest.raises(TypeError):

msg = "Invalid comparison between dtype=int64 and Timestamp"

with pytest.raises(TypeError, match=msg):
df > ts
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
df < ts
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
ts < df
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
ts > df

assert not (df == ts).any().any()
Expand Down
41 changes: 30 additions & 11 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@
class TestHDFStore:
def test_format_kwarg_in_constructor(self, setup_path):
# GH 13291

msg = "format is not a defined argument for HDFStore"

with ensure_clean_path(setup_path) as path:
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
HDFStore(path, format="table")

def test_context(self, setup_path):
Expand Down Expand Up @@ -203,21 +206,27 @@ def test_api(self, setup_path):
# Invalid.
df = tm.makeDataFrame()

with pytest.raises(ValueError):
msg = "Can only append to Tables"

with pytest.raises(ValueError, match=msg):
df.to_hdf(path, "df", append=True, format="f")

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.to_hdf(path, "df", append=True, format="fixed")

with pytest.raises(TypeError):
msg = r"invalid HDFStore format specified \[foo\]"

with pytest.raises(TypeError, match=msg):
df.to_hdf(path, "df", append=True, format="foo")

with pytest.raises(TypeError):
df.to_hdf(path, "df", append=False, format="bar")
with pytest.raises(TypeError, match=msg):
df.to_hdf(path, "df", append=False, format="foo")

# File path doesn't exist
path = ""
with pytest.raises(FileNotFoundError):
msg = f"File {path} does not exist"

with pytest.raises(FileNotFoundError, match=msg):
read_hdf(path, "df")

def test_api_default_format(self, setup_path):
Expand All @@ -230,7 +239,10 @@ def test_api_default_format(self, setup_path):
_maybe_remove(store, "df")
store.put("df", df)
assert not store.get_storer("df").is_table
with pytest.raises(ValueError):

msg = "Can only append to Tables"

with pytest.raises(ValueError, match=msg):
store.append("df2", df)

pd.set_option("io.hdf.default_format", "table")
Expand All @@ -251,7 +263,7 @@ def test_api_default_format(self, setup_path):
df.to_hdf(path, "df")
with HDFStore(path) as store:
assert not store.get_storer("df").is_table
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.to_hdf(path, "df2", append=True)

pd.set_option("io.hdf.default_format", "table")
Expand Down Expand Up @@ -384,7 +396,10 @@ def test_versioning(self, setup_path):
# this is an error because its table_type is appendable, but no
# version info
store.get_node("df2")._v_attrs.pandas_version = None
with pytest.raises(Exception):

msg = "'NoneType' object has no attribute 'startswith'"

with pytest.raises(Exception, match=msg):
store.select("df2")

def test_mode(self, setup_path):
Expand Down Expand Up @@ -428,7 +443,11 @@ def check(mode):

# conv read
if mode in ["w"]:
with pytest.raises(ValueError):
msg = (
"mode w is not allowed while performing a read. "
r"Allowed modes are r, r\+ and a."
)
with pytest.raises(ValueError, match=msg):
read_hdf(path, "df", mode=mode)
else:
result = read_hdf(path, "df", mode=mode)
Expand Down