Skip to content

Definition fixture scope and a generic ItemDefinition node (#3926) - #14805

Draft
RonnyPfannschmidt wants to merge 2 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:feature/definition-scope
Draft

Definition fixture scope and a generic ItemDefinition node (#3926)#14805
RonnyPfannschmidt wants to merge 2 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:feature/definition-scope

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Note

Stacked on #14769 — that PR's commit is included here. Review only the second
commit, or diff against RonnyPfannschmidt:feature/function-definition-collection-node.

Builds on the opt-in FunctionDefinition collection node in two directions: a
fixture scope that targets that node, and a collector-agnostic base so
non-Python test definitions can be parametrized through the same hook.

1. Scope.Definition

Sits between Function and Class, anchored on the definition node, so one
fixture instance is shared by all invocations generated from a single
definition:

@pytest.fixture(scope="definition")
def resource(): ...

@pytest.mark.parametrize("n", [1, 2, 3])
def test_it(resource, n):
    ...  # one `resource` for all three parameter sets
SETUP    D resource
  test_x.py::test_it[1] .
  test_x.py::test_it[2] .
  test_x.py::test_it[3] .
TEARDOWN D resource

The scope needs the definition to actually be in the tree, i.e.
collect_function_definition = pedantic. Without it, both a definition-scoped
fixture and parametrize(scope="definition") fail with an explicit
ScopeUnavailable message rather than silently degrading to function scope —
there is nothing to fall back to that preserves the meaning of the scope.

--setup-show spells it D. The scope-to-indentation mapping is now written
out explicitly so that adding a scope does not shift the output of all the
existing ones.

2. nodes.ItemDefinition

The collector-agnostic counterpart of FunctionDefinition: a collector standing
for one test definition, whose collect() runs the generate-tests protocol and
turns each callspec into an item.

class YamlDefinition(nodes.ItemDefinition):
    parametrize_argnames = ("value",)

    def make_item(self, parent, *, name, callspec, nodeid, context):
        return YamlItem.from_parent(
            parent, name=name, nodeid=nodeid, spec=dict(callspec.params)
        )

Any ordinary pytest_generate_tests implementation now applies to it, and the
chosen values arrive on item.callspec.params. Ids, pytest.param(...) marks
and node-id selection work as they do for Python tests.

Attaching the callspec, its marks and its id to the generated item is done by
the protocol rather than left to each make_item() — the callspec drives
reordering and high-scoped param caching, so it is not optional.

Not included: fixture resolution for non-Python items — no indirect=True,
no scope inference, no requesting fixtures. doc/en/proposals/generic_fixture_closures.rst
writes up the four concrete couplings that stand in the way (TopRequest
assuming a Python item, signature-driven getfixtureinfo(),
DirectParamFixtureDef desugaring, Function.setup()) and a three-step path.

3. Layering

The parametrization core moves out of python.py into a new
_pytest.parametrize: CallSpec, IdMaker and the new ParametrizeContext.
Metafunc is now the Python-specific subclass of the latter, overriding four
seams (_infer_scope, _validate_argnames, _register_direct_params,
_introspection_target) to bring in the fixture closure, signature validation
and direct-parameter desugaring. nodes and fixtures both import the new
module; no import cycles. _pytest.python.CallSpec2 keeps warning and resolving
as before.

Two bugs found while testing, fixed here

  • Node-id selection is broken by Opt-in FunctionDefinition collection tree node (#3926) #14769 as it stands. pytest test_x.py::test_a[1]
    reports "no match" under pedantic/messy: the argument matcher compares the
    last part against the definition's bare name, and the parametrization lives on
    the items below it. Fixed by looking through the definition node; regression
    test covers 6 selector shapes × 3 modes.
  • Skipping a non-Python item without a line number crashes pytest with an
    INTERNALERROR on assert line is not None in reports.py. Long-standing,
    but trivially reachable now that a non-Python definition can carry
    pytest.param(..., marks=pytest.mark.skip(...)). This one has no issue of its
    own; it is filed under the Opt-in FunctionDefinition collection tree node (#3926) #14769 changelog entry rather than under an invented
    number.

Notes for review

  • HIGH_SCOPES now includes Definition, so reorder_items recurses one extra
    level on every run (O(n) per collection). Left as-is rather than special-cased.
  • get_param_argkeys gained a scoped_definition_id key field and now reads
    item.cls defensively, since non-Python items can carry a callspec.
  • 24 new tests in testing/test_definition_scope.py and
    testing/test_item_definition.py (the latter drives a real non-Python
    collector end to end), plus the selection matrix in
    testing/test_collect_function_definition.py.

🤖 Generated with Claude Code

RonnyPfannschmidt and others added 2 commits July 30, 2026 11:43
Introduce the ``collect_function_definition`` ini option to promote the
internal ``FunctionDefinition`` from a transient parametrization helper to
a real collector node in the collection tree.

The option is tri-state:

- ``hidden`` (default): unchanged flat layout; the definition drives
  parametrization and is kept out of the tree.
- ``pedantic``: insert the definition node between the Module/Class and its
  test functions; function-level markers are scoped to the definition and
  each invocation owns only its callspec markers.
- ``messy``: migration stopgap -- insert the node but transfer the
  function-level markers back onto each invocation to preserve the legacy
  marker layout for code not yet prepared for the new scope; emits a header
  warning.

FunctionDefinition becomes a PyCollector (no longer a Function/Item), so it
is collected rather than run. Invocation node ids stay flat and identical
across all modes, so selection, caching and reporting are unaffected. obj
and instance resolution and getmodpath skip an interposed definition node.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…3926)

Build on the opt-in FunctionDefinition collection node in two directions:
a fixture scope which targets that node, and a collector-agnostic base so
non-Python test definitions can be parametrized through the same hook.

Scope.Definition sits between Function and Class and is anchored on the
definition node, so one fixture instance is shared by all invocations
generated from a single definition:

    @pytest.fixture(scope="definition")
    def resource(): ...

    @pytest.mark.parametrize("n", [1, 2, 3])
    def test_it(resource, n): ...

It requires the definition to actually be in the tree, i.e.
collect_function_definition=pedantic. Without it, both a definition-scoped
fixture and parametrize(scope="definition") fail with an explicit message
rather than silently degrading to function scope. --setup-show spells the
scope "D" and keeps the existing indentation for every other scope.

nodes.ItemDefinition is the generic counterpart of FunctionDefinition: a
collector standing for one test definition, whose collect() runs the
generate-tests protocol and turns each callspec into an item. A non-Python
collector declares parametrize_argnames and implements make_item(); the
chosen values arrive on item.callspec.params, with ids, pytest.param marks
and nodeid selection working as they do for Python tests. There is no
fixture resolution on that path -- doc/en/proposals/generic_fixture_closures
writes up what that would take.

The parametrization core moves out of python.py into the new
_pytest.parametrize module: CallSpec, IdMaker and the new
ParametrizeContext, of which Metafunc is now the Python-specific subclass
adding the fixture closure, signature validation and direct-param desugaring.
_pytest.python.CallSpec2 keeps warning and resolving as before.

Two bugs fixed along the way, both reachable only once a definition node is
in the tree: node-id selection of a parametrized test looked at the
definition's bare name and failed to match, and skipping an item whose
reportinfo() has no line number crashed with an INTERNALERROR.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant