{"record":{"id":"afe1bb0909d5e7de","repo":"pytest-dev/pytest","slug":"unexpected-keyword-arguments-passed-to-pytest-rais","errorCode":null,"errorMessage":"Unexpected keyword arguments passed to pytest.raises: {kwargs}\nUse context-manager form instead?","messagePattern":"Unexpected keyword arguments passed to pytest\\.raises: (.+?)\nUse context-manager form instead\\?","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/raises.py","lineNumber":262,"sourceCode":"        help the Python interpreter speed up its garbage collection.\n\n        Clearing those references breaks a reference cycle\n        (``ExceptionInfo`` --> caught exception --> frame stack raising\n        the exception --> current frame stack --> local variables -->\n        ``ExceptionInfo``) which makes Python keep all objects referenced\n        from that cycle (including all local variables in the current\n        frame) alive until the next cyclic garbage collection run.\n        More detailed information can be found in the official Python\n        documentation for :ref:`the try statement <python:try>`.\n    \"\"\"\n    __tracebackhide__ = True\n\n    if func is None and not args:\n        if set(kwargs) - {\"match\", \"check\", \"expected_exception\"}:\n            msg = \"Unexpected keyword arguments passed to pytest.raises: \"\n            msg += \", \".join(sorted(kwargs))\n            msg += \"\\nUse context-manager form instead?\"\n            raise TypeError(msg)\n\n        if expected_exception is None:\n            return RaisesExc(**kwargs)\n        return RaisesExc(expected_exception, **kwargs)\n\n    if not expected_exception:\n        raise ValueError(\n            f\"Expected an exception type or a tuple of exception types, but got `{expected_exception!r}`. \"\n            f\"Raising exceptions is already understood as failing the test, so you don't need \"\n            f\"any special code to say 'this should never raise an exception'.\"\n        )\n    if not callable(func):\n        raise TypeError(f\"{func!r} object (type: {type(func)}) must be callable\")\n    with RaisesExc(expected_exception) as excinfo:\n        func(*args, **kwargs)\n    try:\n        return excinfo\n    finally:","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/raises.py#L244-L280","documentation":"Thrown by pytest.raises() when called with no positional exception and no func/args, but with keyword arguments outside the allowed set {match, check, expected_exception}. It catches user typos like pytest.raises(message=...) or pytest.raises(exc=...).","triggerScenarios":"Calling pytest.raises(ValueError, msg='...') (msg is not a valid kwarg); pytest.raises(code=...) instead of passing a callable; pre-8.x code using `message` which was removed.","commonSituations":"Upgrading pytest from an old version where `message` was a valid alias for `match`; typos in keyword names; mixing the function form and context-manager form.","solutions":["Use only the allowed kwargs: match, check, expected_exception.","Replace the removed `message` kwarg with `match`.","Use the context-manager form: with pytest.raises(ValueError, match='...'):"],"exampleFix":"// before\nwith pytest.raises(ValueError, message=\"my msg\"):\n    raise ValueError(\"x\")\n// after\nwith pytest.raises(ValueError, match=\"my msg\"):\n    raise ValueError(\"x\")","handlingStrategy":"validation","validationCode":"allowed = {'match', 'check', 'expected_exception'}\nbad = set(kwargs) - allowed\nif bad:\n    raise TypeError(f'pytest.raises: unknown kwargs {sorted(bad)}')\npytest.raises(ValueError, **kwargs)","typeGuard":"def kwargs_are_valid_for_raises(kwargs: dict) -> bool:\n    return set(kwargs) <= {'match', 'check', 'expected_exception'}","tryCatchPattern":null,"preventionTips":["Use only match, check, expected_exception with pytest.raises.","Replace removed `message` kwarg with `match`.","Prefer the context-manager form to avoid kwarg confusion."],"tags":["raises","kwargs","api-misuse","deprecation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}