From 7666acbb1ecfc6fecdc060c70e75cd29c00972d4 Mon Sep 17 00:00:00 2001 From: howardlee Date: Wed, 16 Nov 2016 19:14:52 +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: I6ad4b07911f6c8236dfcd048aceebd44cd760fe2 --- keystoneauth1/loading/opts.py | 6 ++++++ keystoneauth1/tests/unit/loading/utils.py | 6 ++++++ keystoneauth1/tests/unit/utils.py | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/keystoneauth1/loading/opts.py b/keystoneauth1/loading/opts.py index 5b4ecac7..b8f17590 100644 --- a/keystoneauth1/loading/opts.py +++ b/keystoneauth1/loading/opts.py @@ -125,6 +125,12 @@ class Opt(object): self.default == other.default and self.metavar == other.metavar) + # NOTE: This function is only needed by Python 2. If we get to point where + # we don't support Python 2 anymore, this function should be removed. + def __ne__(self, other): + """Define inequality operator on option parameters.""" + return not self.__eq__(other) + @property def _all_opts(self): return itertools.chain([self], self.deprecated) diff --git a/keystoneauth1/tests/unit/loading/utils.py b/keystoneauth1/tests/unit/loading/utils.py index e8958085..c7c8de4e 100644 --- a/keystoneauth1/tests/unit/loading/utils.py +++ b/keystoneauth1/tests/unit/loading/utils.py @@ -76,6 +76,12 @@ class BoolType(object): # hack around oslo.config equality comparison return type(self) == type(other) + # NOTE: This function is only needed by Python 2. If we get to point where + # we don't support Python 2 anymore, this function should be removed. + def __ne__(self, other): + """Define inequiality for many bool types.""" + return not self.__eq__(other) + def __call__(self, value): return str(value).lower() in ('1', 'true', 't', 'yes', 'y') diff --git a/keystoneauth1/tests/unit/utils.py b/keystoneauth1/tests/unit/utils.py index 1aef9da3..0245b291 100644 --- a/keystoneauth1/tests/unit/utils.py +++ b/keystoneauth1/tests/unit/utils.py @@ -135,6 +135,12 @@ class TestResponse(requests.Response): """Define equiality behavior of request and response.""" return self.__dict__ == other.__dict__ + # NOTE: This function is only needed by Python 2. If we get to point where + # we don't support Python 2 anymore, this function should be removed. + def __ne__(self, other): + """Define inequiality behavior of request and response.""" + return not self.__eq__(other) + @property def text(self): return self.content