Skip to content

Add the "definition" fixture scope (#3926) - #14857

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

Add the "definition" fixture scope (#3926)#14857
RonnyPfannschmidt wants to merge 4 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:feature/definition-fixture-scope

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Aug 11, 2026

Copy link
Copy Markdown
Member

Note

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

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

Needs the definition to 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.

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

Part of #3926.

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/definition-fixture-scope branch from b32ac42 to d50c3e8 Compare August 11, 2026 18:54
RonnyPfannschmidt and others added 3 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.
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the feature/definition-fixture-scope branch from d50c3e8 to 0e39ef1 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