Skip to content

Commit f0fd818

Browse files
committed
Replace == with is for comparison of cache keys
Closes #6497
1 parent 85df6bb commit f0fd818

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Guido Wesdorp
112112
Guoqiang Zhang
113113
Harald Armin Massa
114114
Henk-Jaap Wagenaar
115+
Holger Kohr
115116
Hugo van Kemenade
116117
Hui Wang (coldnight)
117118
Ian Bicking

changelog/6497.bugfix.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix bug in the comparison of request key with cached key in fixture.
2+
3+
A construct ``if key == cached_key:`` can fail either because ``==`` is explicitly disallowed, or for, e.g., NumPy arrays, where the result of ``a == b`` cannot generally be converted to `bool`.
4+
The implemented fix replaces `==` with ``is``.
5+

src/_pytest/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,9 @@ def execute(self, request):
894894
cached_result = getattr(self, "cached_result", None)
895895
if cached_result is not None:
896896
result, cache_key, err = cached_result
897-
if my_cache_key == cache_key:
897+
# note: comparison with `==` can fail (or be expensive) for e.g.
898+
# numpy arrays
899+
if my_cache_key is cache_key:
898900
if err is not None:
899901
_, val, tb = err
900902
raise val.with_traceback(tb)

0 commit comments

Comments
 (0)