Generic ItemDefinition parametrization protocol (#3926) - #14858
Draft
RonnyPfannschmidt wants to merge 5 commits into
Draft
Generic ItemDefinition parametrization protocol (#3926)#14858RonnyPfannschmidt wants to merge 5 commits into
RonnyPfannschmidt wants to merge 5 commits into
Conversation
RonnyPfannschmidt
force-pushed
the
feature/generic-item-definition
branch
2 times, most recently
from
August 11, 2026 18:34
a7b20fd to
9cecde6
Compare
This was referenced Aug 11, 2026
RonnyPfannschmidt
force-pushed
the
feature/generic-item-definition
branch
from
August 11, 2026 18:39
9cecde6 to
a465315
Compare
TestReport.from_item_and_call asserted reportinfo() yields a line number. Items which cannot point at a line -- non-Python items legitimately report None -- crashed with an INTERNALERROR when skipped via a marker. Report the location without a line instead.
RonnyPfannschmidt
force-pushed
the
feature/generic-item-definition
branch
from
August 11, 2026 18:54
a465315 to
c945ff4
Compare
Move IdMaker, CallSpec and _ascii_escaped_by_config out of python.py into a new module, together with the long-str id strategy literals they own. python.py imports them back, so _pytest.python.CallSpec and the CallSpec2 alias keep resolving as before. Pure move, no behaviour change. The two CollectError call sites go through a small _collect_error() helper which imports nodes lazily, so nodes stays free to import this module later.
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>
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 -- across the parameter sets of one parametrized test, but not between two different tests. 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. --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 the existing ones.
) Give nodes.ItemDefinition the collect() protocol so a non-Python collector can have its test definitions parametrized through pytest_generate_tests: declare the accepted names in parametrize_argnames, build one item per parameter set in make_item(), and the chosen values arrive on item.callspec.params. 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. ParametrizeContext moves into _pytest.parametrize as the collector agnostic base of Metafunc, which overrides four seams to bring in the fixture closure, signature validation and direct-parameter desugaring. No fixture resolution on the non-Python path; the four couplings in the way are written up in doc/en/proposals/generic_fixture_closures.rst.
RonnyPfannschmidt
force-pushed
the
feature/generic-item-definition
branch
from
August 11, 2026 19:25
c945ff4 to
94ecddc
Compare
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
Stack: #14855 -> #14856 -> #14769 -> #14857 -> #14858. Top of the chain.
Review only the last commit.
Gives
nodes.ItemDefinitionthecollect()protocol, so a non-Python collectorcan have its test definitions parametrized through
pytest_generate_tests:Ids,
pytest.param(...)marks and node-id selection work as they do for Pythontests. 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.
ParametrizeContextmoves into_pytest.parametrizeas the collector-agnosticbase of
Metafunc, which overrides four seams (_infer_scope,_validate_argnames,_register_direct_params,_introspection_target).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 couplings inthe way and a three-step path.
Requires #14855:
test_ids_and_marks_from_pytest_paramhits the skip-locationINTERNALERRORwithout it. Relevant if the chain is reordered.