Files
RustPython/extra_tests/snippets/stdlib_warnings_keywords.py
sean abreau 5d15791e11 Accept keyword arguments in warnings.warn_explicit (#8814)
Allow the four required arguments to be passed by position or keyword, matching CPython and fixing pytest Node.warn calls. Add regression coverage for mixed calls, warning instances, and invalid argument binding.

Codex implemented the fix and regression tests, ran validation, and drafted this commit and the PR description. The contributor manually verified the warning reproducer.

Assisted-by: Codex:GPT-6
2026-09-25 09:45:19 +09:00

31 lines
1.1 KiB
Python

import warnings
from testutils import assert_raises
names = ("message", "category", "filename", "lineno")
for message, category in (("probe", UserWarning), (UserWarning("probe"), None)):
values = (message, category, "probe.py", 7)
for positional_count in range(5):
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
warnings.warn_explicit(
*values[:positional_count],
**dict(zip(names[positional_count:], values[positional_count:])),
)
assert len(caught) == 1
warning = caught[0]
assert str(warning.message) == "probe"
assert warning.category is UserWarning
assert warning.filename == "probe.py"
assert warning.lineno == 7
arguments = dict(zip(names, ("probe", UserWarning, "probe.py", 7)))
for name in names:
missing = arguments.copy()
del missing[name]
with assert_raises(TypeError):
warnings.warn_explicit(**missing)
with assert_raises(TypeError):
warnings.warn_explicit("probe", **arguments)