pytest-dev / pytest-dev/pytest

`pytest.RaisesGroup` calls the check function with simple exceptions instead of the group

Open
#14,324 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

The following code:

import pytest

def foo():
    raise ExceptionGroup("Main message", [ValueError("foo")])

def check(exc_group: ExceptionGroup) -> bool:
    print(exc_group.__class__)
    # example check
    return len(exc_group.exceptions) > 1

def test_foo():
    with pytest.RaisesGroup(ValueError, match="Main message", check=check):
        foo()

errors with AttributeError: 'ValueError' object has no attribute 'exceptions' because the check function gets invoked twice, the first time with the exception group for actually performing the check, and the second time with the ValueError raised by the foo function.

As a further proof, the stdout of the run is:

<class 'ExceptionGroup'>
<class 'ValueError'>

This seems wrong, as the docs of RaisesGroup says

check (Callable[[E], bool]) – If specified, a callable that will be called with the group as a parameter after successfully matching the expected exceptions. If it returns True it will be considered a match, if not it will be considered a failed match.

I'm on python 3.12, with just pytest==9.0.2 installed in the environment, but I can reproduce the same issue on 3.14.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the pytest.RaisesGroup entry point and reproduce the supplied example on Python 3.12 or 3.14. Trace the matching path to determine why check receives both the ExceptionGroup and its ValueError, then add a regression test showing that check is called only with the group and passes when the group matches.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.