@@ -855,6 +855,7 @@ def __init__(
855855 self .argnames = getfuncargnames (func , name = argname , is_method = unittest )
856856 self .unittest = unittest
857857 self .ids = ids
858+ self .cached_result = None
858859 self ._finalizers = []
859860
860861 def addfinalizer (self , finalizer ):
@@ -881,8 +882,7 @@ def finish(self, request):
881882 # the cached fixture value and remove
882883 # all finalizers because they may be bound methods which will
883884 # keep instances alive
884- if hasattr (self , "cached_result" ):
885- del self .cached_result
885+ self .cached_result = None
886886 self ._finalizers = []
887887
888888 def execute (self , request ):
@@ -894,9 +894,8 @@ def execute(self, request):
894894 fixturedef .addfinalizer (functools .partial (self .finish , request = request ))
895895
896896 my_cache_key = self .cache_key (request )
897- cached_result = getattr (self , "cached_result" , None )
898- if cached_result is not None :
899- result , cache_key , err = cached_result
897+ if self .cached_result is not None :
898+ result , cache_key , err = self .cached_result
900899 # note: comparison with `==` can fail (or be expensive) for e.g.
901900 # numpy arrays (#6497)
902901 if my_cache_key is cache_key :
@@ -908,7 +907,7 @@ def execute(self, request):
908907 # we have a previous but differently parametrized fixture instance
909908 # so we need to tear it down before creating a new one
910909 self .finish (request )
911- assert not hasattr ( self , " cached_result" )
910+ assert self . cached_result is None
912911
913912 hook = self ._fixturemanager .session .gethookproxy (request .node .fspath )
914913 return hook .pytest_fixture_setup (fixturedef = self , request = request )
@@ -953,6 +952,7 @@ def pytest_fixture_setup(fixturedef, request):
953952 kwargs = {}
954953 for argname in fixturedef .argnames :
955954 fixdef = request ._get_active_fixturedef (argname )
955+ assert fixdef .cached_result is not None
956956 result , arg_cache_key , exc = fixdef .cached_result
957957 request ._check_scope (argname , request .scope , fixdef .scope )
958958 kwargs [argname ] = result
0 commit comments