Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/_collections_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ def __eq__(self, other):
return NotImplemented
return len(self) == len(other) and self.__le__(other)

__ne__ = object.__ne__

@classmethod
def _from_iterable(cls, it):
'''Construct an instance of the class from any iterable input.
Expand Down Expand Up @@ -821,6 +823,8 @@ def __eq__(self, other):
return NotImplemented
return dict(self.items()) == dict(other.items())

__ne__ = object.__ne__

__reversed__ = None

Mapping.register(mappingproxy)
Expand Down
22 changes: 19 additions & 3 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ def validate_isinstance(self, abc, name):
self.assertNotIsInstance(C(), abc)
self.assertNotIsSubclass(C, abc)

def validate_comparison(self, instance):
def validate_comparison(self, klass):
instance = klass()
ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
operators = {}
for op in ops:
Expand Down Expand Up @@ -782,6 +783,21 @@ def __eq__(self, other):
self.assertTrue(other.right_side,'Right side not called for %s.%s'
% (type(instance), name))

# gh-85588: Inherited __ne__ should be overriddent together with __eq__
# in Set and Mapping.
class Mixin:
def __eq__(self, other):
raise AssertionError('should not be called')
__ne__ = __eq__
class C(klass, Mixin):
pass
instance = C()
other = object()
self.assertIs(instance == other, False)
self.assertIs(instance != other, True)
self.assertIs(instance.__eq__(other), NotImplemented)
self.assertIs(instance.__ne__(other), NotImplemented)

def _test_gen():
yield

Expand Down Expand Up @@ -1430,7 +1446,7 @@ def __len__(self):
return 0
def __iter__(self):
return iter([])
self.validate_comparison(MySet())
self.validate_comparison(MySet)

def test_hash_Set(self):
class OneTwoThreeSet(Set):
Expand Down Expand Up @@ -1852,7 +1868,7 @@ def __getitem__(self, i):
raise IndexError
def __iter__(self):
return iter(())
self.validate_comparison(MyMapping())
self.validate_comparison(MyMapping)
self.assertRaises(TypeError, reversed, MyMapping())

def test_MutableMapping(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Restore the :meth:`!__ne__` method (identical to :meth:`object.__ne__`) in
:class:`collections.abc.Set` and :class:`collections.abc.Mapping` classes.
This guarantees that the ``!=`` operator is consistent with the ``==``
operator, even if the :meth:`!__ne__` method was defined in other parent
class.
Loading