From 686f9e0720da7d50ea94fe54dbea85da50e300af Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 27 Feb 2024 17:49:35 +0200 Subject: [PATCH] fixtures: remove unneeded optimization from `_getnextfixturedef` According to my understanding, this code, which handles obtaining the relevant fixturedefs when a dynamic `getfixturevalue` is used, has an optimization where it only grabs fixturedefs that are visible to the *parent* of the item, instead of the item itself, under the assumption that a fixturedef can't be visible to a single item, only to a collector. Remove this optimization for the following reasons: - It doesn't save much (one loop iteration in `matchfactories`) - It slightly complicates the complex fixtures code - If some plugin wants to make a fixture visible only to a single item, why not let it? - In the static case (`getfixtureclosure`), this optimization is not done (despite the confusing name `parentnode`, it is *not* the parent node). This is inconsistent. --- src/_pytest/fixtures.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 99de03fe8..0a505d65a 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -413,9 +413,7 @@ class FixtureRequest(abc.ABC): # We arrive here because of a dynamic call to # getfixturevalue(argname) usage which was naturally # not known at parsing/collection time. - parent = self._pyfuncitem.parent - assert parent is not None - fixturedefs = self._fixturemanager.getfixturedefs(argname, parent) + fixturedefs = self._fixturemanager.getfixturedefs(argname, self._pyfuncitem) if fixturedefs is not None: self._arg2fixturedefs[argname] = fixturedefs # No fixtures defined with this name.