From 774f0c44e63077efb2a7cf35b19b99a255b22332 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 9 Mar 2024 09:11:33 +0200 Subject: [PATCH] fixtures: only call `instance` property once in function No need to compute the property multiple times. --- src/_pytest/fixtures.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 58b515434..1eca68207 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1101,17 +1101,18 @@ def resolve_fixture_function( # The fixture function needs to be bound to the actual # request.instance so that code working with "fixturedef" behaves # as expected. - if request.instance is not None: + instance = request.instance + if instance is not None: # Handle the case where fixture is defined not in a test class, but some other class # (for example a plugin class with a fixture), see #2270. if hasattr(fixturefunc, "__self__") and not isinstance( - request.instance, + instance, fixturefunc.__self__.__class__, # type: ignore[union-attr] ): return fixturefunc fixturefunc = getimfunc(fixturedef.func) if fixturefunc != fixturedef.func: - fixturefunc = fixturefunc.__get__(request.instance) # type: ignore[union-attr] + fixturefunc = fixturefunc.__get__(instance) # type: ignore[union-attr] return fixturefunc