changeset: 101236:1c6cf4010df3 parent: 101234:3bad4b0f7612 user: Raymond Hettinger date: Thu May 05 11:14:06 2016 +0300 files: Lib/_collections_abc.py Lib/test/test_collections.py Misc/NEWS description: Issue 26915: Add identity checks to the collections ABC __contains__ methods. diff -r 3bad4b0f7612 -r 1c6cf4010df3 Lib/_collections_abc.py --- a/Lib/_collections_abc.py Thu May 05 10:55:45 2016 +0300 +++ b/Lib/_collections_abc.py Thu May 05 11:14:06 2016 +0300 @@ -689,7 +689,7 @@ except KeyError: return False else: - return v == value + return v is value or v == value def __iter__(self): for key in self._mapping: @@ -704,7 +704,8 @@ def __contains__(self, value): for key in self._mapping: - if value == self._mapping[key]: + v = self._mapping[key] + if v is value or v == value: return True return False @@ -839,7 +840,7 @@ def __contains__(self, value): for v in self: - if v == value: + if v is value or v == value: return True return False diff -r 3bad4b0f7612 -r 1c6cf4010df3 Lib/test/test_collections.py --- a/Lib/test/test_collections.py Thu May 05 10:55:45 2016 +0300 +++ b/Lib/test/test_collections.py Thu May 05 11:14:06 2016 +0300 @@ -23,7 +23,7 @@ from collections.abc import Hashable, Iterable, Iterator, Generator, Reversible from collections.abc import Sized, Container, Callable from collections.abc import Set, MutableSet -from collections.abc import Mapping, MutableMapping, KeysView, ItemsView +from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView from collections.abc import Sequence, MutableSequence from collections.abc import ByteString @@ -1074,6 +1074,26 @@ self.assertFalse(ncs > cs) self.assertTrue(ncs >= cs) + def test_issue26915(self): + # Container membership test should check identity first + class CustomEqualObject: + def __eq__(self, other): + return False + class CustomSequence(list): + def __contains__(self, value): + return Sequence.__contains__(self, value) + + nan = float('nan') + obj = CustomEqualObject() + containers = [ + CustomSequence([nan, obj]), + ItemsView({1: nan, 2: obj}), + ValuesView({1: nan, 2: obj}) + ] + for container in containers: + for elem in container: + self.assertIn(elem, container) + def assertSameSet(self, s1, s2): # coerce both to a real set then check equality self.assertSetEqual(set(s1), set(s2)) diff -r 3bad4b0f7612 -r 1c6cf4010df3 Misc/NEWS --- a/Misc/NEWS Thu May 05 10:55:45 2016 +0300 +++ b/Misc/NEWS Thu May 05 11:14:06 2016 +0300 @@ -268,6 +268,11 @@ - Issue #26873: xmlrpc now raises ResponseError on unsupported type tags instead of silently return incorrect result. +- Issue #26915: The __contains__ methods in the collections ABCs now check + for identity before checking equality. This better matches the behavior + of the concrete classes, allows sensible handling of NaNs, and makes it + easier to reason about container invariants. + - Issue #26711: Fixed the comparison of plistlib.Data with other types. - Issue #24114: Fix an uninitialized variable in `ctypes.util`.