Skip to content

Commit 6df9dae

Browse files
ShaharNavehWillAyd
authored andcommitted
TST: Removed some bare pytest raises (#30825)
1 parent bdad43b commit 6df9dae

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

pandas/tests/arithmetic/test_numeric.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,16 @@ def test_df_numeric_cmp_dt64_raises(self):
6565
# GH#8932, GH#22163
6666
ts = pd.Timestamp.now()
6767
df = pd.DataFrame({"x": range(5)})
68-
with pytest.raises(TypeError):
68+
69+
msg = "Invalid comparison between dtype=int64 and Timestamp"
70+
71+
with pytest.raises(TypeError, match=msg):
6972
df > ts
70-
with pytest.raises(TypeError):
73+
with pytest.raises(TypeError, match=msg):
7174
df < ts
72-
with pytest.raises(TypeError):
75+
with pytest.raises(TypeError, match=msg):
7376
ts < df
74-
with pytest.raises(TypeError):
77+
with pytest.raises(TypeError, match=msg):
7578
ts > df
7679

7780
assert not (df == ts).any().any()

pandas/tests/io/pytables/test_store.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@
6666
class TestHDFStore:
6767
def test_format_kwarg_in_constructor(self, setup_path):
6868
# GH 13291
69+
70+
msg = "format is not a defined argument for HDFStore"
71+
6972
with ensure_clean_path(setup_path) as path:
70-
with pytest.raises(ValueError):
73+
with pytest.raises(ValueError, match=msg):
7174
HDFStore(path, format="table")
7275

7376
def test_context(self, setup_path):
@@ -203,21 +206,27 @@ def test_api(self, setup_path):
203206
# Invalid.
204207
df = tm.makeDataFrame()
205208

206-
with pytest.raises(ValueError):
209+
msg = "Can only append to Tables"
210+
211+
with pytest.raises(ValueError, match=msg):
207212
df.to_hdf(path, "df", append=True, format="f")
208213

209-
with pytest.raises(ValueError):
214+
with pytest.raises(ValueError, match=msg):
210215
df.to_hdf(path, "df", append=True, format="fixed")
211216

212-
with pytest.raises(TypeError):
217+
msg = r"invalid HDFStore format specified \[foo\]"
218+
219+
with pytest.raises(TypeError, match=msg):
213220
df.to_hdf(path, "df", append=True, format="foo")
214221

215-
with pytest.raises(TypeError):
216-
df.to_hdf(path, "df", append=False, format="bar")
222+
with pytest.raises(TypeError, match=msg):
223+
df.to_hdf(path, "df", append=False, format="foo")
217224

218225
# File path doesn't exist
219226
path = ""
220-
with pytest.raises(FileNotFoundError):
227+
msg = f"File {path} does not exist"
228+
229+
with pytest.raises(FileNotFoundError, match=msg):
221230
read_hdf(path, "df")
222231

223232
def test_api_default_format(self, setup_path):
@@ -230,7 +239,10 @@ def test_api_default_format(self, setup_path):
230239
_maybe_remove(store, "df")
231240
store.put("df", df)
232241
assert not store.get_storer("df").is_table
233-
with pytest.raises(ValueError):
242+
243+
msg = "Can only append to Tables"
244+
245+
with pytest.raises(ValueError, match=msg):
234246
store.append("df2", df)
235247

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

257269
pd.set_option("io.hdf.default_format", "table")
@@ -384,7 +396,10 @@ def test_versioning(self, setup_path):
384396
# this is an error because its table_type is appendable, but no
385397
# version info
386398
store.get_node("df2")._v_attrs.pandas_version = None
387-
with pytest.raises(Exception):
399+
400+
msg = "'NoneType' object has no attribute 'startswith'"
401+
402+
with pytest.raises(Exception, match=msg):
388403
store.select("df2")
389404

390405
def test_mode(self, setup_path):
@@ -428,7 +443,11 @@ def check(mode):
428443

429444
# conv read
430445
if mode in ["w"]:
431-
with pytest.raises(ValueError):
446+
msg = (
447+
"mode w is not allowed while performing a read. "
448+
r"Allowed modes are r, r\+ and a."
449+
)
450+
with pytest.raises(ValueError, match=msg):
432451
read_hdf(path, "df", mode=mode)
433452
else:
434453
result = read_hdf(path, "df", mode=mode)

0 commit comments

Comments
 (0)