From e0c03c63cfcec84dfddd3321bdfbb9e61c74fa88 Mon Sep 17 00:00:00 2001 From: yuyafei Date: Mon, 4 Jul 2016 17:00:46 +0800 Subject: [PATCH] Add __ne__ built-in function In Python 3 __ne__ by default delegates to __eq__ and inverts the result, but in Python 2 they urge you to define __ne__ when you define __eq__ for it to work properly [1].There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected. [1]https://docs.python.org/2/reference/datamodel.html#object.__ne__ Change-Id: I7d41128d88c79ebb5de1c38f8aaf98cc9561367c --- glance/domain/__init__.py | 3 +++ glance/location.py | 3 +++ glance/quota/__init__.py | 6 ++++++ glance/tests/unit/test_domain.py | 6 +++--- glance/tests/unit/test_quota.py | 4 ++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/glance/domain/__init__.py b/glance/domain/__init__.py index a77bf020c2..238ca1a69e 100644 --- a/glance/domain/__init__.py +++ b/glance/domain/__init__.py @@ -295,6 +295,9 @@ class ExtraProperties(collections.MutableMapping, dict): else: return False + def __ne__(self, other): + return not self.__eq__(other) + def __len__(self): return dict(self).__len__() diff --git a/glance/location.py b/glance/location.py index 2739a1caa5..5a8f5cbd05 100644 --- a/glance/location.py +++ b/glance/location.py @@ -303,6 +303,9 @@ class StoreLocations(collections.MutableSequence): def __eq__(self, other): return self.value == self.__cast(other) + def __ne__(self, other): + return not self.__eq__(other) + def __iter__(self): return iter(self.value) diff --git a/glance/quota/__init__.py b/glance/quota/__init__.py index ee3047dc2f..ee16a0c136 100644 --- a/glance/quota/__init__.py +++ b/glance/quota/__init__.py @@ -150,6 +150,9 @@ class QuotaImageTagsProxy(object): def __eq__(self, other): return self.tags == other + def __ne__(self, other): + return not self.__eq__(other) + def __iter__(self, *args, **kwargs): return self.tags.__iter__(*args, **kwargs) @@ -214,6 +217,9 @@ class QuotaImageLocationsProxy(object): def __eq__(self, other): return self.locations == other + def __ne__(self, other): + return not self.__eq__(other) + def __getitem__(self, *args, **kwargs): return self.locations.__getitem__(*args, **kwargs) diff --git a/glance/tests/unit/test_domain.py b/glance/tests/unit/test_domain.py index 95e0bf1649..5328ec3049 100644 --- a/glance/tests/unit/test_domain.py +++ b/glance/tests/unit/test_domain.py @@ -298,7 +298,7 @@ class TestExtraProperties(test_utils.BaseTestCase): a_dict = {'foo': 'bar', 'snitch': 'golden'} extra_properties = domain.ExtraProperties(a_dict) ref_extra_properties = {'boo': 'far', 'gnitch': 'solden'} - self.assertFalse(extra_properties.__eq__(ref_extra_properties)) + self.assertNotEqual(ref_extra_properties, extra_properties) def test_eq_with_unequal_ExtraProperties_object(self): a_dict = {'foo': 'bar', 'snitch': 'golden'} @@ -306,13 +306,13 @@ class TestExtraProperties(test_utils.BaseTestCase): ref_extra_properties = domain.ExtraProperties() ref_extra_properties['gnitch'] = 'solden' ref_extra_properties['boo'] = 'far' - self.assertFalse(extra_properties.__eq__(ref_extra_properties)) + self.assertNotEqual(ref_extra_properties, extra_properties) def test_eq_with_incompatible_object(self): a_dict = {'foo': 'bar', 'snitch': 'golden'} extra_properties = domain.ExtraProperties(a_dict) random_list = ['foo', 'bar'] - self.assertFalse(extra_properties.__eq__(random_list)) + self.assertNotEqual(random_list, extra_properties) class TestTaskFactory(test_utils.BaseTestCase): diff --git a/glance/tests/unit/test_quota.py b/glance/tests/unit/test_quota.py index 0dfadec87a..22bd395613 100644 --- a/glance/tests/unit/test_quota.py +++ b/glance/tests/unit/test_quota.py @@ -572,6 +572,10 @@ class TestQuotaImageTagsProxy(test_utils.BaseTestCase): proxy = glance.quota.QuotaImageTagsProxy(set([])) self.assertEqual(set([]), proxy) + def test_not_equals(self): + proxy = glance.quota.QuotaImageTagsProxy(set([])) + self.assertNotEqual('foo', proxy) + def test_contains(self): proxy = glance.quota.QuotaImageTagsProxy(set(['foo'])) self.assertIn('foo', proxy)