Backtestspytestrun-20261106-234554#15052
fixtures: --fixtures-per-test has an off-by-one on item's lineno
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Root cause: `getlocation` returns `co_firstlineno + 1`. `co_firstlineno` is already 1-based and points at the first decorator, so the `+1` only produced the `def` line by accident for single-decorator fixtures and is one too high for undecorated test items (the 357 vs 356 in the issue). Both patches fix the item header line, and the three independent header tests pass for both. Patch B simply drops the `+1`. That fixes the item line but shifts every decorated fixture's reported location from its `def` line to the `@pytest.fixture` decorator line. The issue quotes `monkeypatch -- src/_pytest/monkeypatch.py:36` as the correct companion line and only objects to 357; the independent test that checks the fixture line stays at its def fails on B (it now prints 35). B's own test edits confirm the regression: `arg1 -- test_fixtures_in_module.py:6` becomes `:5`, and the 'Requested fixture ... defined in' and 'request is a reserved word' messages also move to the decorator line. That is a user-visible behaviour change beyond the ask, not justified by the issue. B does add a changelog entry, which is a plus for craft. Patch A replaces the accidental `+1` with a real lookup of the `def` line via `inspect.getsourcelines` + `ast`, with fallbacks to `co_firstlineno` on OSError/TypeError/SyntaxError/TokenError, an `if True:` wrapper to cope with indented methods whose docstrings dedent, and name matching on `co_name`. This yields correct def-line locations for undecorated items, single-decorator fixtures (preserving existing output), stacked decorators, methods, async methods, and falls back sanely for lambdas. It adds targeted tests for each of those cases in test_compat.py, show_fixtures_per_test.py and fixtures.py, and the independent fixture-location test passes. The approach is heavier than a one-line change, and it lacks a changelog, but the extra code is directly needed to fix the item line without regressing fixture lines, and it is guarded at every edge. A is the better fix.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
9m 10s
Wall-clock time in the container
Full test suite
No new failures
938 passed · 10 skipped · 948 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
179vs 91 merged
Changed lines, ours vs merged
The issue
View on GitHubIn the --fixtures-per-test output e.g. this part:
---- fixtures used by test_callspec2_renamed ----
---- (testing/deprecated_test.py:357) ---
monkeypatch -- src/_pytest/monkeypatch.py:36
A convenient fixture for monkey-patching.
the 357 should be 356.
The two patches
src/_pytest/compat.pyindex 93aec228c..b826aeb38 100644@@ -3,6 +3,7 @@ from __future__ import annotations +import ast from collections.abc import Callable import enum import functools@@ -12,6 +13,7 @@ from inspect import Signature import os from pathlib import Path import sys+import tokenize from typing import Any from typing import Final from typing import NoReturn@@ -72,18 +74,55 @@ def signature(obj: Callable[..., Any]) -> Signature: return inspect.signature(obj) +def _get_def_lineno(function) -> int:+ """Return the 1-based line number of the ``def`` statement of a function.++ ``co_firstlineno`` points at the first decorator for decorated functions,+ so the source is parsed to find the line of the ``def`` itself. Falls back+ to ``co_firstlineno`` if the source is not available or cannot be parsed.+ """+ firstlineno: int = function.__code__.co_firstlineno+ try:+ lines, start = inspect.getsourcelines(function)+ except (OSError, TypeError, SyntaxError, tokenize.TokenError):+ return firstlineno+ source = "".join(lines)+ # Methods and nested functions are indented; wrap them in a block instead+ # of dedenting, which would choke on less-indented multi-line strings.+ header_lines = 0+ if source[:1].isspace():+ source = "if True:\n" + source+ header_lines = 1+ try:+ tree = ast.parse(source)+ except SyntaxError:+ return firstlineno+ body = tree.body+ if header_lines:+ if not (body and isinstance(body[0], ast.If)):+ return firstlineno+ body = body[0].body+ for node in body:+ if (+ isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))+ and node.name == function.__code__.co_name+ ):+ return start + node.lineno - 1 - header_lines+ return firstlineno++ def getlocation(function, curdir: str | os.PathLike[str] | None = None) -> str: function = get_real_func(function) fn = Path(inspect.getfile(function))- lineno = function.__code__.co_firstlineno+ lineno = _get_def_lineno(function) if curdir is not None: try: relfn = fn.relative_to(curdir) except ValueError: pass else:- return f"{relfn}:{lineno + 1}"- return f"{fn}:{lineno + 1}"+ return f"{relfn}:{lineno}"+ return f"{fn}:{lineno}" def num_mock_patch_args(function) -> int:testing/python/fixtures.pyindex 8e779a93d..193e12bbc 100644@@ -3889,6 +3889,33 @@ class TestShowFixtures: ) result.stdout.no_fnmatch_line("*arg0*") + def test_show_fixtures_location_with_stacked_decorators(+ self, pytester: Pytester+ ) -> None:+ """The location points to the ``def`` line, not to the line after the+ first decorator."""+ p = pytester.makepyfile(+ '''+ import pytest++ def noop(func):+ return func++ @pytest.fixture+ @noop+ def arg1():+ """ hello world """+ '''+ )+ result = pytester.runpytest("--fixtures", p)+ result.stdout.fnmatch_lines(+ """+ *fixtures defined from*+ *arg1 -- test_show_fixtures_location_with_stacked_decorators.py:8*+ *hello world*+ """+ )+ @pytest.mark.parametrize("testmod", [True, False]) def test_show_fixtures_conftest(self, pytester: Pytester, testmod) -> None: pytester.makeconftest(testing/python/show_fixtures_per_test.pyindex 76fc37282..210ff29ae 100644@@ -30,7 +30,7 @@ def test_fixtures_in_module(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_fixtures_in_module.py:9)*",+ "*(test_fixtures_in_module.py:8)*", "arg1 -- test_fixtures_in_module.py:6", " arg1 docstring", ]@@ -69,11 +69,11 @@ def test_fixtures_in_conftest(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg2*",- "*(test_fixtures_in_conftest.py:2)*",+ "*(test_fixtures_in_conftest.py:1)*", "arg2 -- conftest.py:6", " arg2 docstring", "*fixtures used by test_arg3*",- "*(test_fixtures_in_conftest.py:4)*",+ "*(test_fixtures_in_conftest.py:3)*", "arg1 -- conftest.py:3", " arg1 docstring", "arg2 -- conftest.py:6",@@ -112,7 +112,7 @@ def test_should_show_fixtures_used_by_test(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_args*",- "*(test_should_show_fixtures_used_by_test.py:6)*",+ "*(test_should_show_fixtures_used_by_test.py:5)*", "arg1 -- test_should_show_fixtures_used_by_test.py:3", " arg1 from testmodule", "arg2 -- conftest.py:6",@@ -149,7 +149,7 @@ def test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_args*",- "*(test_verbose_include_private_fixtures_and_loc.py:6)*",+ "*(test_verbose_include_private_fixtures_and_loc.py:5)*", "_arg1 -- conftest.py:3", " _arg1 from conftest", "arg2 -- conftest.py:6",@@ -209,7 +209,7 @@ def test_multiline_docstring_in_module(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_multiline_docstring_in_module.py:13)*",+ "*(test_multiline_docstring_in_module.py:12)*", "arg1 -- test_multiline_docstring_in_module.py:3", " Docstring content that spans across multiple lines,", " through second line,",@@ -243,7 +243,7 @@ def test_verbose_include_multiline_docstring(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_verbose_include_multiline_docstring.py:13)*",+ "*(test_verbose_include_multiline_docstring.py:12)*", "arg1 -- test_verbose_include_multiline_docstring.py:3", " Docstring content that spans across multiple lines,", " through second line,",@@ -309,11 +309,11 @@ def test_should_show_parametrized_fixtures_used_by_test(pytester: Pytester) -> N result.stdout.fnmatch_lines( [ "*fixtures used by test_directly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:13)*", "directly -- test_should_show_parametrized_fixtures_used_by_test.py:4", " parametrized fixture", "*fixtures used by test_directly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:13)*", "directly -- test_should_show_parametrized_fixtures_used_by_test.py:4", " parametrized fixture", "*fixtures used by test_indirectly_parametrized_fixture*",@@ -326,3 +326,59 @@ def test_should_show_parametrized_fixtures_used_by_test(pytester: Pytester) -> N " indirectly parametrized fixture", ] )+++def test_item_and_fixture_locations_point_to_def_line(pytester: Pytester) -> None:+ """The reported locations are the lines of the ``def`` statements,+ regardless of how many decorators precede them."""+ pytester.makepyfile(+ '''+ import pytest++ def noop(func):+ return func++ @pytest.fixture+ @noop+ def stacked():+ """fixture with stacked decorators"""++ def test_plain(stacked):+ pass++ @pytest.mark.usefixtures("stacked")+ @pytest.mark.parametrize(+ "x", [1],+ )+ def test_decorated(stacked, x):+ pass++ class TestClass:+ def test_method(self, stacked):+ """Docstring+ with a less indented line."""++ @pytest.mark.usefixtures("stacked")+ async def test_async_method(self, stacked):+ pass+ '''+ )+ result = pytester.runpytest("--fixtures-per-test")+ assert result.ret == 0++ result.stdout.fnmatch_lines(+ [+ "*fixtures used by test_plain*",+ "*(test_item_and_fixture_locations_point_to_def_line.py:11)*",+ "stacked -- test_item_and_fixture_locations_point_to_def_line.py:8",+ "*fixtures used by test_decorated*",+ "*(test_item_and_fixture_locations_point_to_def_line.py:18)*",+ "stacked -- test_item_and_fixture_locations_point_to_def_line.py:8",+ "*fixtures used by test_method*",+ "*(test_item_and_fixture_locations_point_to_def_line.py:22)*",+ "stacked -- test_item_and_fixture_locations_point_to_def_line.py:8",+ "*fixtures used by test_async_method*",+ "*(test_item_and_fixture_locations_point_to_def_line.py:27)*",+ "stacked -- test_item_and_fixture_locations_point_to_def_line.py:8",+ ]+ )testing/test_compat.pyindex 2c86f06c9..c011d6fd7 100644@@ -5,15 +5,18 @@ import enum from functools import cached_property from functools import partial from functools import wraps+import importlib from typing import Literal import warnings from _pytest.compat import assert_never from _pytest.compat import deprecated from _pytest.compat import get_real_func+from _pytest.compat import getlocation from _pytest.compat import safe_getattr from _pytest.compat import safe_isclass from _pytest.outcomes import OutcomeException+from _pytest.pytester import Pytester import pytest @@ -202,3 +205,33 @@ def test_deprecated() -> None: with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) assert old_way() == "human intelligence" # type: ignore[deprecated]+++def test_getlocation_points_to_def_line(pytester: Pytester) -> None:+ mod = pytester.makepyfile(+ test_mod="""+ import functools++ def plain():+ pass++ @functools.lru_cache+ @functools.lru_cache+ def decorated():+ pass++ class C:+ def method(self):+ '''Docstring+ with a less indented line.'''++ func = lambda: None+ """+ )+ pytester.syspathinsert()+ module = importlib.import_module("test_mod")+ assert getlocation(module.plain, mod.parent) == "test_mod.py:3"+ assert getlocation(module.decorated, mod.parent) == "test_mod.py:8"+ assert getlocation(module.C.method, mod.parent) == "test_mod.py:12"+ # Not a def statement: falls back to the code object's first line.+ assert getlocation(module.func, mod.parent) == "test_mod.py:16"
changelog/15052.bugfix.rstnew file mode 100644index 000000000..89954ff3e@@ -0,0 +1 @@+Fixed possible off-by-one line numbers in :option:`--fixtures` and :option:`--fixtures-per-test` output.src/_pytest/compat.pyindex 93aec228c..0e060f15d 100644@@ -82,8 +82,8 @@ def getlocation(function, curdir: str | os.PathLike[str] | None = None) -> str: except ValueError: pass else:- return f"{relfn}:{lineno + 1}"- return f"{fn}:{lineno + 1}"+ return f"{relfn}:{lineno}"+ return f"{fn}:{lineno}" def num_mock_patch_args(function) -> int:testing/python/fixtures.pyindex 8e779a93d..bc7b5a40c 100644@@ -3883,7 +3883,7 @@ class TestShowFixtures: """ *tmp_path -- * *fixtures defined from*- *arg1 -- test_show_fixtures_testmodule.py:6*+ *arg1 -- test_show_fixtures_testmodule.py:5* *hello world* """ )@@ -3943,10 +3943,10 @@ class TestShowFixtures: textwrap.dedent( """\ * fixtures defined from test_show_fixtures_trimmed_doc *- arg2 -- test_show_fixtures_trimmed_doc.py:10+ arg1 -- test_show_fixtures_trimmed_doc.py:2 line1 line2- arg1 -- test_show_fixtures_trimmed_doc.py:3+ arg2 -- test_show_fixtures_trimmed_doc.py:9 line1 line2 """@@ -3972,7 +3972,7 @@ class TestShowFixtures: textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_doc *- fixture1 -- test_show_fixtures_indented_doc.py:3+ fixture1 -- test_show_fixtures_indented_doc.py:2 line1 indented line """@@ -4000,7 +4000,7 @@ class TestShowFixtures: textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *- fixture1 -- test_show_fixtures_indented_doc_first_line_unindented.py:3+ fixture1 -- test_show_fixtures_indented_doc_first_line_unindented.py:2 line1 line2 indented line@@ -4028,7 +4028,7 @@ class TestShowFixtures: textwrap.dedent( """\ * fixtures defined from test_show_fixtures_indented_in_class *- fixture1 -- test_show_fixtures_indented_in_class.py:4+ fixture1 -- test_show_fixtures_indented_in_class.py:3 line1 line2 indented line@@ -4068,11 +4068,11 @@ class TestShowFixtures: result.stdout.fnmatch_lines( """ * fixtures defined from test_a *- fix_a -- test_a.py:4+ fix_a -- test_a.py:3 Fixture A * fixtures defined from test_b *- fix_b -- test_b.py:4+ fix_b -- test_b.py:3 Fixture B """ )@@ -4108,11 +4108,11 @@ class TestShowFixtures: result.stdout.fnmatch_lines( """ * fixtures defined from conftest *- arg1 -- conftest.py:3+ arg1 -- conftest.py:2 Hello World in conftest.py * fixtures defined from test_show_fixtures_with_same_name *- arg1 -- test_show_fixtures_with_same_name.py:3+ arg1 -- test_show_fixtures_with_same_name.py:2 Hi from test module """ )@@ -4305,7 +4305,7 @@ class TestParameterizedSubRequest: "The requested fixture has no parameter defined for test:", " test_call_from_fixture.py::test_foo", "Requested fixture 'fix_with_param' defined in:",- "test_call_from_fixture.py:4",+ "test_call_from_fixture.py:3", "Requested here:", "test_call_from_fixture.py:9", "*1 error in*",@@ -4331,7 +4331,7 @@ class TestParameterizedSubRequest: "The requested fixture has no parameter defined for test:", " test_call_from_test.py::test_foo", "Requested fixture 'fix_with_param' defined in:",- "test_call_from_test.py:4",+ "test_call_from_test.py:3", "Requested here:", "test_call_from_test.py:8", "*1 failed*",@@ -4362,7 +4362,7 @@ class TestParameterizedSubRequest: " test_external_fixture.py::test_foo", "", "Requested fixture 'fix_with_param' defined in:",- "conftest.py:4",+ "conftest.py:3", "Requested here:", "test_external_fixture.py:2", "*1 failed*",@@ -4408,7 +4408,7 @@ class TestParameterizedSubRequest: " test_foos.py::test_foo", "", "Requested fixture 'fix_with_param' defined in:",- f"{fixfile}:4",+ f"{fixfile}:3", "Requested here:", "test_foos.py:4", "*1 failed*",@@ -4425,7 +4425,7 @@ class TestParameterizedSubRequest: " test_foos.py::test_foo", "", "Requested fixture 'fix_with_param' defined in:",- f"{fixfile}:4",+ f"{fixfile}:3", "Requested here:", f"{testfile}:4", "*1 failed*",@@ -5278,7 +5278,7 @@ def test_fixture_named_request(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*'request' is a reserved word for fixtures, use another name:",- " *test_fixture_named_request.py:8",+ " *test_fixture_named_request.py:7", ] ) testing/python/show_fixtures_per_test.pyindex 76fc37282..2362847f3 100644@@ -30,8 +30,8 @@ def test_fixtures_in_module(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_fixtures_in_module.py:9)*",- "arg1 -- test_fixtures_in_module.py:6",+ "*(test_fixtures_in_module.py:8)*",+ "arg1 -- test_fixtures_in_module.py:5", " arg1 docstring", ] )@@ -69,16 +69,16 @@ def test_fixtures_in_conftest(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg2*",- "*(test_fixtures_in_conftest.py:2)*",- "arg2 -- conftest.py:6",+ "*(test_fixtures_in_conftest.py:1)*",+ "arg2 -- conftest.py:5", " arg2 docstring", "*fixtures used by test_arg3*",- "*(test_fixtures_in_conftest.py:4)*",- "arg1 -- conftest.py:3",+ "*(test_fixtures_in_conftest.py:3)*",+ "arg1 -- conftest.py:2", " arg1 docstring",- "arg2 -- conftest.py:6",+ "arg2 -- conftest.py:5", " arg2 docstring",- "arg3 -- conftest.py:9",+ "arg3 -- conftest.py:8", " arg3", ] )@@ -112,10 +112,10 @@ def test_should_show_fixtures_used_by_test(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_args*",- "*(test_should_show_fixtures_used_by_test.py:6)*",- "arg1 -- test_should_show_fixtures_used_by_test.py:3",+ "*(test_should_show_fixtures_used_by_test.py:5)*",+ "arg1 -- test_should_show_fixtures_used_by_test.py:2", " arg1 from testmodule",- "arg2 -- conftest.py:6",+ "arg2 -- conftest.py:5", " arg2 from conftest", ] )@@ -149,12 +149,12 @@ def test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_args*",- "*(test_verbose_include_private_fixtures_and_loc.py:6)*",- "_arg1 -- conftest.py:3",+ "*(test_verbose_include_private_fixtures_and_loc.py:5)*",+ "_arg1 -- conftest.py:2", " _arg1 from conftest",- "arg2 -- conftest.py:6",+ "arg2 -- conftest.py:5", " arg2 from conftest",- "arg3 -- test_verbose_include_private_fixtures_and_loc.py:3",+ "arg3 -- test_verbose_include_private_fixtures_and_loc.py:2", " arg3 from testmodule", ] )@@ -209,8 +209,8 @@ def test_multiline_docstring_in_module(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_multiline_docstring_in_module.py:13)*",- "arg1 -- test_multiline_docstring_in_module.py:3",+ "*(test_multiline_docstring_in_module.py:12)*",+ "arg1 -- test_multiline_docstring_in_module.py:2", " Docstring content that spans across multiple lines,", " through second line,", " and through third line.",@@ -243,8 +243,8 @@ def test_verbose_include_multiline_docstring(pytester: Pytester) -> None: result.stdout.fnmatch_lines( [ "*fixtures used by test_arg1*",- "*(test_verbose_include_multiline_docstring.py:13)*",- "arg1 -- test_verbose_include_multiline_docstring.py:3",+ "*(test_verbose_include_multiline_docstring.py:12)*",+ "arg1 -- test_verbose_include_multiline_docstring.py:2", " Docstring content that spans across multiple lines,", " through second line,", " and through third line.",@@ -309,20 +309,20 @@ def test_should_show_parametrized_fixtures_used_by_test(pytester: Pytester) -> N result.stdout.fnmatch_lines( [ "*fixtures used by test_directly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",- "directly -- test_should_show_parametrized_fixtures_used_by_test.py:4",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:13)*",+ "directly -- test_should_show_parametrized_fixtures_used_by_test.py:3", " parametrized fixture", "*fixtures used by test_directly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:14)*",- "directly -- test_should_show_parametrized_fixtures_used_by_test.py:4",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:13)*",+ "directly -- test_should_show_parametrized_fixtures_used_by_test.py:3", " parametrized fixture", "*fixtures used by test_indirectly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:17)*",- "indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:16)*",+ "indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:8", " indirectly parametrized fixture", "*fixtures used by test_indirectly_parametrized_fixture*",- "*(test_should_show_parametrized_fixtures_used_by_test.py:17)*",- "indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:9",+ "*(test_should_show_parametrized_fixtures_used_by_test.py:16)*",+ "indirectly -- test_should_show_parametrized_fixtures_used_by_test.py:8", " indirectly parametrized fixture", ] )