{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/raises.py#L244-L280","documentation":"Raised by pytest.raises when called in the legacy function-call form (pytest.raises(Exc, func, ...)) and unexpected keyword arguments are passed. The function form only accepts 'match', 'check', and 'expected_exception' as kwargs; anything else triggers this TypeError with a hint to use the context-manager form. The context-manager form (with pytest.raises(Exc) as ...) is the recommended modern usage.","triggerScenarios":"Calling pytest.raises(ValueError, func, foo=bar) where 'foo' is not a recognized kwarg; passing test assertions or extra args meant for the body as kwargs to raises.","commonSituations":"Mixing up the context-manager and function-call APIs; passing extra arguments that belong to the callable but using the wrong calling convention; old code relying on undocumented kwargs.","solutions":["Switch to the context-manager form: 'with pytest.raises(ValueError, match=...) as exc_info: func()'.","If using the function form, pass only recognized kwargs (match, check) and pass the callable's own args positionally inside the body.","Remove unrecognized keyword arguments."],"exampleFix":"// before\npytest.raises(ValueError, my_func, unknown=True)\n// after\nwith pytest.raises(ValueError, match=\"bad\") as exc_info:\n    my_func(unknown=True)","handlingStrategy":"validation","validationCode":"ALLOWED = {\"match\", \"check\", \"expected_exception\"}\nbad = set(kwargs) - ALLOWED\nif func is None and not args and bad:\n    raise TypeError(f\"unsupported kwargs: {bad}; use context-manager form\")\npytest.raises(ValueError, func, **{k: kwargs[k] for k in kwargs if k in ALLOWED})","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the context-manager form: 'with pytest.raises(Exc) as exc_info:'.","Pass only 'match'/'check' as kwargs to raises.","Pass the callable's own arguments inside the with-block."],"tags":["pytest-raises","api-misuse","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}