{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/recwarn.py#L145-L181","documentation":"In context-manager form, pytest.warns accepts only match= as a keyword argument. Any other keyword argument is rejected because there is no function to apply them to.","triggerScenarios":"Calling pytest.warns(UserWarning, foo='bar') or pytest.warns(UserWarning, match='x', extra=1). Hits recwarn.py:163 when kwargs remain after popping match.","commonSituations":"Typo in a kwarg name (e.g. 'metch'); passing function arguments via kwargs intended for the function-call form; passing a deprecated/removed parameter.","solutions":["Use only the match= keyword in context-manager form; remove unknown kwargs.","If you meant to pass arguments to a function, use the function-call form: pytest.warns(UserWarning, my_func, arg1, arg2).","Check the pytest version docs — the parameter may have been renamed or removed."],"exampleFix":"// before\npytest.warns(UserWarning, match='x', messege='y')\n// after\npytest.warns(UserWarning, match='x')","handlingStrategy":"validation","validationCode":"allowed = {'match'}\nbad = set(kwargs) - allowed\nif bad:\n    raise TypeError(f'unexpected kwargs for context-manager form: {sorted(bad)}')\npytest.warns(UserWarning, match=kwargs.get('match'))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["In context-manager form, only match= is accepted as a kwarg.","To pass arguments to a function, use the function-call form: pytest.warns(Cat, func, *args).","Double-check kwarg spelling against the current pytest version's API."],"tags":["pytest","warns","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}