fixtures: only call `instance` property once in function

No need to compute the property multiple times.
This commit is contained in:
Ran Benita 2024-03-09 09:11:33 +02:00
parent 006058f1f9
commit 774f0c44e6
1 changed files with 4 additions and 3 deletions

View File

@ -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