pytest-dev/pytest · error · TypeError

Expected , but got . RaisesGroup does not support tuples of…

Error message

Expected {expected}, but got {type(exc).__name__!r}.
RaisesGroup does not support tuples of exception types when expecting one of several possible exception types like RaisesExc.
If you meant to expect a group with multiple exceptions, list them as separate arguments.

What it means

Raised by RaisesGroup._parse_excgroup when a tuple is passed as an expected exception. Unlike RaisesExc (which accepts tuples to mean 'one of several'), RaisesGroup expects each candidate as a separate positional argument representing members of the group. Passing a tuple is ambiguous with group membership, so pytest rejects it with a TypeError and directs the user to list exceptions as separate arguments.

Solutions

  1. Splat the tuple into positional arguments: RaisesGroup(*exc_tuple).
  2. List the group members as separate arguments: RaisesGroup(ValueError, KeyError).
  3. If you actually want 'one of several' (not group membership), use RaisesExc((ValueError, KeyError)) instead of RaisesGroup.

Example fix

// before
with pytest.raises(RaisesGroup((ValueError, KeyError))):
    ...
// after
with pytest.raises(RaisesGroup(ValueError, KeyError)):
    ...
// or splat:
with pytest.raises(RaisesGroup(*candidates)):
    ...
Defensive patterns

Strategy: validation

Validate before calling

assert not isinstance(expected_exception, tuple), 'RaisesGroup expects separate args, not a tuple; use RaisesGroup(*tuple)'

Type guard

def raisesgroup_arg_not_tuple(expected_exception) -> bool:
    return not isinstance(expected_exception, tuple)

Prevention

When it happens

Trigger: RaisesGroup((ValueError, KeyError)); RaisesGroup(exc_tuple). The `case tuple()` arm of the match statement fires.

Common situations: Carrying over pytest.raises(ValueError, KeyError) or RaisesExc((ValueError, KeyError)) habits into RaisesGroup; building a RaisesGroup from a programmatically-built tuple of types.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/4294631925c37ad5. Report an issue: GitHub.

Appendix: source

Thrown at src/_pytest/raises.py:1027

            | RaisesGroup[BaseExcT_2]
        ),
        expected: str,
    ) -> type[BaseExcT_co] | RaisesExc[BaseExcT_1] | RaisesGroup[BaseExcT_2]:
        # verify exception type and set `self.is_baseexception`
        match exc:
            case RaisesGroup() if self.flatten_subgroups:
                raise ValueError(
                    "You cannot specify a nested structure inside a RaisesGroup with"
                    " `flatten_subgroups=True`. The parameter will flatten subgroups"
                    " in the raised exceptiongroup before matching, which would never"
                    " match a nested structure.",
                )
            case RaisesGroup() | RaisesExc():
                self.is_baseexception |= exc.is_baseexception
                exc._nested = True
                return exc
            case tuple():
                raise TypeError(
                    f"Expected {expected}, but got {type(exc).__name__!r}.\n"
                    "RaisesGroup does not support tuples of exception types when expecting one of "
                    "several possible exception types like RaisesExc.\n"
                    "If you meant to expect a group with multiple exceptions, list them as separate arguments."
                )
            case _:
                return super()._parse_exc(exc, expected)

    @overload
    def __enter__(
        self: RaisesGroup[ExcT_1],
    ) -> ExceptionInfo[ExceptionGroup[ExcT_1]]: ...
    @overload
    def __enter__(
        self: RaisesGroup[BaseExcT_1],
    ) -> ExceptionInfo[BaseExceptionGroup[BaseExcT_1]]: ...

    def __enter__(self) -> ExceptionInfo[BaseExceptionGroup[BaseException]]:

View on GitHub (pinned to 0d6fbdeffa)