From c9ea9be579d166b92ac602549a6fe809db2cc1a6 Mon Sep 17 00:00:00 2001 From: gecong1973 Date: Thu, 24 Nov 2016 10:00:01 +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: I52633a8a4b19c5e0c0d7d786f21770496e128c7a --- monasca_analytics/banana/grammar/ast.py | 9 +++++ monasca_analytics/banana/typeck/type_util.py | 36 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/monasca_analytics/banana/grammar/ast.py b/monasca_analytics/banana/grammar/ast.py index 0e308e7..efe3db9 100644 --- a/monasca_analytics/banana/grammar/ast.py +++ b/monasca_analytics/banana/grammar/ast.py @@ -189,6 +189,9 @@ class StringLit(ASTNode): return (isinstance(other, StringLit) or isinstance(other, Ident))\ and self.inner_val() == other.inner_val() + def __ne__(self, other): + return not self.__eq__(other) + def __str__(self): return "StringLit< {} >".format(self.val) @@ -218,6 +221,9 @@ class Ident(ASTNode): return (isinstance(other, StringLit) or isinstance(other, Ident))\ and self.val == other.inner_val() + def __ne__(self, other): + return not self.__eq__(other) + def __str__(self): return "Ident< {} >".format(self.val) @@ -274,6 +280,9 @@ class DotPath(ASTNode): def __eq__(self, other): return self.__key() == other.__key() + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.__key()) diff --git a/monasca_analytics/banana/typeck/type_util.py b/monasca_analytics/banana/typeck/type_util.py index 0121726..25e12fd 100644 --- a/monasca_analytics/banana/typeck/type_util.py +++ b/monasca_analytics/banana/typeck/type_util.py @@ -79,6 +79,9 @@ class Any(IsType): # Any type is equal to nothing not even itself. return False + def __ne__(self, other): + return not self.__eq__(other) + def __getitem__(self, _): return Any() @@ -103,6 +106,9 @@ class String(IsType): def __eq__(self, other): return isinstance(other, String) + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(str(self)) @@ -124,6 +130,9 @@ class Number(String): def __eq__(self, other): return isinstance(other, Number) + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(str(self)) @@ -146,6 +155,9 @@ class Enum(String): def __eq__(self, other): return isinstance(other, Enum) and self.variants == other.variants + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.variants) @@ -291,6 +303,9 @@ class Object(String): def __eq__(self, other): return self.props == other + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.props) @@ -418,6 +433,9 @@ class Component(IsType): def __eq__(self, other): return isinstance(other, Component) + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(str(self)) @@ -442,6 +460,9 @@ class Source(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name) @@ -457,6 +478,9 @@ class Ingestor(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name) @@ -472,6 +496,9 @@ class Sink(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name) @@ -487,6 +514,9 @@ class Voter(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name) @@ -502,6 +532,9 @@ class Ldp(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name) @@ -517,6 +550,9 @@ class Sml(Component): def __eq__(self, other): return self.class_name == other.class_name + def __ne__(self, other): + return not self.__eq__(other) + def __hash__(self): return hash(self.class_name)