Skip to content

Commit 55e07ae

Browse files
committed
Hash and compare cells by identity
1 parent 2b33b52 commit 55e07ae

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

Lib/annotationlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ def __eq__(self, other):
279279
# because dictionaries are not hashable.
280280
and self.__globals__ is other.__globals__
281281
and self.__forward_is_class__ == other.__forward_is_class__
282-
and self.__cell__ == other.__cell__
282+
# Two separate cells are always considered unequal in forward refs.
283+
and self.__cell__ is other.__cell__
283284
and self.__owner__ == other.__owner__
284285
and (
285286
(tuple(sorted(self.__extra_names__.items())) if self.__extra_names__ else None) ==
@@ -293,7 +294,10 @@ def __hash__(self):
293294
self.__forward_module__,
294295
id(self.__globals__), # dictionaries are not hashable, so hash by identity
295296
self.__forward_is_class__,
296-
tuple(sorted(self.__cell__.items())) if isinstance(self.__cell__, dict) else self.__cell__,
297+
( # cells are mutable and not hashable as well
298+
tuple(sorted([(name, id(cell)) for name, cell in self.__cell__.items()]))
299+
if isinstance(self.__cell__, dict) else id(self.__cell__),
300+
),
297301
self.__owner__,
298302
tuple(sorted(self.__extra_names__.items())) if self.__extra_names__ else None,
299303
))

0 commit comments

Comments
 (0)