Skip to content

Generic ItemDefinition parametrization protocol (#3926) - #14858

Draft
RonnyPfannschmidt wants to merge 5 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:feature/generic-item-definition
Draft

Generic ItemDefinition parametrization protocol (#3926)#14858
RonnyPfannschmidt wants to merge 5 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:feature/generic-item-definition

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Aug 11, 2026

Copy link
Copy Markdown
Member

Note

Stack: #14855 -> #14856 -> #14769 -> #14857 -> #14858. Top of the chain.
Review only the last commit.

Gives nodes.ItemDefinition the collect() protocol, so a non-Python collector
can have its test definitions parametrized through pytest_generate_tests:

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)
        )

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.

ParametrizeContext moves into _pytest.parametrize as the collector-agnostic
base 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.rst writes up the four couplings in
the way and a three-step path.

Requires #14855: test_ids_and_marks_from_pytest_param hits the skip-location
INTERNALERROR without it. Relevant if the chain is reordered.

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
RonnyPfannschmidt force-pushed the feature/generic-item-definition branch from a465315 to c945ff4 Compare August 11, 2026 18:54
RonnyPfannschmidt and others added 4 commits August 11, 2026 21:25
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
RonnyPfannschmidt force-pushed the feature/generic-item-definition branch from c945ff4 to 94ecddc Compare August 11, 2026 19:25
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