From ef34175095d92a117fda149ad8a2e216e3a2b78c Mon Sep 17 00:00:00 2001 From: yuyafei Date: Tue, 5 Jul 2016 15:21:02 +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__ Also fixes spelling errors:resoruces. Change-Id: Iae4ce0fe84fae810711cc8c3fdb94eb9ca1d772e Closes-Bug: #1586268 --- keystoneclient/base.py | 4 ++++ keystoneclient/tests/unit/test_base.py | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/keystoneclient/base.py b/keystoneclient/base.py index d028d97e7..3f13a6cc9 100644 --- a/keystoneclient/base.py +++ b/keystoneclient/base.py @@ -523,6 +523,10 @@ class Resource(object): return False return self._info == other._info + def __ne__(self, other): + """Define inequality for resources.""" + return not self == other + def is_loaded(self): return self._loaded diff --git a/keystoneclient/tests/unit/test_base.py b/keystoneclient/tests/unit/test_base.py index 7a43619fc..9814d3d16 100644 --- a/keystoneclient/tests/unit/test_base.py +++ b/keystoneclient/tests/unit/test_base.py @@ -58,25 +58,37 @@ class BaseTest(utils.TestCase): r1 = base.Resource(None, {'id': 1, 'name': 'hi'}) r2 = base.Resource(None, {'id': 1, 'name': 'hello'}) self.assertNotEqual(r1, r2) + self.assertTrue(r1 != r2) # Two resources with same ID: equal if their info is equal + # The truth of r1==r2 does not imply that r1!=r2 is false in PY2. + # Test that inequality operator is defined and that comparing equal + # items returns False r1 = base.Resource(None, {'id': 1, 'name': 'hello'}) r2 = base.Resource(None, {'id': 1, 'name': 'hello'}) - self.assertEqual(r1, r2) + self.assertTrue(r1 == r2) + self.assertFalse(r1 != r2) - # Two resoruces of different types: never equal + # Two resources of different types: never equal r1 = base.Resource(None, {'id': 1}) r2 = roles.Role(None, {'id': 1}) self.assertNotEqual(r1, r2) + self.assertTrue(r1 != r2) # Two resources with no ID: equal if their info is equal + # The truth of r1==r2 does not imply that r1!=r2 is false in PY2. + # Test that inequality operator is defined and that comparing equal + # items returns False. r1 = base.Resource(None, {'name': 'joe', 'age': 12}) r2 = base.Resource(None, {'name': 'joe', 'age': 12}) - self.assertEqual(r1, r2) + self.assertTrue(r1 == r2) + self.assertFalse(r1 != r2) r1 = base.Resource(None, {'id': 1}) self.assertNotEqual(r1, object()) + self.assertTrue(r1 != object()) self.assertNotEqual(r1, {'id': 1}) + self.assertTrue(r1 != {'id': 1}) def test_human_id(self): r = base.Resource(None, {"name": "1 of !"})