Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Feb 20, 2025
1 parent 2c8cd64 commit 09d06fe
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
if TYPE_CHECKING:
from numpy import ndarray

E = TypeVar("E", bound=BaseException, default=BaseException)


def _compare_approx(
full_object: object,
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
13 changes: 4 additions & 9 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'\""
)
Expand Down Expand Up @@ -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"
Expand All @@ -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?"
Expand All @@ -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")
Expand Down
13 changes: 7 additions & 6 deletions testing/python/raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]):
Expand Down

0 comments on commit 09d06fe

Please sign in to comment.