From 6ee40c217024607d808ca69ea1a6f0f3fa4f1f84 Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Thu, 14 Jun 2018 14:46:48 +0100 Subject: [PATCH] utils/types: implement __ne__ for level This should have been handled by the @total_ordering decorator, but isn't due to https://bugs.python.org/issue25732 (briefly, total_ordering is back-ported from Python 3, where the base object provides the default implementation of __ne__ based on __eq__, so total_ordering did not override it; this, however does not happen in Python 2). Also update unit tests to catch this edge case. --- tests/test_utils.py | 2 ++ wa/utils/types.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index c3469f9e..4f09a478 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -114,6 +114,8 @@ class TestEnumLevel(TestCase): assert_in('names', e.levels) assert_list_equal(e.names, ['names', 'one', 'two']) assert_equal(e.NAMES, 'names') + result = not (e.NAMES != 'names') + assert_true(result) def test_enum_behavior(self): e = enum(['one', 'two', 'three']) diff --git a/wa/utils/types.py b/wa/utils/types.py index 6256b9c9..c68d504c 100644 --- a/wa/utils/types.py +++ b/wa/utils/types.py @@ -569,6 +569,15 @@ class level(object): else: return self.value < other + def __ne__(self, other): + if isinstance(other, level): + return self.value != other.value + elif isinstance(other, basestring): + return self.name != other + else: + return self.value != other + + class _EnumMeta(type):