Skip to content

Commit

Permalink
fix: allow caching of parameterized fixtures
Browse files Browse the repository at this point in the history
The fix for Issue pytest-dev#6541 caused regression where cache hits became
cache misses, unexpectedly.  Attempt to restore the previous behavior,
while also retaining the fix for the bug.

Fixes: Issue pytest-dev#6962
  • Loading branch information
nisimond committed Jul 11, 2024
1 parent 16cdacc commit 5a97246
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,16 @@ def execute(self, request: SubRequest) -> FixtureValue:
my_cache_key = self.cache_key(request)
if self.cached_result is not None:
cache_key = self.cached_result[1]
# note: comparison with `==` can fail (or be expensive) for e.g.
# numpy arrays (#6497).
if my_cache_key is cache_key:

# note: `__eq__` is not required to return a bool, and sometimes
# doesn't, e.g., numpy arrays (#6497). Coerce the comparison
# into a bool, and if that fails, fall back to an identity check.
try:
cache_hit = bool(my_cache_key == cache_key)
except (ValueError, RuntimeError):
cache_hit = my_cache_key is cache_key

if cache_hit:
if self.cached_result[2] is not None:
exc, exc_tb = self.cached_result[2]
raise exc.with_traceback(exc_tb)
Expand Down

0 comments on commit 5a97246

Please sign in to comment.