summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryuyafei <yu.yafei@zte.com.cn>2016-07-05 15:21:02 +0800
committeryuyafei <yu.yafei@zte.com.cn>2016-08-11 11:19:30 +0800
commitef34175095d92a117fda149ad8a2e216e3a2b78c (patch)
tree5f22e77174783ca3a6911a51113b2c00b539c39d
parent64d458fcf8c2f7eab24f0134f3c27e15c181fe15 (diff)
downloadpython-keystoneclient-ef34175095d92a117fda149ad8a2e216e3a2b78c.tar.gz
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
-rw-r--r--keystoneclient/base.py4
-rw-r--r--keystoneclient/tests/unit/test_base.py18
2 files changed, 19 insertions, 3 deletions
diff --git a/keystoneclient/base.py b/keystoneclient/base.py
index d028d97..3f13a6c 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 7a43619..9814d3d 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 !"})