{"record":{"id":"54136c0ed6022be9","repo":"pytest-dev/pytest","slug":"unexpected-keyword-arguments-passed-to-pytest-warn","errorCode":null,"errorMessage":"Unexpected keyword arguments passed to pytest.warns: {argnames}\nUse context-manager form instead?","messagePattern":"Unexpected keyword arguments passed to pytest\\.warns: (.+?)\nUse context-manager form instead\\?","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/_pytest/recwarn.py","lineNumber":163,"sourceCode":"        Failed: Regex pattern did not match any of the 1 warnings emitted.\n         Regex: ...\n         Emitted warnings: ...UserWarning...\n\n    **Using with** ``pytest.mark.parametrize``\n\n    When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests\n    such that some runs raise a warning and others do not.\n\n    This could be achieved in the same way as with exceptions, see\n    :ref:`parametrizing_conditional_raising` for an example.\n\n    \"\"\"\n    __tracebackhide__ = True\n    if func is None and not args:\n        match: str | re.Pattern[str] | None = kwargs.pop(\"match\", None)\n        if kwargs:\n            argnames = \", \".join(sorted(kwargs))\n            raise TypeError(\n                f\"Unexpected keyword arguments passed to pytest.warns: {argnames}\"\n                \"\\nUse context-manager form instead?\"\n            )\n        return WarningsChecker(expected_warning, match_expr=match, _ispytest=True)\n    else:\n        if not callable(func):\n            raise TypeError(f\"{func!r} object (type: {type(func)}) must be callable\")\n        with WarningsChecker(expected_warning, _ispytest=True):\n            return func(*args, **kwargs)\n\n\nclass WarningsRecorder(warnings.catch_warnings):\n    \"\"\"A context manager to record raised warnings.\n\n    Each recorded warning is an instance of :class:`warnings.WarningMessage`.\n\n    Adapted from `warnings.catch_warnings`.\n","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/recwarn.py#L145-L181","documentation":"Raised by pytest.warns when the context-manager form is used but unrecognized keyword arguments are passed. Only match= is consumed from kwargs in that form; anything else is almost certainly a typo or a misplaced parameter meant for the function form. The error lists the sorted offending names and reminds the user to use the context-manager form correctly.","triggerScenarios":"pytest.warns(UserWarning, match='x', foo=1); pytest.warns(UserWarning, message='x'); any kwarg other than match when func is None and args is empty. The `if kwargs:` branch after popping match fires.","commonSituations":"Typing message= instead of match=; passing category kwargs that belong to warnings.warn; copy-paste from filterwarnings parameter names.","solutions":["Use the supported match= keyword for regex matching: pytest.warns(UserWarning, match='x').","Drop the unrecognized kwargs if they were accidental.","If you intended the function form, pass a callable as the second positional argument."],"exampleFix":"// before\nwith pytest.warns(UserWarning, message='deprecated'):\n    ...\n// after\nwith pytest.warns(UserWarning, match='deprecated'):\n    ...","handlingStrategy":"validation","validationCode":"allowed = {'match'}\nbad = set(kwargs) - allowed\nassert not bad, f'pytest.warns context-manager form only accepts {allowed}; got {sorted(bad)}'","typeGuard":"def warns_kwargs_valid(kwargs) -> bool:\n    return set(kwargs) <= {'match'}","tryCatchPattern":null,"preventionTips":["In context-manager form, only match= is accepted by pytest.warns.","Use message= only with warnings.warn, not pytest.warns.","Run a quick smoke test of warns helpers after refactor."],"tags":["pytest","warns","kwargs","api-misuse","validation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}