From 09d06fe99dcf2493383c0ef82d04e9561ab0fd7e Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 20 Feb 2025 17:14:43 +0100
Subject: [PATCH] fix tests
---
src/_pytest/_code/code.py | 2 +-
src/_pytest/python_api.py | 4 ++--
src/_pytest/raises_group.py | 5 +++--
testing/code/test_excinfo.py | 5 +++--
testing/python/raises.py | 13 ++++---------
testing/python/raises_group.py | 13 +++++++------
6 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index 429b856596b..5b85d295d36 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -763,7 +763,7 @@ def match(self, regexp: str | re.Pattern[str]) -> Literal[True]:
"""
__tracebackhide__ = True
value = self._stringify_exception(self.value)
- msg = f"Raised exception did not match: Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
+ msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?"
assert re.search(regexp, value), msg
diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py
index 8a11a128293..b1da082134f 100644
--- a/src/_pytest/python_api.py
+++ b/src/_pytest/python_api.py
@@ -29,6 +29,8 @@
if TYPE_CHECKING:
from numpy import ndarray
+ E = TypeVar("E", bound=BaseException, default=BaseException)
+
def _compare_approx(
full_object: object,
@@ -786,8 +788,6 @@ def _as_numpy_array(obj: object) -> ndarray | None:
# builtin pytest.raises helper
-E = TypeVar("E", bound=BaseException, default=BaseException)
-
@overload
def raises(
diff --git a/src/_pytest/raises_group.py b/src/_pytest/raises_group.py
index e549b8e8975..d314f14af1c 100644
--- a/src/_pytest/raises_group.py
+++ b/src/_pytest/raises_group.py
@@ -218,7 +218,8 @@ def _parse_exc(
self.is_baseexception = True
return cast(type[BaseExcT_1], origin_exc)
else:
- raise TypeError(
+ # I kinda think this should be a TypeError...
+ raise ValueError(
f"Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseExeption]` "
f"are accepted as generic types but got `{exc}`. "
f"As `raises` will catch all instances of the specified group regardless of the "
@@ -451,7 +452,7 @@ def __exit__(
)
if not self.matches(exc_val):
- raise AssertionError(f"Raised exception did not match: {self._fail_reason}")
+ raise AssertionError(self._fail_reason)
# Cast to narrow the exception type now that it's verified....
# even though the TypeGuard in self.matches should be narrowing
diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py
index 89088576980..438a5259f20 100644
--- a/testing/code/test_excinfo.py
+++ b/testing/code/test_excinfo.py
@@ -481,8 +481,9 @@ def test_raises_exception_escapes_generic_group() -> None:
try:
with pytest.raises(ExceptionGroup[Exception]):
raise ValueError("my value error")
- except ValueError as e:
- assert str(e) == "my value error"
+ except AssertionError as e:
+ assert str(e) == "`ValueError()` is not an instance of `ExceptionGroup`"
+ assert str(e.__context__) == "my value error"
else:
pytest.fail("Expected ValueError to be raised")
diff --git a/testing/python/raises.py b/testing/python/raises.py
index c7ecc301701..65081b2adb1 100644
--- a/testing/python/raises.py
+++ b/testing/python/raises.py
@@ -238,7 +238,7 @@ def test_raises_match(self) -> None:
msg = "with base 16"
expr = (
- "Raised exception did not match: Regex pattern did not match.\n"
+ "Regex pattern did not match.\n"
f" Regex: {msg!r}\n"
" Input: \"invalid literal for int() with base 10: 'asdf'\""
)
@@ -277,10 +277,7 @@ def test_match_failure_string_quoting(self):
with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar")
(msg,) = excinfo.value.args
- assert (
- msg
- == '''Raised exception did not match: Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
- )
+ assert msg == '''Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
def test_match_failure_exact_string_message(self):
message = "Oh here is a message with (42) numbers in parameters"
@@ -289,7 +286,7 @@ def test_match_failure_exact_string_message(self):
raise AssertionError(message)
(msg,) = excinfo.value.args
assert msg == (
- "Raised exception did not match: Regex pattern did not match.\n"
+ "Regex pattern did not match.\n"
" Regex: 'Oh here is a message with (42) numbers in parameters'\n"
" Input: 'Oh here is a message with (42) numbers in parameters'\n"
" Did you mean to `re.escape()` the regex?"
@@ -303,9 +300,7 @@ def test_raises_match_wrong_type(self):
"""
with pytest.raises(
AssertionError,
- match=wrap_escape(
- "Raised exception did not match: `ValueError()` is not an instance of `IndexError`"
- ),
+ match=wrap_escape("`ValueError()` is not an instance of `IndexError`"),
):
with pytest.raises(IndexError, match="nomatch"):
int("asdf")
diff --git a/testing/python/raises_group.py b/testing/python/raises_group.py
index 5e00aaa06bb..2619eb41c1d 100644
--- a/testing/python/raises_group.py
+++ b/testing/python/raises_group.py
@@ -1101,11 +1101,10 @@ def test_raisesexc() -> None:
with RaisesExc((ValueError, TypeError)):
...
+ # currently RaisesGroup says "Raised exception did not match" but RaisesExc doesn't...
with pytest.raises(
AssertionError,
- match=wrap_escape(
- "Raised exception did not match: `TypeError()` is not an instance of `ValueError`"
- ),
+ match=wrap_escape("`TypeError()` is not an instance of `ValueError`"),
):
with RaisesExc(ValueError):
raise TypeError
@@ -1287,13 +1286,15 @@ def test_parametrizing_conditional_raisesgroup(
def test_annotated_group() -> None:
+ # repr depends on if exceptiongroup backport is being used or not
+ t = repr(ExceptionGroup[ValueError])
fail_msg = wrap_escape(
- "Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseExeption]` are accepted as generic types but got `ExceptionGroup[ValueError]`. As `raises` will catch all instances of the specified group regardless of the generic argument specific nested exceptions has to be checked with `RaisesGroup`."
+ f"Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseExeption]` are accepted as generic types but got `{t}`. As `raises` will catch all instances of the specified group regardless of the generic argument specific nested exceptions has to be checked with `RaisesGroup`."
)
- with pytest.raises(TypeError, match=fail_msg):
+ with pytest.raises(ValueError, match=fail_msg):
with RaisesGroup(ExceptionGroup[ValueError]):
... # pragma: no cover
- with pytest.raises(TypeError, match=fail_msg):
+ with pytest.raises(ValueError, match=fail_msg):
with RaisesExc(ExceptionGroup[ValueError]):
... # pragma: no cover
with RaisesGroup(ExceptionGroup[Exception]):