pytest-dev/pytest · error · ValueError

You cannot specify multiple exceptions with `allow_unwrapped

Error message

You cannot specify multiple exceptions with `allow_unwrapped=True.` If you want to match one of multiple possible exceptions you should use a `RaisesExc`. E.g. `RaisesExc(check=lambda e: isinstance(e, (...)))`

What it means

allow_unwrapped only makes sense for a single expected exception; specifying multiple exceptions alongside allow_unwrapped=True is ambiguous because unwrapping applies to exactly one exception. Use RaisesExc with a check for 'one of several'.

Source

Thrown at src/_pytest/raises.py:971

            Callable[[BaseExceptionGroup[BaseExcT_1]], bool]
            | Callable[[ExceptionGroup[ExcT_1]], bool]
            | None
        ) = None,
    ):
        # The type hint on the `self` and `check` parameters uses different formats
        # that are *very* hard to reconcile while adhering to the overloads, so we cast
        # it to avoid an error when passing it to super().__init__
        check = cast(
            "Callable[[BaseExceptionGroup[ExcT_1|BaseExcT_1|BaseExceptionGroup[BaseExcT_2]]], bool]",
            check,
        )
        super().__init__(match=match, check=check)
        self.allow_unwrapped = allow_unwrapped
        self.flatten_subgroups: bool = flatten_subgroups
        self.is_baseexception = False

        if allow_unwrapped and other_exceptions:
            raise ValueError(
                "You cannot specify multiple exceptions with `allow_unwrapped=True.`"
                " If you want to match one of multiple possible exceptions you should"
                " use a `RaisesExc`."
                " E.g. `RaisesExc(check=lambda e: isinstance(e, (...)))`",
            )
        if allow_unwrapped and isinstance(expected_exception, RaisesGroup):
            raise ValueError(
                "`allow_unwrapped=True` has no effect when expecting a `RaisesGroup`."
                " You might want it in the expected `RaisesGroup`, or"
                " `flatten_subgroups=True` if you don't care about the structure.",
            )
        if allow_unwrapped and (match is not None or check is not None):
            raise ValueError(
                "`allow_unwrapped=True` bypasses the `match` and `check` parameters"
                " if the exception is unwrapped. If you intended to match/check the"
                " exception you should use a `RaisesExc` object. If you want to match/check"
                " the exceptiongroup when the exception *is* wrapped you need to"
                " do e.g. `if isinstance(exc.value, ExceptionGroup):"

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Use RaisesExc(check=lambda e: isinstance(e, (ValueError, TypeError))) to express 'one of several', optionally inside a RaisesGroup with allow_unwrapped.
  2. Drop allow_unwrapped if you genuinely expect a group containing multiple exceptions.
  3. Split into separate RaisesGroup blocks per exception type if the structure differs.

Example fix

// before
with RaisesGroup(ValueError, TypeError, allow_unwrapped=True):
    raise ValueError()
// after
with RaisesGroup(RaisesExc(check=lambda e: isinstance(e, (ValueError, TypeError))), allow_unwrapped=True):
    raise ValueError()
Defensive patterns

Strategy: validation

Validate before calling

if allow_unwrapped and len(expected) > 1:
    raise ValueError('use RaisesExc(check=...) for one-of-several with allow_unwrapped')
RaisesGroup(*expected, allow_unwrapped=allow_unwrapped)

Prevention

When it happens

Trigger: Calling RaisesGroup(ValueError, TypeError, allow_unwrapped=True) — two or more positional exceptions with allow_unwrapped. Hits raises.py:971.

Common situations: Developer wants to match one of several possible exceptions that may or may not be wrapped in a group; misunderstanding that allow_unwrapped is singular.

Related errors


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