Merge pull request #10343 from RonnyPfannschmidt/fix-10342-warn-explicit-add-location
fix #10342: put location into warning exceptions
This commit is contained in:
commit
24ef7c98e8
|
@ -158,7 +158,7 @@ def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
|
||||||
filename = inspect.getfile(method)
|
filename = inspect.getfile(method)
|
||||||
module = method.__module__
|
module = method.__module__
|
||||||
mod_globals = method.__globals__
|
mod_globals = method.__globals__
|
||||||
|
try:
|
||||||
warnings.warn_explicit(
|
warnings.warn_explicit(
|
||||||
message,
|
message,
|
||||||
type(message),
|
type(message),
|
||||||
|
@ -167,3 +167,6 @@ def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
|
||||||
registry=mod_globals.setdefault("__warningregistry__", {}),
|
registry=mod_globals.setdefault("__warningregistry__", {}),
|
||||||
lineno=lineno,
|
lineno=lineno,
|
||||||
)
|
)
|
||||||
|
except Warning as w:
|
||||||
|
# If warnings are errors (e.g. -Werror), location information gets lost, so we add it to the message.
|
||||||
|
raise type(w)(f"{w}\n at {filename}:{lineno}") from None
|
||||||
|
|
|
@ -36,3 +36,11 @@ def test_pytest_warnings_repr_integration_test(pytester: Pytester) -> None:
|
||||||
)
|
)
|
||||||
result = pytester.runpytest()
|
result = pytester.runpytest()
|
||||||
result.stdout.fnmatch_lines(["E pytest.PytestWarning: some warning"])
|
result.stdout.fnmatch_lines(["E pytest.PytestWarning: some warning"])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.filterwarnings("error")
|
||||||
|
def test_warn_explicit_for_annotates_errors_with_location():
|
||||||
|
with pytest.raises(Warning, match="(?m)test\n at .*python_api.py:\\d+"):
|
||||||
|
warning_types.warn_explicit_for(
|
||||||
|
pytest.raises, warning_types.PytestWarning("test") # type: ignore
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue