From 530be285751143febe54b8974b234eed5eb8b079 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 14 Mar 2024 20:24:25 +0200 Subject: [PATCH] fixtures: use early return in `_get_active_fixturedef` --- src/_pytest/fixtures.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 656160ff7..17d6ddc7f 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -570,18 +570,21 @@ class FixtureRequest(abc.ABC): self, argname: str ) -> Union["FixtureDef[object]", PseudoFixtureDef[object]]: fixturedef = self._fixture_defs.get(argname) - if fixturedef is None: - try: - fixturedef = self._getnextfixturedef(argname) - except FixtureLookupError: - if argname == "request": - cached_result = (self, [0], None) - return PseudoFixtureDef(cached_result, Scope.Function) - raise - self._compute_fixture_value(fixturedef) - self._fixture_defs[argname] = fixturedef - else: + if fixturedef is not None: self._check_scope(fixturedef, fixturedef._scope) + return fixturedef + + try: + fixturedef = self._getnextfixturedef(argname) + except FixtureLookupError: + if argname == "request": + cached_result = (self, [0], None) + return PseudoFixtureDef(cached_result, Scope.Function) + raise + + self._compute_fixture_value(fixturedef) + + self._fixture_defs[argname] = fixturedef return fixturedef def _get_fixturestack(self) -> List["FixtureDef[Any]"]: