Definition fixture scope and a generic ItemDefinition node (#3926) - #14805
Draft
RonnyPfannschmidt wants to merge 2 commits into
Draft
Definition fixture scope and a generic ItemDefinition node (#3926)#14805RonnyPfannschmidt wants to merge 2 commits into
RonnyPfannschmidt wants to merge 2 commits into
Conversation
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>
This was referenced Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
FunctionDefinitioncollection node in two directions: afixture scope that targets that node, and a collector-agnostic base so
non-Python test definitions can be parametrized through the same hook.
1.
Scope.DefinitionSits between
FunctionandClass, anchored on the definition node, so onefixture instance is shared by all invocations generated from a single
definition:
The scope needs the definition to actually be in the tree, i.e.
collect_function_definition = pedantic. Without it, both a definition-scopedfixture and
parametrize(scope="definition")fail with an explicitScopeUnavailablemessage rather than silently degrading to function scope —there is nothing to fall back to that preserves the meaning of the scope.
--setup-showspells itD. The scope-to-indentation mapping is now writtenout explicitly so that adding a scope does not shift the output of all the
existing ones.
2.
nodes.ItemDefinitionThe collector-agnostic counterpart of
FunctionDefinition: a collector standingfor one test definition, whose
collect()runs the generate-tests protocol andturns each callspec into an item.
Any ordinary
pytest_generate_testsimplementation now applies to it, and thechosen values arrive on
item.callspec.params. Ids,pytest.param(...)marksand 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 drivesreordering 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.rstwrites up the four concrete couplings that stand in the way (
TopRequestassuming a Python item, signature-driven
getfixtureinfo(),DirectParamFixtureDefdesugaring,Function.setup()) and a three-step path.3. Layering
The parametrization core moves out of
python.pyinto a new_pytest.parametrize:CallSpec,IdMakerand the newParametrizeContext.Metafuncis now the Python-specific subclass of the latter, overriding fourseams (
_infer_scope,_validate_argnames,_register_direct_params,_introspection_target) to bring in the fixture closure, signature validationand direct-parameter desugaring.
nodesandfixturesboth import the newmodule; no import cycles.
_pytest.python.CallSpec2keeps warning and resolvingas before.
Two bugs found while testing, fixed here
pytest test_x.py::test_a[1]reports "no match" under
pedantic/messy: the argument matcher compares thelast 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.
INTERNALERRORonassert line is not Noneinreports.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 itsown; 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_SCOPESnow includesDefinition, soreorder_itemsrecurses one extralevel on every run (O(n) per collection). Left as-is rather than special-cased.
get_param_argkeysgained ascoped_definition_idkey field and now readsitem.clsdefensively, since non-Python items can carry a callspec.testing/test_definition_scope.pyandtesting/test_item_definition.py(the latter drives a real non-Pythoncollector end to end), plus the selection matrix in
testing/test_collect_function_definition.py.🤖 Generated with Claude Code