Skip to content

Commit

Permalink
Add extra test, changelog improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jan 21, 2025
1 parent a74e557 commit de5f0be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
10 changes: 8 additions & 2 deletions changelog/13115.improvement.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
Allows supplying ``ExceptionGroup[Exception]`` and ``BaseExceptionGroup[BaseException]`` to ``pytest.raises`` to keep full typing on ExcInfo.
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.
Allows supplying ``ExceptionGroup[Exception]`` and ``BaseExceptionGroup[BaseException]`` to ``pytest.raises`` to keep full typing on :class:`ExceptionInfo <pytest.ExceptionInfo>`:

.. code-block:: python
with pytest.raises(ExceptionGroup[Exception]) as exc_info:
some_function()
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.
1 change: 1 addition & 0 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ def raises(
expected_exceptions = expected_exception

def validate_exc(exc: type[E]) -> type[E]:
__tracebackhide__ = True
origin_exc: type[E] | None = get_origin(exc)
if origin_exc and issubclass(origin_exc, BaseExceptionGroup):
exc_type = get_args(exc)[0]
Expand Down
19 changes: 13 additions & 6 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,14 @@ def test_division_zero():


def test_raises_accepts_generic_group() -> None:
exc_group = ExceptionGroup("", [RuntimeError()])
with pytest.raises(ExceptionGroup[Exception]) as exc_info:
raise exc_group
raise ExceptionGroup("", [RuntimeError()])
assert exc_info.group_contains(RuntimeError)


def test_raises_accepts_generic_base_group() -> None:
exc_group = ExceptionGroup("", [RuntimeError()])
with pytest.raises(BaseExceptionGroup[BaseException]) as exc_info:
raise exc_group
raise ExceptionGroup("", [RuntimeError()])
assert exc_info.group_contains(RuntimeError)


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


def test_raises_accepts_generic_group_in_tuple() -> None:
exc_group = ExceptionGroup("", [RuntimeError()])
with pytest.raises((ValueError, ExceptionGroup[Exception])) as exc_info:
raise exc_group
raise ExceptionGroup("", [RuntimeError()])
assert exc_info.group_contains(RuntimeError)


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"
else:
pytest.fail("Expected ValueError to be raised")


class TestGroupContains:
def test_contains_exception_type(self) -> None:
exc_group = ExceptionGroup("", [RuntimeError()])
Expand Down

0 comments on commit de5f0be

Please sign in to comment.