pytest-dev/pytest · error · TypeError
Unexpected keyword arguments passed to pytest.warns: {argnam
Error message
Unexpected keyword arguments passed to pytest.warns: {argnames}
Use context-manager form instead? What it means
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.
Source
Thrown at src/_pytest/recwarn.py:163
Failed: Regex pattern did not match any of the 1 warnings emitted.
Regex: ...
Emitted warnings: ...UserWarning...
**Using with** ``pytest.mark.parametrize``
When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests
such that some runs raise a warning and others do not.
This could be achieved in the same way as with exceptions, see
:ref:`parametrizing_conditional_raising` for an example.
"""
__tracebackhide__ = True
if func is None and not args:
match: str | re.Pattern[str] | None = kwargs.pop("match", None)
if kwargs:
argnames = ", ".join(sorted(kwargs))
raise TypeError(
f"Unexpected keyword arguments passed to pytest.warns: {argnames}"
"\nUse context-manager form instead?"
)
return WarningsChecker(expected_warning, match_expr=match, _ispytest=True)
else:
if not callable(func):
raise TypeError(f"{func!r} object (type: {type(func)}) must be callable")
with WarningsChecker(expected_warning, _ispytest=True):
return func(*args, **kwargs)
class WarningsRecorder(warnings.catch_warnings):
"""A context manager to record raised warnings.
Each recorded warning is an instance of :class:`warnings.WarningMessage`.
Adapted from `warnings.catch_warnings`.
View on GitHub (pinned to 98b357f69e)
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.
Example fix
// before pytest.warns(UserWarning, match='x', messege='y') // after pytest.warns(UserWarning, match='x')
Defensive patterns
Strategy: validation
Validate before calling
allowed = {'match'}
bad = set(kwargs) - allowed
if bad:
raise TypeError(f'unexpected kwargs for context-manager form: {sorted(bad)}')
pytest.warns(UserWarning, match=kwargs.get('match')) Prevention
- 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.
When it happens
Trigger: Calling pytest.warns(UserWarning, foo='bar') or pytest.warns(UserWarning, match='x', extra=1). Hits recwarn.py:163 when kwargs remain after popping match.
Common situations: Typo in a kwarg name (e.g. 'metch'); passing function arguments via kwargs intended for the function-call form; passing a deprecated/removed parameter.
Related errors
- {func!r} object (type: {type(func)}) must be callable
- exceptions must be derived from Warning, not %s
- Warning must be str or Warning, got {msg!r} (type {type(msg)
- absolute tolerance can't be NaN.
- relative tolerance can't be negative: {relative_tolerance}
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/54136c0ed6022be9.json.
Report an issue: GitHub.