Skip to content

Commit

Permalink
fixtures: inline _getnextfixturedef into _get_active_fixturedef
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech authored and nicoddemus committed Apr 27, 2024
1 parent 3c77aec commit acf2971
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,41 +410,6 @@ def node(self):
"""Underlying collection node (depends on current request scope)."""
raise NotImplementedError()

def _getnextfixturedef(self, argname: str) -> "FixtureDef[Any]":
fixturedefs = self._arg2fixturedefs.get(argname, None)
if fixturedefs is None:
# We arrive here because of a dynamic call to
# getfixturevalue(argname) usage which was naturally
# not known at parsing/collection time.
fixturedefs = self._fixturemanager.getfixturedefs(argname, self._pyfuncitem)
if fixturedefs is not None:
self._arg2fixturedefs[argname] = fixturedefs
# No fixtures defined with this name.
if fixturedefs is None:
raise FixtureLookupError(argname, self)
# The are no fixtures with this name applicable for the function.
if not fixturedefs:
raise FixtureLookupError(argname, self)

# A fixture may override another fixture with the same name, e.g. a
# fixture in a module can override a fixture in a conftest, a fixture in
# a class can override a fixture in the module, and so on.
# An overriding fixture can request its own name (possibly indirectly);
# in this case it gets the value of the fixture it overrides, one level
# up.
# Check how many `argname`s deep we are, and take the next one.
# `fixturedefs` is sorted from furthest to closest, so use negative
# indexing to go in reverse.
index = -1
for request in self._iter_chain():
if request.fixturename == argname:
index -= 1
# If already consumed all of the available levels, fail.
if -index > len(fixturedefs):
raise FixtureLookupError(argname, self)

return fixturedefs[index]

@property
def config(self) -> Config:
"""The pytest config object associated with this request."""
Expand Down Expand Up @@ -580,7 +545,40 @@ def _get_active_fixturedef(
self._check_scope(fixturedef, fixturedef._scope)
return fixturedef

fixturedef = self._getnextfixturedef(argname)
# Find the appropriate fixturedef.
fixturedefs = self._arg2fixturedefs.get(argname, None)
if fixturedefs is None:
# We arrive here because of a dynamic call to
# getfixturevalue(argname) which was naturally
# not known at parsing/collection time.
fixturedefs = self._fixturemanager.getfixturedefs(argname, self._pyfuncitem)
if fixturedefs is not None:
self._arg2fixturedefs[argname] = fixturedefs
# No fixtures defined with this name.
if fixturedefs is None:
raise FixtureLookupError(argname, self)
# The are no fixtures with this name applicable for the function.
if not fixturedefs:
raise FixtureLookupError(argname, self)

# A fixture may override another fixture with the same name, e.g. a
# fixture in a module can override a fixture in a conftest, a fixture in
# a class can override a fixture in the module, and so on.
# An overriding fixture can request its own name (possibly indirectly);
# in this case it gets the value of the fixture it overrides, one level
# up.
# Check how many `argname`s deep we are, and take the next one.
# `fixturedefs` is sorted from furthest to closest, so use negative
# indexing to go in reverse.
index = -1
for request in self._iter_chain():
if request.fixturename == argname:
index -= 1
# If already consumed all of the available levels, fail.
if -index > len(fixturedefs):
raise FixtureLookupError(argname, self)

fixturedef = fixturedefs[index]

# Prepare a SubRequest object for calling the fixture.
funcitem = self._pyfuncitem
Expand Down

0 comments on commit acf2971

Please sign in to comment.