Opt-in FunctionDefinition collection tree node (#3926) - #14769
Opt-in FunctionDefinition collection tree node (#3926)#14769RonnyPfannschmidt wants to merge 1 commit 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>
7fe152c to
d011e35
Compare
|
Heads-up on a defect in this PR as it currently stands: selecting a test by node id is broken in the tree modes. $ cat pytest.ini
[pytest]
collect_function_definition = pedantic
$ pytest -q "test_x.py::test_a[1]"
ERROR: not found: test_x.py::test_a
(no match in any of [<Module test_x.py>])Works under
holds for the node id strings, but not for selection: The minimal fix is to let the matcher look through an Worth noting the existing comment right at that spot already anticipates this: # A non-parameterized arg matches all parametrizations (if any).
# TODO: Remove the hacky split once the collection structure
# contains parametrization.
is_match = node.name.split("[")[0] == matchparts[0]This PR is exactly what makes the collection structure contain the parametrization, so that |
Closes #3926.
Promotes the internal
FunctionDefinitionfrom a transient parametrizationhelper to a real collector node in the collection tree, behind an opt-in ini
option.
collect_function_definition(tri-state string, defaulthidden)hidden(default)Function(legacy)pedanticmessymessyis a migration stopgap for code that is not yet prepared for thedefinition scope — it reproduces the old duplicated-marker layout so bad code
keeps working. The target is
pedantic.What changes
FunctionDefinitionbecomes aPyCollector(no longer aFunction/Item), soit is collected rather than run. Its
collect()runspytest_generate_testsand yields the invocations.
(
test_mod.py::test_x[0]), so-k, caching,--lf, xdist and reporting areunaffected.
obj/instanceresolution andgetmodpathtransparently skip an interposeddefinition node.
iter_markersyields each mark once).The default (
hidden) path is behavior-preserving; the whole test suite passesunchanged.
Known warts / follow-up TODOs
These are intentionally left as follow-ups (inline
TODO(#3926)where relevant),to keep this PR focused — happy to split into a tracking issue:
getfixtureinfoon a collector. The fixture closure must becomputed before the invocations exist, so
FunctionDefinition._generate_functionscalls the item-scoped
getfixtureinfoon the collector via acast. Needs anode-level entry point for closure computation. (
TODO(#3926)inline.)messymode is temporary. It exists only to unblock migration and shouldbe removed once consumers move to the definition scope. Marked clearly in the
docs and header warning.
node and its sole
Functionchild share a node id. Accepted for now; thestructured-nodeids work will give these a better data structure.
originalnamecould now become a read-only property returning thedefinition's name (see the existing note in
Function.__init__).🤖 Generated with Claude Code