pytest-dev/pytest · error · TypeError

Expected {expected}, but got {type(exc).__name__!r}. RaisesG

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

Unlike RaisesExc/pytest.raises, RaisesGroup does NOT accept a tuple of exception types as a single positional argument. To expect a group with multiple exceptions, pass them as separate positional arguments.

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 98b357f69e)

Solutions

  1. Spread the types as separate positional args: RaisesGroup(ValueError, TypeError).
  2. If you hold a tuple variable, splat it: RaisesGroup(*my_exc_tuple).
  3. If you meant 'one of several types in a single slot', use RaisesExc(check=lambda e: isinstance(e, (...))) instead.

Example fix

// before
RaisesGroup((ValueError, TypeError))
// after
RaisesGroup(ValueError, TypeError)
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(expected, tuple):
    expected = list(expected)  # will be spread
RaisesGroup(*expected)

Type guard

def is_flat_exception_list(exc) -> TypeGuard[list]:
    return not isinstance(exc, tuple)

Prevention

When it happens

Trigger: Calling RaisesGroup((ValueError, TypeError)) — a single tuple argument. Hits raises.py:1027 in _parse_excgroup via the `case tuple():` branch.

Common situations: Developer copies the tuple syntax familiar from pytest.raises(ExceptionGroup, (A, B)) into RaisesGroup; programmatic construction passes a tuple by mistake.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/4294631925c37ad5.json. Report an issue: GitHub.