From 2072395eb4143ef7e38aeb6da164ead2fec81b6e Mon Sep 17 00:00:00 2001 From: dariomesic Date: Wed, 29 Oct 2025 23:50:24 +0100 Subject: [PATCH 1/4] Style: Use type.__name__ in raises error messages for consistency --- changelog/13862.improvement.rst | 1 + src/_pytest/raises.py | 9 +++++---- testing/python/raises.py | 6 +++--- testing/python/raises_group.py | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 changelog/13862.improvement.rst diff --git a/changelog/13862.improvement.rst b/changelog/13862.improvement.rst new file mode 100644 index 00000000000..6a89cbc323b --- /dev/null +++ b/changelog/13862.improvement.rst @@ -0,0 +1 @@ +Improved the readability of "DID NOT RAISE" error messages by using the exception type's name instead of its `repr`. diff --git a/src/_pytest/raises.py b/src/_pytest/raises.py index ad56a8c14d2..f6b90d97b61 100644 --- a/src/_pytest/raises.py +++ b/src/_pytest/raises.py @@ -704,10 +704,11 @@ def __exit__( if exc_type is None: if not self.expected_exceptions: fail("DID NOT RAISE any exception") - if len(self.expected_exceptions) > 1: - fail(f"DID NOT RAISE any of {self.expected_exceptions!r}") - - fail(f"DID NOT RAISE {self.expected_exceptions[0]!r}") + if len(self.expected_exceptions) == 1: + fail(f"DID NOT RAISE {self.expected_exceptions[0].__name__}") + else: + names = ", ".join(f"{x.__name__}" for x in self.expected_exceptions) + fail(f"DID NOT RAISE any of ({names})") assert self.excinfo is not None, ( "Internal error - should have been constructed in __enter__" diff --git a/testing/python/raises.py b/testing/python/raises.py index 0bf02a8063f..3db9f160320 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -194,7 +194,7 @@ def test_no_raise_message(self) -> None: try: pytest.raises(ValueError, int, "0") except pytest.fail.Exception as e: - assert e.msg == f"DID NOT RAISE {ValueError!r}" + assert e.msg == f"DID NOT RAISE ValueError" else: assert False, "Expected pytest.raises.Exception" @@ -202,7 +202,7 @@ def test_no_raise_message(self) -> None: with pytest.raises(ValueError): pass except pytest.fail.Exception as e: - assert e.msg == f"DID NOT RAISE {ValueError!r}" + assert e.msg == f"DID NOT RAISE ValueError" else: assert False, "Expected pytest.raises.Exception" @@ -333,7 +333,7 @@ class ClassLooksIterableException(Exception, metaclass=Meta): with pytest.raises( Failed, - match=r"DID NOT RAISE ", + match=r"DID NOT RAISE ClassLooksIterableException", ): pytest.raises(ClassLooksIterableException, lambda: None) diff --git a/testing/python/raises_group.py b/testing/python/raises_group.py index 0247b118c58..aa9ca6d3d85 100644 --- a/testing/python/raises_group.py +++ b/testing/python/raises_group.py @@ -1099,7 +1099,7 @@ def test_raisesexc() -> None: # FIXME: leaving this one formatted differently for now to not change # tests in python/raises.py - with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ")): + with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")): with RaisesExc(ValueError): ... @@ -1111,7 +1111,7 @@ def test_raisesexc() -> None: # FIXME: do we want repr(type) or type.__name__ ? Failed, match=wrap_escape( - "DID NOT RAISE any of (, )" + "DID NOT RAISE any of (ValueError, TypeError)" ), ): with RaisesExc((ValueError, TypeError)): From c1bcde5fc16a58d76b35ef8e927e081ec6696a0c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 14:20:41 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/python/raises.py | 4 ++-- testing/python/raises_group.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/testing/python/raises.py b/testing/python/raises.py index 3db9f160320..203bc8e74ba 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -194,7 +194,7 @@ def test_no_raise_message(self) -> None: try: pytest.raises(ValueError, int, "0") except pytest.fail.Exception as e: - assert e.msg == f"DID NOT RAISE ValueError" + assert e.msg == "DID NOT RAISE ValueError" else: assert False, "Expected pytest.raises.Exception" @@ -202,7 +202,7 @@ def test_no_raise_message(self) -> None: with pytest.raises(ValueError): pass except pytest.fail.Exception as e: - assert e.msg == f"DID NOT RAISE ValueError" + assert e.msg == "DID NOT RAISE ValueError" else: assert False, "Expected pytest.raises.Exception" diff --git a/testing/python/raises_group.py b/testing/python/raises_group.py index aa9ca6d3d85..25eb5ae0a49 100644 --- a/testing/python/raises_group.py +++ b/testing/python/raises_group.py @@ -1110,9 +1110,7 @@ def test_raisesexc() -> None: with pytest.raises( # FIXME: do we want repr(type) or type.__name__ ? Failed, - match=wrap_escape( - "DID NOT RAISE any of (ValueError, TypeError)" - ), + match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"), ): with RaisesExc((ValueError, TypeError)): ... From ebdb46671b49da143c179090e751b754cd62a099 Mon Sep 17 00:00:00 2001 From: John Litborn <11260241+jakkdl@users.noreply.github.com> Date: Wed, 10 Dec 2025 18:11:07 +0100 Subject: [PATCH 3/4] Update src/_pytest/raises.py Co-authored-by: Florian Bruhin --- src/_pytest/raises.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/raises.py b/src/_pytest/raises.py index 85e54d301ca..0b698d75504 100644 --- a/src/_pytest/raises.py +++ b/src/_pytest/raises.py @@ -707,7 +707,7 @@ def __exit__( if len(self.expected_exceptions) == 1: fail(f"DID NOT RAISE {self.expected_exceptions[0].__name__}") else: - names = ", ".join(f"{x.__name__}" for x in self.expected_exceptions) + names = ", ".join(x.__name__ for x in self.expected_exceptions) fail(f"DID NOT RAISE any of ({names})") assert self.excinfo is not None, ( From fb7afc95140feff5823e74f6bf8d6af17f53a57a Mon Sep 17 00:00:00 2001 From: John Litborn <11260241+jakkdl@users.noreply.github.com> Date: Wed, 10 Dec 2025 18:15:09 +0100 Subject: [PATCH 4/4] Apply suggestions from code review remove the fixed FIXME comments --- testing/python/raises_group.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/testing/python/raises_group.py b/testing/python/raises_group.py index 1a4fdf51ede..8b311bd0eed 100644 --- a/testing/python/raises_group.py +++ b/testing/python/raises_group.py @@ -1094,8 +1094,6 @@ def test_raisesexc() -> None: with RaisesExc(ValueError): raise ValueError - # FIXME: leaving this one formatted differently for now to not change - # tests in python/raises.py with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")): with RaisesExc(ValueError): ... @@ -1105,7 +1103,6 @@ def test_raisesexc() -> None: ... with pytest.raises( - # FIXME: do we want repr(type) or type.__name__ ? Failed, match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"), ):