{"record":{"id":"330548c3671ec7dd","repo":"pytest-dev/pytest","slug":"expected-expected-but-got-exc-name-r","errorCode":null,"errorMessage":"Expected {expected}, but got {exc.__name__!r}","messagePattern":"Expected (.+?), but got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/raises.py","lineNumber":451,"sourceCode":"            ) or (\n                issubclass(origin_exc, BaseExceptionGroup)\n                and exc_type in (BaseException, Any)\n            ):\n                if not issubclass(origin_exc, ExceptionGroup):\n                    self.is_baseexception = True\n                return cast(type[BaseExcT_1], origin_exc)\n            else:\n                raise ValueError(\n                    f\"Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseException]` \"\n                    f\"are accepted as generic types but got `{exc}`. \"\n                    f\"As `raises` will catch all instances of the specified group regardless of the \"\n                    f\"generic argument specific nested exceptions has to be checked \"\n                    f\"with `RaisesGroup`.\"\n                )\n        # unclear if the Type/ValueError distinction is even helpful here\n        msg = f\"Expected {expected}, but got \"\n        if isinstance(exc, type):  # type: ignore[unreachable]\n            raise ValueError(msg + f\"{exc.__name__!r}\")\n        if isinstance(exc, BaseException):  # type: ignore[unreachable]\n            raise TypeError(msg + f\"an exception instance: {type(exc).__name__}\")\n        raise TypeError(msg + repr(type(exc).__name__))\n\n    @property\n    def fail_reason(self) -> str | None:\n        \"\"\"Set after a call to :meth:`matches` to give a human-readable reason for why the match failed.\n        When used as a context manager the string will be printed as the reason for the\n        test failing.\"\"\"\n        return self._fail_reason\n\n    def _check_check(\n        self: AbstractRaises[BaseExcT_1],\n        exception: BaseExcT_1,\n    ) -> bool:\n        if self.check is None:\n            return True\n","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/raises.py#L433-L469","documentation":"Raised by RaisesExc._parse_exc when the exc argument is a class (isinstance(exc, type) is True) but it is neither a BaseException subclass nor a validly-parameterized exception group. It is a defensive ValueError for a path the static type checker marks unreachable — reached only when callers bypass type hints. The message interpolates expected (the human label passed by the caller, e.g. 'a BaseException type') and the offending class name.","triggerScenarios":"Passing a non-exception class such as pytest.raises(int), pytest.raises(dict), or a dataclass type to raises/RaisesExc. Reached only after the earlier BaseException-group branch fails, when the value is a type but issubclass(exc, BaseException) is False.","commonSituations":"Passing a Pydantic/custom error-code class instead of its raised exception type; refactoring that accidentally substitutes an enum or sentinel class for the exception type; dynamically computed exception arguments that lose their exception-ness.","solutions":["Pass the actual exception class that derives from BaseException, e.g. pytest.raises(ValueError).","If the value is a group-bearing generic, use the bare group class or RaisesGroup instead of a constrained generic.","Add a runtime assertion before the call: assert isinstance(exc, type) and issubclass(exc, BaseException)."],"exampleFix":"// before\nwith pytest.raises(MyErrorCode):  # MyErrorCode is an IntEnum\n    ...\n// after\nwith pytest.raises(MyException):\n    ...\n// or guard:\nassert issubclass(exc_target, BaseException)\nwith pytest.raises(exc_target):\n    ...","handlingStrategy":"type-guard","validationCode":"assert isinstance(exc, type) and issubclass(exc, BaseException), f'expected a BaseException type, got {exc!r}'","typeGuard":"def is_exception_type(exc) -> bool:\n    return isinstance(exc, type) and issubclass(exc, BaseException)","tryCatchPattern":null,"preventionTips":["Always pass exception classes derived from BaseException to raises/RaisesExc.","When computing the exception type dynamically, assert it is a BaseException subclass before use.","Run mypy/pyright in strict mode so the type checker catches misuse before runtime."],"tags":["pytest","raises","type-validation","misuse"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}