Skip to content

Commit c83c1c4

Browse files
committed
fixtures: add _iter_chain helper method
Will be reused in the next commit.
1 parent 98b008f commit c83c1c4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/_pytest/fixtures.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ def getfixturevalue(self, argname: str) -> Any:
540540
)
541541
return fixturedef.cached_result[0]
542542

543+
def _iter_chain(self) -> Iterator["SubRequest"]:
544+
"""Yield all SubRequests in the chain, from self up.
545+
546+
Note: does *not* yield the TopRequest.
547+
"""
548+
current = self
549+
while isinstance(current, SubRequest):
550+
yield current
551+
current = current._parent_request
552+
543553
def _get_active_fixturedef(
544554
self, argname: str
545555
) -> Union["FixtureDef[object]", PseudoFixtureDef[object]]:
@@ -557,11 +567,7 @@ def _get_active_fixturedef(
557567
return fixturedef
558568

559569
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
560-
current = self
561-
values: List[FixtureDef[Any]] = []
562-
while isinstance(current, SubRequest):
563-
values.append(current._fixturedef) # type: ignore[has-type]
564-
current = current._parent_request
570+
values = [request._fixturedef for request in self._iter_chain()]
565571
values.reverse()
566572
return values
567573

@@ -705,7 +711,7 @@ def __init__(
705711
)
706712
self._parent_request: Final[FixtureRequest] = request
707713
self._scope_field: Final = scope
708-
self._fixturedef: Final = fixturedef
714+
self._fixturedef: Final[FixtureDef[object]] = fixturedef
709715
if param is not NOTSET:
710716
self.param = param
711717
self.param_index: Final = param_index

0 commit comments

Comments
 (0)