Skip to content

Commit 7bb1aa6

Browse files
dariomesicpre-commit-ci[bot]jakkdlThe-Compiler
authored
Style: Use type.__name__ in raises error messages for consistency (#13862)
* Style: Use type.__name__ in raises error messages for consistency * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update src/_pytest/raises.py Co-authored-by: Florian Bruhin <me@the-compiler.org> * Apply suggestions from code review remove the fixed FIXME comments --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: John Litborn <11260241+jakkdl@users.noreply.github.com> Co-authored-by: Florian Bruhin <me@the-compiler.org>
1 parent 2d3d169 commit 7bb1aa6

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
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
@@ -685,10 +685,11 @@ def __exit__(
685685
if exc_type is None:
686686
if not self.expected_exceptions:
687687
fail("DID NOT RAISE any exception")
688-
if len(self.expected_exceptions) > 1:
689-
fail(f"DID NOT RAISE any of {self.expected_exceptions!r}")
690-
691-
fail(f"DID NOT RAISE {self.expected_exceptions[0]!r}")
688+
if len(self.expected_exceptions) == 1:
689+
fail(f"DID NOT RAISE {self.expected_exceptions[0].__name__}")
690+
else:
691+
names = ", ".join(x.__name__ for x in self.expected_exceptions)
692+
fail(f"DID NOT RAISE any of ({names})")
692693

693694
assert self.excinfo is not None, (
694695
"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
@@ -198,15 +198,15 @@ def test_no_raise_message(self) -> None:
198198
with pytest.raises(ValueError):
199199
int("0")
200200
except pytest.fail.Exception as e:
201-
assert e.msg == f"DID NOT RAISE {ValueError!r}"
201+
assert e.msg == "DID NOT RAISE ValueError"
202202
else:
203203
assert False, "Expected pytest.raises.Exception"
204204

205205
try:
206206
with pytest.raises(ValueError):
207207
pass
208208
except pytest.fail.Exception as e:
209-
assert e.msg == f"DID NOT RAISE {ValueError!r}"
209+
assert e.msg == "DID NOT RAISE ValueError"
210210
else:
211211
assert False, "Expected pytest.raises.Exception"
212212

@@ -337,7 +337,7 @@ class ClassLooksIterableException(Exception, metaclass=Meta):
337337

338338
with pytest.raises(
339339
Failed,
340-
match=r"DID NOT RAISE <class 'raises(\..*)*ClassLooksIterableException'>",
340+
match=r"DID NOT RAISE ClassLooksIterableException",
341341
):
342342
with pytest.raises(ClassLooksIterableException):
343343
... # pragma: no cover

testing/python/raises_group.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,9 +1094,7 @@ def test_raisesexc() -> None:
10941094
with RaisesExc(ValueError):
10951095
raise ValueError
10961096

1097-
# FIXME: leaving this one formatted differently for now to not change
1098-
# tests in python/raises.py
1099-
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE <class 'ValueError'>")):
1097+
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")):
11001098
with RaisesExc(ValueError):
11011099
...
11021100

@@ -1105,11 +1103,8 @@ def test_raisesexc() -> None:
11051103
...
11061104

11071105
with pytest.raises(
1108-
# FIXME: do we want repr(type) or type.__name__ ?
11091106
Failed,
1110-
match=wrap_escape(
1111-
"DID NOT RAISE any of (<class 'ValueError'>, <class 'TypeError'>)"
1112-
),
1107+
match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"),
11131108
):
11141109
with RaisesExc((ValueError, TypeError)):
11151110
...

0 commit comments

Comments
 (0)