Skip to content

Commit 18af2c9

Browse files
committed
fixtures: find SubRequest.node eagerly
Currently `SubRequest.node` is a property which finds the node on every access. This is mildly expensive (need to search up the collection tree), and is almost guaranteed to be called several times (in `execute` and `finish`). Since the node can't change, let's find the node in the ctor once and save it.
1 parent d889ca0 commit 18af2c9

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

src/_pytest/fixtures.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -726,22 +726,11 @@ def __init__(
726726
_ispytest=_ispytest,
727727
)
728728
self._parent_request: Final[FixtureRequest] = request
729-
self._scope_field: Final = scope
730729
self._fixturedef: Final[FixtureDef[object]] = fixturedef
731730
if param is not NOTSET:
732731
self.param = param
733732
self.param_index: Final = param_index
734-
735-
def __repr__(self) -> str:
736-
return f"<SubRequest {self.fixturename!r} for {self._pyfuncitem!r}>"
737-
738-
@property
739-
def _scope(self) -> Scope:
740-
return self._scope_field
741-
742-
@property
743-
def node(self):
744-
scope = self._scope
733+
self._scope_field: Final = scope
745734
if scope is Scope.Function:
746735
# This might also be a non-function Item despite its attribute name.
747736
node: nodes.Node | None = self._pyfuncitem
@@ -755,7 +744,18 @@ def node(self):
755744
assert node, (
756745
f'Could not obtain a node for scope "{scope}" for function {self._pyfuncitem!r}'
757746
)
758-
return node
747+
self._node: Final = node
748+
749+
def __repr__(self) -> str:
750+
return f"<SubRequest {self.fixturename!r} for {self._pyfuncitem!r}>"
751+
752+
@property
753+
def _scope(self) -> Scope:
754+
return self._scope_field
755+
756+
@property
757+
def node(self):
758+
return self._node
759759

760760
def _check_scope(
761761
self,

0 commit comments

Comments
 (0)