GeezerVison is showing github.com — Indirect parametrization with scope="function" does not always work · Issue #11575 · pytest-dev/pytest
Indirect parametrization with scope="function" does not always work #11575DescriptionIndirect parametrization with scope="function" (or not specifying scope) does not seem to work when the following two conditions match:
- The previous test's request param scope is larger than
function
- The current test's parametrized request value is identical to the previous test's
import pytest
@pytest.fixture(scope="module")
def fixture(request):
print(f"fixture (scope={request.scope})")
@pytest.mark.parametrize("fixture", ["a", "b"], scope="module", indirect=True)
def test1(fixture):
...
@pytest.mark.parametrize("fixture", ["b", "c"], scope="function", indirect=True)
def test2(fixture):
...
@pytest.mark.parametrize("fixture", ["c", "d"], scope="function", indirect=True)
def test3(fixture):
...
$ pytest -s -v
======================================================= test session starts ========================================================
platform darwin -- Python 3.9.18, pytest-7.4.3, pluggy-1.3.0 -- /Users/yugo/.pyenv/versions/3.9.18/envs/test_3.9.18/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/yugo/Desktop/test
collected 6 items
test_something.py::test1[a] fixture (scope=module)
PASSED
test_something.py::test1[b] fixture (scope=module)
PASSED
test_something.py::test2[b] PASSED
test_something.py::test2[c] fixture (scope=function)
PASSED
test_something.py::test3[c] fixture (scope=function)
PASSED
test_something.py::test3[d] fixture (scope=function)
PASSED
Note that scope="function" is not working in the test test_something.py::test2[b] that matches the above condition.
$ pip list
Package Version
-------------- -------
exceptiongroup 1.1.3
iniconfig 2.0.0
packaging 23.2
pip 23.0.1
pluggy 1.3.0
pytest 7.4.3
setuptools 58.1.0
tomli 2.0.1
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.7
BuildVersion: 19H1217
ActivityRonnyPfannschmidt commented on Nov 1, 2023 the real bug is that we don't directly fail if the passed scope differs from the fixture scope yugokato commented on Nov 1, 2023
the real bug is that we don't directly fail if the passed scope differs from the fixture scope
API doc linked from pytest.mark.parametrize says It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. for scope param. I took it as the scope will be overwritten by the passed value (in my above example code, function). Is that not true here? RonnyPfannschmidt commented on Oct 9, 2026 🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
To follow up on my earlier comment: the root problem is that parametrize(..., indirect=True, scope=...) accepts a scope that is narrower than the fixture's own scope.
- A param scope that is wider than the fixture scope is fine. Reordering groups the tests by param, so the fixture is set up once per wider scope (Parametrization with Variables Unexpectedly Changes Fixture Scope from Class-Level to Function-Level #12146 was this case; the uneven behaviour there came from the
is comparison of cache keys, which fix: improved caching of parameterized fixtures #12600 replaced with ==).
- A param scope that is narrower than the fixture scope has no correct cheap answer. Today pytest returns the wider-scoped value to the narrower request, which is wrong data. The only correct behaviour is to tear down the wider-scoped value and set up a new one, so a module-scoped fixture can be set up several times within one module whenever narrower requests are interleaved. Reordering cannot avoid that.
Plan: #15112 fixes the wrong data by tearing down and recreating. In the same change, a narrower param scope gets a deprecation warning that explains the extra setup/teardown, and in a future major release it becomes an error.
Generated by Claude Code
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|