Skip to content

Commit 4dba104

Browse files
committed
fix: allow caching of parameterized fixtures
The fix for Issue #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 #6962
1 parent 16cdacc commit 4dba104

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Nicholas Devenish
306306
Nicholas Murphy
307307
Niclas Olofsson
308308
Nicolas Delaby
309+
Nicolas Simonds
309310
Nico Vidal
310311
Nikolay Kondratyev
311312
Nipunn Koorapati

changelog/6962.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow caching of parameterized fixtures

src/_pytest/fixtures.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,16 @@ def execute(self, request: SubRequest) -> FixtureValue:
10561056
my_cache_key = self.cache_key(request)
10571057
if self.cached_result is not None:
10581058
cache_key = self.cached_result[1]
1059-
# note: comparison with `==` can fail (or be expensive) for e.g.
1060-
# numpy arrays (#6497).
1061-
if my_cache_key is cache_key:
1059+
1060+
# note: `__eq__` is not required to return a bool, and sometimes
1061+
# doesn't, e.g., numpy arrays (#6497). Coerce the comparison
1062+
# into a bool, and if that fails, fall back to an identity check.
1063+
try:
1064+
cache_hit = bool(my_cache_key == cache_key)
1065+
except (ValueError, RuntimeError):
1066+
cache_hit = my_cache_key is cache_key
1067+
1068+
if cache_hit:
10621069
if self.cached_result[2] is not None:
10631070
exc, exc_tb = self.cached_result[2]
10641071
raise exc.with_traceback(exc_tb)

0 commit comments

Comments
 (0)