Add the "definition" fixture scope (#3926) - #14857
Draft
RonnyPfannschmidt wants to merge 4 commits into
Draft
Conversation
RonnyPfannschmidt
force-pushed
the
feature/definition-fixture-scope
branch
2 times, most recently
from
August 11, 2026 12:46
ac65d86 to
b04e98c
Compare
RonnyPfannschmidt
force-pushed
the
feature/definition-fixture-scope
branch
from
August 11, 2026 18:34
b04e98c to
6fcbd3f
Compare
This was referenced Aug 11, 2026
RonnyPfannschmidt
force-pushed
the
feature/definition-fixture-scope
branch
from
August 11, 2026 18:39
6fcbd3f to
b32ac42
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/definition-fixture-scope
branch
from
August 11, 2026 18:54
b32ac42 to
d50c3e8
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.
RonnyPfannschmidt
force-pushed
the
feature/definition-fixture-scope
branch
from
August 11, 2026 19:25
d50c3e8 to
0e39ef1
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. Review only the last commit.
Scope.Definitionsits betweenFunctionandClass, anchored on thedefinition node, so one fixture instance is shared by all invocations generated
from a single definition:
Needs the definition to 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.--setup-showspells itD. The scope-to-indentation mapping is now spelledout so adding a scope does not shift the output of the existing ones.
Part of #3926.