Skip to content

Commit f005758

Browse files
committed
Identity comparsion first
1 parent 3670225 commit f005758

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/_pytest/fixtures.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,12 +1057,14 @@ def execute(self, request: SubRequest) -> FixtureValue:
10571057
if self.cached_result is not None:
10581058
cache_key = self.cached_result[1]
10591059

1060-
# Coerce the comparison into a bool (#12600), and if that fails, fall back to an identity check:
1061-
# `__eq__` is not required to return a bool, and sometimes doesn't, e.g., numpy arrays (#6497).
1062-
try:
1063-
cache_hit = bool(my_cache_key == cache_key)
1064-
except (ValueError, RuntimeError):
1065-
cache_hit = my_cache_key is cache_key
1060+
# First attempt to use 'is' for performance reasons (for example numpy arrays (#6497)).
1061+
cache_hit = my_cache_key is cache_key
1062+
if not cache_hit:
1063+
# If they are not the same, fallback to a bool comparison (#12600).
1064+
try:
1065+
cache_hit = bool(my_cache_key == cache_key)
1066+
except (ValueError, RuntimeError):
1067+
cache_hit = False
10661068

10671069
if cache_hit:
10681070
if self.cached_result[2] is not None:

0 commit comments

Comments
 (0)