Skip to content

Commit 5c4e99b

Browse files
committed
Refactor: Improve clarity of 'expected exception' error messages
1 parent b3f3263 commit 5c4e99b

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/_pytest/raises.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def _parse_exc(
464464
f"with `RaisesGroup`."
465465
)
466466
# unclear if the Type/ValueError distinction is even helpful here
467-
msg = f"expected exception must be {expected}, not "
467+
msg = f"Expected {expected}, but got "
468468
if isinstance(exc, type): # type: ignore[unreachable]
469469
raise ValueError(msg + f"{exc.__name__!r}")
470470
if isinstance(exc, BaseException): # type: ignore[unreachable]
@@ -1036,7 +1036,7 @@ def _parse_excgroup(
10361036
return exc
10371037
elif isinstance(exc, tuple):
10381038
raise TypeError(
1039-
f"expected exception must be {expected}, not {type(exc).__name__!r}.\n"
1039+
f"Expected {expected}, but got {type(exc).__name__!r}.\n"
10401040
"RaisesGroup does not support tuples of exception types when expecting one of "
10411041
"several possible exception types like RaisesExc.\n"
10421042
"If you meant to expect a group with multiple exceptions, list them as separate arguments."

testing/python/raises.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def test_expected_exception_is_not_a_baseexception(self) -> None:
368368
with pytest.raises(
369369
TypeError,
370370
match=wrap_escape(
371-
"expected exception must be a BaseException type, not 'str'"
371+
"Expected a BaseException type, but got 'str'"
372372
),
373373
):
374374
with pytest.raises("hello"): # type: ignore[call-overload]
@@ -380,7 +380,7 @@ class NotAnException:
380380
with pytest.raises(
381381
ValueError,
382382
match=wrap_escape(
383-
"expected exception must be a BaseException type, not 'NotAnException'"
383+
"Expected a BaseException type, but got 'NotAnException'"
384384
),
385385
):
386386
with pytest.raises(NotAnException): # type: ignore[type-var]
@@ -389,7 +389,7 @@ class NotAnException:
389389
with pytest.raises(
390390
TypeError,
391391
match=wrap_escape(
392-
"expected exception must be a BaseException type, not 'str'"
392+
"Expected a BaseException type, but got 'str'"
393393
),
394394
):
395395
with pytest.raises(("hello", NotAnException)): # type: ignore[arg-type]

testing/python/raises_group.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ def fails_raises_group(msg: str, add_prefix: bool = True) -> RaisesExc[Failed]:
3636
def test_raises_group() -> None:
3737
with pytest.raises(
3838
TypeError,
39-
match=wrap_escape("expected exception must be a BaseException type, not 'int'"),
39+
match=wrap_escape("Expected a BaseException type, but got 'int'"),
4040
):
4141
RaisesExc(5) # type: ignore[call-overload]
4242
with pytest.raises(
4343
ValueError,
44-
match=wrap_escape("expected exception must be a BaseException type, not 'int'"),
44+
match=wrap_escape("Expected a BaseException type, but got 'int'"),
4545
):
4646
RaisesExc(int) # type: ignore[type-var]
4747
with pytest.raises(
4848
TypeError,
49-
# TODO: bad sentence structure
5049
match=wrap_escape(
51-
"expected exception must be a BaseException type, RaisesExc, or RaisesGroup, not an exception instance (ValueError)",
50+
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got an exception instance: ValueError",
5251
),
5352
):
5453
RaisesGroup(ValueError()) # type: ignore[call-overload]
@@ -1079,7 +1078,7 @@ def test_raisesexc() -> None:
10791078
with pytest.raises(
10801079
ValueError,
10811080
match=wrap_escape(
1082-
"expected exception must be a BaseException type, not 'object'"
1081+
"Expected a BaseException type, but got 'object'"
10831082
),
10841083
):
10851084
RaisesExc(object) # type: ignore[type-var]
@@ -1351,7 +1350,7 @@ def test_tuples() -> None:
13511350
with pytest.raises(
13521351
TypeError,
13531352
match=wrap_escape(
1354-
"expected exception must be a BaseException type, RaisesExc, or RaisesGroup, not 'tuple'.\n"
1353+
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got 'tuple'.\n"
13551354
"RaisesGroup does not support tuples of exception types when expecting one of "
13561355
"several possible exception types like RaisesExc.\n"
13571356
"If you meant to expect a group with multiple exceptions, list them as separate arguments."

0 commit comments

Comments
 (0)