pytest-dev/pytest · error · ValueError
Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[Base
Error message
Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseException]` are accepted as generic types but got `{exc}`. As `raises` will catch all instances of the specified group regardless of the generic argument specific nested exceptions has to be checked with `RaisesGroup`. What it means
Raised when a parameterized exception-group generic type is passed whose nested type argument is restricted. pytest.raises only accepts ExceptionGroup[Exception] or BaseExceptionGroup[BaseException] as generic forms because raises catches all instances of the group class regardless of the type parameter — specific nested types must be checked via RaisesGroup.
Source
Thrown at src/_pytest/raises.py:441
if not issubclass(exc, Exception):
self.is_baseexception = True
return exc
# because RaisesGroup does not support variable number of exceptions there's
# still a use for RaisesExc(ExceptionGroup[Exception]).
origin_exc: type[BaseException] | None = get_origin(exc)
if origin_exc and issubclass(origin_exc, BaseExceptionGroup):
exc_type = get_args(exc)[0]
if (
issubclass(origin_exc, ExceptionGroup) and exc_type in (Exception, Any)
) or (
issubclass(origin_exc, BaseExceptionGroup)
and exc_type in (BaseException, Any)
):
if not issubclass(origin_exc, ExceptionGroup):
self.is_baseexception = True
return cast(type[BaseExcT_1], origin_exc)
else:
raise ValueError(
f"Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseException]` "
f"are accepted as generic types but got `{exc}`. "
f"As `raises` will catch all instances of the specified group regardless of the "
f"generic argument specific nested exceptions has to be checked "
f"with `RaisesGroup`."
)
# unclear if the Type/ValueError distinction is even helpful here
msg = f"Expected {expected}, but got "
if isinstance(exc, type): # type: ignore[unreachable]
raise ValueError(msg + f"{exc.__name__!r}")
if isinstance(exc, BaseException): # type: ignore[unreachable]
raise TypeError(msg + f"an exception instance: {type(exc).__name__}")
raise TypeError(msg + repr(type(exc).__name__))
@property
def fail_reason(self) -> str | None:
"""Set after a call to :meth:`matches` to give a human-readable reason for why the match failed.
When used as a context manager the string will be printed as the reason for theView on GitHub (pinned to 98b357f69e)
Solutions
- Use pytest.RaisesGroup(ValueError) to assert specific nested exception types within the group.
- If you truly want to catch any group regardless of contents, use the bare ExceptionGroup[Exception] or BaseExceptionGroup[BaseException] generic.
- For 'one of several nested types', use RaisesGroup(RaisesExc(check=lambda e: isinstance(e, (ValueError, TypeError)))).
Example fix
// before
with pytest.raises(ExceptionGroup[ValueError]):
raise ExceptionGroup('', [ValueError()])
// after
with pytest.RaisesGroup(ValueError):
raise ExceptionGroup('', [ValueError()]) Defensive patterns
Strategy: validation
Validate before calling
from typing import get_origin, get_args
import sys
if sys.version_info >= (3, 11):
o = get_origin(expected_exception)
if o and issubclass(o, BaseExceptionGroup):
a = get_args(expected_exception)[0]
if a not in (Exception, BaseException) and a is not Any:
# use RaisesGroup instead
... Prevention
- Use pytest.RaisesGroup(NestedType) when you care about nested exception types.
- Only use ExceptionGroup[Exception] / BaseExceptionGroup[BaseException] bare generics with raises.
- Remember the generic parameter on raises does NOT filter nested types — it is informational only.
When it happens
Trigger: Calling pytest.raises(ExceptionGroup[ValueError]) or RaisesExc(ExceptionGroup[TypeError]). Hits the else branch at raises.py:441 when get_origin returns a BaseExceptionGroup subclass but get_args()[0] is not Exception/BaseException/Any.
Common situations: Developer assumes the generic parameter filters nested exception types (it does not); migrating from except* syntax expecting per-type narrowing; copy-paste from type annotations into raises.
Related errors
- Expected an exception type or a tuple of exception types, bu
- {func!r} object (type: {type(func)}) must be callable
- Expected {expected}, but got {exc.__name__!r}
- Expected {expected}, but got an exception instance: {type(ex
- You must specify at least one parameter to match on.
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/be424c7d64acc5bb.json.
Report an issue: GitHub.