Skip to content

TST: add messages to bare pytest raises #38414

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
5 changes: 4 additions & 1 deletion pandas/tests/generic/methods/test_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ def test_pipe_tuple_error(self, frame_or_series):
obj = obj["A"]

f = lambda x, y: y
with pytest.raises(ValueError):

msg = "y is both the pipe target and a keyword argument"

with pytest.raises(ValueError, match=msg):
obj.pipe((f, "y"), x=1, y=0)
42 changes: 27 additions & 15 deletions pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,29 @@ def test_multi_index(self):
# MultiIndex is prohibited
trades = self.trades.set_index(["time", "price"])
quotes = self.quotes.set_index("time")
with pytest.raises(MergeError):
with pytest.raises(MergeError, match="left can only have one index"):
merge_asof(trades, quotes, left_index=True, right_index=True)

trades = self.trades.set_index("time")
quotes = self.quotes.set_index(["time", "bid"])
with pytest.raises(MergeError):
with pytest.raises(MergeError, match="right can only have one index"):
merge_asof(trades, quotes, left_index=True, right_index=True)

def test_on_and_index(self):

# "on" parameter and index together is prohibited
trades = self.trades.set_index("time")
quotes = self.quotes.set_index("time")
with pytest.raises(MergeError):
msg = 'Can only pass argument "left_on" OR "left_index" not both.'
with pytest.raises(MergeError, match=msg):
merge_asof(
trades, quotes, left_on="price", left_index=True, right_index=True
)

trades = self.trades.set_index("time")
quotes = self.quotes.set_index("time")
with pytest.raises(MergeError):
msg = 'Can only pass argument "right_on" OR "right_index" not both.'
with pytest.raises(MergeError, match=msg):
merge_asof(
trades, quotes, right_on="bid", left_index=True, right_index=True
)
Expand Down Expand Up @@ -439,7 +441,9 @@ def test_multiby_indexed(self):

tm.assert_frame_equal(expected, result)

with pytest.raises(MergeError):
with pytest.raises(
MergeError, match="left_by and right_by must be same length"
):
pd.merge_asof(
left,
right,
Expand Down Expand Up @@ -478,13 +482,15 @@ def test_valid_join_keys(self):
trades = self.trades
quotes = self.quotes

with pytest.raises(MergeError):
msg = r"incompatible merge keys \[1\] .* must be the same type"

with pytest.raises(MergeError, match=msg):
merge_asof(trades, quotes, left_on="time", right_on="bid", by="ticker")

with pytest.raises(MergeError):
with pytest.raises(MergeError, match="can only asof on a key for left"):
merge_asof(trades, quotes, on=["time", "ticker"], by="ticker")

with pytest.raises(MergeError):
with pytest.raises(MergeError, match="can only asof on a key for left"):
merge_asof(trades, quotes, by="ticker")

def test_with_duplicates(self, datapath):
Expand Down Expand Up @@ -513,7 +519,9 @@ def test_valid_allow_exact_matches(self):
trades = self.trades
quotes = self.quotes

with pytest.raises(MergeError):
msg = "allow_exact_matches must be boolean, passed foo"

with pytest.raises(MergeError, match=msg):
merge_asof(
trades, quotes, on="time", by="ticker", allow_exact_matches="foo"
)
Expand All @@ -535,12 +543,14 @@ def test_valid_tolerance(self):
tolerance=1,
)

msg = r"incompatible tolerance .*, must be compat with type .*"

# incompat
with pytest.raises(MergeError):
with pytest.raises(MergeError, match=msg):
merge_asof(trades, quotes, on="time", by="ticker", tolerance=1)

# invalid
with pytest.raises(MergeError):
with pytest.raises(MergeError, match=msg):
merge_asof(
trades.reset_index(),
quotes.reset_index(),
Expand All @@ -549,13 +559,15 @@ def test_valid_tolerance(self):
tolerance=1.0,
)

msg = "tolerance must be positive"

# invalid negative
with pytest.raises(MergeError):
with pytest.raises(MergeError, match=msg):
merge_asof(
trades, quotes, on="time", by="ticker", tolerance=-Timedelta("1s")
)

with pytest.raises(MergeError):
with pytest.raises(MergeError, match=msg):
merge_asof(
trades.reset_index(),
quotes.reset_index(),
Expand All @@ -572,13 +584,13 @@ def test_non_sorted(self):
# we require that we are already sorted on time & quotes
assert not trades.time.is_monotonic
assert not quotes.time.is_monotonic
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="left keys must be sorted"):
merge_asof(trades, quotes, on="time", by="ticker")

trades = self.trades.sort_values("time")
assert trades.time.is_monotonic
assert not quotes.time.is_monotonic
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="right keys must be sorted"):
merge_asof(trades, quotes, on="time", by="ticker")

quotes = self.quotes.sort_values("time")
Expand Down