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
- Spread the types as separate positional args: RaisesGroup(ValueError, TypeError).
- If you hold a tuple variable, splat it: RaisesGroup(*my_exc_tuple).
- 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
- Pass multiple expected types as separate positional args, not a tuple.
- Splat existing tuples: RaisesGroup(*my_tuple).
- Remember RaisesGroup's API differs from raises — no tuple-of-types shortcut.
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
- You cannot specify multiple exceptions with `allow_unwrapped
- `allow_unwrapped=True` has no effect when expecting a `Raise
- `allow_unwrapped=True` bypasses the `match` and `check` para
- You cannot specify a nested structure inside a RaisesGroup w
- absolute tolerance can't be NaN.
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/4294631925c37ad5.json.
Report an issue: GitHub.