Skip to content

Commit de5f0be

Browse files
committed
Add extra test, changelog improvement
1 parent a74e557 commit de5f0be

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

changelog/13115.improvement.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
Allows supplying ``ExceptionGroup[Exception]`` and ``BaseExceptionGroup[BaseException]`` to ``pytest.raises`` to keep full typing on ExcInfo.
2-
Parametrizing with other element types remains an error - we do not check the types of child exceptions and thus do not permit code that might look like we do.
1+
Allows supplying ``ExceptionGroup[Exception]`` and ``BaseExceptionGroup[BaseException]`` to ``pytest.raises`` to keep full typing on :class:`ExceptionInfo <pytest.ExceptionInfo>`:
2+
3+
.. code-block:: python
4+
5+
with pytest.raises(ExceptionGroup[Exception]) as exc_info:
6+
some_function()
7+
8+
Parametrizing with other exception types remains an error - we do not check the types of child exceptions and thus do not permit code that might look like we do.

src/_pytest/python_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ def raises(
972972
expected_exceptions = expected_exception
973973

974974
def validate_exc(exc: type[E]) -> type[E]:
975+
__tracebackhide__ = True
975976
origin_exc: type[E] | None = get_origin(exc)
976977
if origin_exc and issubclass(origin_exc, BaseExceptionGroup):
977978
exc_type = get_args(exc)[0]

testing/code/test_excinfo.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,14 @@ def test_division_zero():
455455

456456

457457
def test_raises_accepts_generic_group() -> None:
458-
exc_group = ExceptionGroup("", [RuntimeError()])
459458
with pytest.raises(ExceptionGroup[Exception]) as exc_info:
460-
raise exc_group
459+
raise ExceptionGroup("", [RuntimeError()])
461460
assert exc_info.group_contains(RuntimeError)
462461

463462

464463
def test_raises_accepts_generic_base_group() -> None:
465-
exc_group = ExceptionGroup("", [RuntimeError()])
466464
with pytest.raises(BaseExceptionGroup[BaseException]) as exc_info:
467-
raise exc_group
465+
raise ExceptionGroup("", [RuntimeError()])
468466
assert exc_info.group_contains(RuntimeError)
469467

470468

@@ -474,12 +472,21 @@ def test_raises_rejects_specific_generic_group() -> None:
474472

475473

476474
def test_raises_accepts_generic_group_in_tuple() -> None:
477-
exc_group = ExceptionGroup("", [RuntimeError()])
478475
with pytest.raises((ValueError, ExceptionGroup[Exception])) as exc_info:
479-
raise exc_group
476+
raise ExceptionGroup("", [RuntimeError()])
480477
assert exc_info.group_contains(RuntimeError)
481478

482479

480+
def test_raises_exception_escapes_generic_group() -> None:
481+
try:
482+
with pytest.raises(ExceptionGroup[Exception]):
483+
raise ValueError("my value error")
484+
except ValueError as e:
485+
assert str(e) == "my value error"
486+
else:
487+
pytest.fail("Expected ValueError to be raised")
488+
489+
483490
class TestGroupContains:
484491
def test_contains_exception_type(self) -> None:
485492
exc_group = ExceptionGroup("", [RuntimeError()])

0 commit comments

Comments
 (0)