Skip to content

Commit a395424

Browse files
committed
Style: Use type.__name__ in raises error messages for consistency
1 parent b3f3263 commit a395424

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

changelog/13862.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved the readability of "DID NOT RAISE" error messages by using the exception type's name instead of its `repr`.

src/_pytest/raises.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,11 @@ def __exit__(
704704
if exc_type is None:
705705
if not self.expected_exceptions:
706706
fail("DID NOT RAISE any exception")
707-
if len(self.expected_exceptions) > 1:
708-
fail(f"DID NOT RAISE any of {self.expected_exceptions!r}")
709-
710-
fail(f"DID NOT RAISE {self.expected_exceptions[0]!r}")
707+
if len(self.expected_exceptions) == 1:
708+
fail(f"DID NOT RAISE {self.expected_exceptions[0].__name__!r}")
709+
else:
710+
names = ", ".join(f"{x.__name__}" for x in self.expected_exceptions)
711+
fail(f"DID NOT RAISE any of ({names})")
711712

712713
assert self.excinfo is not None, (
713714
"Internal error - should have been constructed in __enter__"

testing/python/raises.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ def test_no_raise_message(self) -> None:
194194
try:
195195
pytest.raises(ValueError, int, "0")
196196
except pytest.fail.Exception as e:
197-
assert e.msg == f"DID NOT RAISE {ValueError!r}"
197+
assert e.msg == f"DID NOT RAISE ValueError"
198198
else:
199199
assert False, "Expected pytest.raises.Exception"
200200

201201
try:
202202
with pytest.raises(ValueError):
203203
pass
204204
except pytest.fail.Exception as e:
205-
assert e.msg == f"DID NOT RAISE {ValueError!r}"
205+
assert e.msg == f"DID NOT RAISE ValueError"
206206
else:
207207
assert False, "Expected pytest.raises.Exception"
208208

@@ -333,7 +333,7 @@ class ClassLooksIterableException(Exception, metaclass=Meta):
333333

334334
with pytest.raises(
335335
Failed,
336-
match=r"DID NOT RAISE <class 'raises(\..*)*ClassLooksIterableException'>",
336+
match=r"DID NOT RAISE ClassLooksIterableException",
337337
):
338338
pytest.raises(ClassLooksIterableException, lambda: None)
339339

testing/python/raises_group.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ def test_raisesexc() -> None:
10991099

11001100
# FIXME: leaving this one formatted differently for now to not change
11011101
# tests in python/raises.py
1102-
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE <class 'ValueError'>")):
1102+
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")):
11031103
with RaisesExc(ValueError):
11041104
...
11051105

@@ -1111,7 +1111,7 @@ def test_raisesexc() -> None:
11111111
# FIXME: do we want repr(type) or type.__name__ ?
11121112
Failed,
11131113
match=wrap_escape(
1114-
"DID NOT RAISE any of (<class 'ValueError'>, <class 'TypeError'>)"
1114+
"DID NOT RAISE any of (ValueError, TypeError)"
11151115
),
11161116
):
11171117
with RaisesExc((ValueError, TypeError)):

0 commit comments

Comments
 (0)