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.
This commit is contained in:
Ran Benita 2024-03-08 23:18:43 +02:00
parent 71671f60b5
commit f5de111357
1 changed files with 3 additions and 5 deletions

View File

@ -573,6 +573,8 @@ class FixtureRequest(abc.ABC):
raise raise
self._compute_fixture_value(fixturedef) self._compute_fixture_value(fixturedef)
self._fixture_defs[argname] = fixturedef self._fixture_defs[argname] = fixturedef
else:
self._check_scope(fixturedef, fixturedef._scope)
return fixturedef return fixturedef
def _get_fixturestack(self) -> List["FixtureDef[Any]"]: def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
@ -1121,11 +1123,7 @@ def pytest_fixture_setup(
"""Execution of fixture setup.""" """Execution of fixture setup."""
kwargs = {} kwargs = {}
for argname in fixturedef.argnames: for argname in fixturedef.argnames:
fixdef = request._get_active_fixturedef(argname) kwargs[argname] = request.getfixturevalue(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
fixturefunc = resolve_fixture_function(fixturedef, request) fixturefunc = resolve_fixture_function(fixturedef, request)
my_cache_key = fixturedef.cache_key(request) my_cache_key = fixturedef.cache_key(request)