From f5de111357d26780760df78b503fdc8f4731f611 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 8 Mar 2024 23:18:43 +0200 Subject: [PATCH] fixtures: check scope mismatch in `getfixturevalue` already-cached case This makes sure the scope is always compatible, and also allows using `getfixturevalue` in `pytest_fixture_setup` so less internal magic. --- src/_pytest/fixtures.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 40568a4a4..2d3593df0 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -573,6 +573,8 @@ class FixtureRequest(abc.ABC): raise self._compute_fixture_value(fixturedef) self._fixture_defs[argname] = fixturedef + else: + self._check_scope(fixturedef, fixturedef._scope) return fixturedef def _get_fixturestack(self) -> List["FixtureDef[Any]"]: @@ -1121,11 +1123,7 @@ def pytest_fixture_setup( """Execution of fixture setup.""" kwargs = {} for argname in fixturedef.argnames: - fixdef = request._get_active_fixturedef(argname) - assert fixdef.cached_result is not None - result, arg_cache_key, exc = fixdef.cached_result - request._check_scope(fixdef, fixdef._scope) - kwargs[argname] = result + kwargs[argname] = request.getfixturevalue(argname) fixturefunc = resolve_fixture_function(fixturedef, request) my_cache_key = fixturedef.cache_key(request)