Skip to content

Commit 09d06fe

Browse files
committed
fix tests
1 parent 2c8cd64 commit 09d06fe

File tree

6 files changed

+20
-22
lines changed

6 files changed

+20
-22
lines changed

src/_pytest/_code/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def match(self, regexp: str | re.Pattern[str]) -> Literal[True]:
763763
"""
764764
__tracebackhide__ = True
765765
value = self._stringify_exception(self.value)
766-
msg = f"Raised exception did not match: Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
766+
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
767767
if regexp == value:
768768
msg += "\n Did you mean to `re.escape()` the regex?"
769769
assert re.search(regexp, value), msg

src/_pytest/python_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
if TYPE_CHECKING:
3030
from numpy import ndarray
3131

32+
E = TypeVar("E", bound=BaseException, default=BaseException)
33+
3234

3335
def _compare_approx(
3436
full_object: object,
@@ -786,8 +788,6 @@ def _as_numpy_array(obj: object) -> ndarray | None:
786788

787789
# builtin pytest.raises helper
788790

789-
E = TypeVar("E", bound=BaseException, default=BaseException)
790-
791791

792792
@overload
793793
def raises(

src/_pytest/raises_group.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def _parse_exc(
218218
self.is_baseexception = True
219219
return cast(type[BaseExcT_1], origin_exc)
220220
else:
221-
raise TypeError(
221+
# I kinda think this should be a TypeError...
222+
raise ValueError(
222223
f"Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseExeption]` "
223224
f"are accepted as generic types but got `{exc}`. "
224225
f"As `raises` will catch all instances of the specified group regardless of the "
@@ -451,7 +452,7 @@ def __exit__(
451452
)
452453

453454
if not self.matches(exc_val):
454-
raise AssertionError(f"Raised exception did not match: {self._fail_reason}")
455+
raise AssertionError(self._fail_reason)
455456

456457
# Cast to narrow the exception type now that it's verified....
457458
# even though the TypeGuard in self.matches should be narrowing

testing/code/test_excinfo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,9 @@ def test_raises_exception_escapes_generic_group() -> None:
481481
try:
482482
with pytest.raises(ExceptionGroup[Exception]):
483483
raise ValueError("my value error")
484-
except ValueError as e:
485-
assert str(e) == "my value error"
484+
except AssertionError as e:
485+
assert str(e) == "`ValueError()` is not an instance of `ExceptionGroup`"
486+
assert str(e.__context__) == "my value error"
486487
else:
487488
pytest.fail("Expected ValueError to be raised")
488489

testing/python/raises.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def test_raises_match(self) -> None:
238238

239239
msg = "with base 16"
240240
expr = (
241-
"Raised exception did not match: Regex pattern did not match.\n"
241+
"Regex pattern did not match.\n"
242242
f" Regex: {msg!r}\n"
243243
" Input: \"invalid literal for int() with base 10: 'asdf'\""
244244
)
@@ -277,10 +277,7 @@ def test_match_failure_string_quoting(self):
277277
with pytest.raises(AssertionError, match="'foo"):
278278
raise AssertionError("'bar")
279279
(msg,) = excinfo.value.args
280-
assert (
281-
msg
282-
== '''Raised exception did not match: Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
283-
)
280+
assert msg == '''Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
284281

285282
def test_match_failure_exact_string_message(self):
286283
message = "Oh here is a message with (42) numbers in parameters"
@@ -289,7 +286,7 @@ def test_match_failure_exact_string_message(self):
289286
raise AssertionError(message)
290287
(msg,) = excinfo.value.args
291288
assert msg == (
292-
"Raised exception did not match: Regex pattern did not match.\n"
289+
"Regex pattern did not match.\n"
293290
" Regex: 'Oh here is a message with (42) numbers in parameters'\n"
294291
" Input: 'Oh here is a message with (42) numbers in parameters'\n"
295292
" Did you mean to `re.escape()` the regex?"
@@ -303,9 +300,7 @@ def test_raises_match_wrong_type(self):
303300
"""
304301
with pytest.raises(
305302
AssertionError,
306-
match=wrap_escape(
307-
"Raised exception did not match: `ValueError()` is not an instance of `IndexError`"
308-
),
303+
match=wrap_escape("`ValueError()` is not an instance of `IndexError`"),
309304
):
310305
with pytest.raises(IndexError, match="nomatch"):
311306
int("asdf")

testing/python/raises_group.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,11 +1101,10 @@ def test_raisesexc() -> None:
11011101
with RaisesExc((ValueError, TypeError)):
11021102
...
11031103

1104+
# currently RaisesGroup says "Raised exception did not match" but RaisesExc doesn't...
11041105
with pytest.raises(
11051106
AssertionError,
1106-
match=wrap_escape(
1107-
"Raised exception did not match: `TypeError()` is not an instance of `ValueError`"
1108-
),
1107+
match=wrap_escape("`TypeError()` is not an instance of `ValueError`"),
11091108
):
11101109
with RaisesExc(ValueError):
11111110
raise TypeError
@@ -1287,13 +1286,15 @@ def test_parametrizing_conditional_raisesgroup(
12871286

12881287

12891288
def test_annotated_group() -> None:
1289+
# repr depends on if exceptiongroup backport is being used or not
1290+
t = repr(ExceptionGroup[ValueError])
12901291
fail_msg = wrap_escape(
1291-
"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`."
1292+
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`."
12921293
)
1293-
with pytest.raises(TypeError, match=fail_msg):
1294+
with pytest.raises(ValueError, match=fail_msg):
12941295
with RaisesGroup(ExceptionGroup[ValueError]):
12951296
... # pragma: no cover
1296-
with pytest.raises(TypeError, match=fail_msg):
1297+
with pytest.raises(ValueError, match=fail_msg):
12971298
with RaisesExc(ExceptionGroup[ValueError]):
12981299
... # pragma: no cover
12991300
with RaisesGroup(ExceptionGroup[Exception]):

0 commit comments

Comments
 (0)