Skip to content

Commit d742b38

Browse files
committed
provide missing location parameter, and add type annotations to the hookspec
1 parent 125b663 commit d742b38

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/_pytest/hookspec.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from _pytest.compat import TYPE_CHECKING
1212

1313
if TYPE_CHECKING:
14+
import warnings
1415
from _pytest.config import Config
1516
from _pytest.main import Session
1617
from _pytest.reports import BaseReport
@@ -653,7 +654,12 @@ def pytest_warning_captured(warning_message, when, item, location):
653654

654655

655656
@hookspec(historic=True)
656-
def pytest_warning_recorded(warning_message, when, nodeid, location):
657+
def pytest_warning_recorded(
658+
warning_message: "warnings.WarningMessage",
659+
when: str,
660+
nodeid: str,
661+
location: Tuple[str, int, str],
662+
):
657663
"""
658664
Process a warning captured by the internal pytest warnings plugin.
659665

src/_pytest/warnings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ def catch_warnings_for_item(config, ihook, when, item):
115115
kwargs=dict(warning_message=warning_message, when=when, item=item)
116116
)
117117
ihook.pytest_warning_recorded.call_historic(
118-
kwargs=dict(warning_message=warning_message, nodeid=nodeid, when=when)
118+
kwargs=dict(
119+
warning_message=warning_message,
120+
nodeid=nodeid,
121+
when=when,
122+
location=None,
123+
)
119124
)
120125

121126

testing/test_warnings.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import warnings
34

45
import pytest
@@ -268,20 +269,49 @@ def test_func(fix):
268269
collected = []
269270

270271
class WarningCollector:
271-
def pytest_warning_recorded(self, warning_message, when, nodeid):
272-
collected.append((str(warning_message.message), when, nodeid))
272+
def pytest_warning_recorded(self, warning_message, when, nodeid, location):
273+
collected.append((str(warning_message.message), when, nodeid, location))
273274

274275
result = testdir.runpytest(plugins=[WarningCollector()])
275276
result.stdout.fnmatch_lines(["*1 passed*"])
276277

277278
expected = [
278-
("config warning", "config", ""),
279-
("collect warning", "collect", ""),
280-
("setup warning", "runtest", "test_warning_captured_hook.py::test_func"),
281-
("call warning", "runtest", "test_warning_captured_hook.py::test_func"),
282-
("teardown warning", "runtest", "test_warning_captured_hook.py::test_func"),
279+
(
280+
"config warning",
281+
"config",
282+
"",
283+
(
284+
r"/tmp/pytest-of-.+/pytest-\d+/test_warning_captured_hook0/conftest.py",
285+
3,
286+
"pytest_configure",
287+
),
288+
),
289+
("collect warning", "collect", "", None),
290+
("setup warning", "runtest", "test_warning_captured_hook.py::test_func", None),
291+
("call warning", "runtest", "test_warning_captured_hook.py::test_func", None),
292+
(
293+
"teardown warning",
294+
"runtest",
295+
"test_warning_captured_hook.py::test_func",
296+
None,
297+
),
283298
]
284-
assert collected == expected, str(collected)
299+
for index in range(len(expected)):
300+
collected_result = collected[index]
301+
expected_result = expected[index]
302+
303+
assert collected_result[0] == expected_result[0], str(collected)
304+
assert collected_result[1] == expected_result[1], str(collected)
305+
assert collected_result[2] == expected_result[2], str(collected)
306+
307+
if expected_result[3] is not None:
308+
assert re.match(expected_result[3][0], collected_result[3][0]), str(
309+
collected
310+
)
311+
assert collected_result[3][1] == expected_result[3][1], str(collected)
312+
assert collected_result[3][2] == expected_result[3][2], str(collected)
313+
else:
314+
assert expected_result[3] == collected_result[3], str(collected)
285315

286316

287317
@pytest.mark.filterwarnings("always")

0 commit comments

Comments
 (0)