[tor-commits] [stem/master] Provide inequality methods when overloading __eq__

atagar at torproject.org atagar at torproject.org
Sun Dec 6 21:57:12 UTC 2015


commit 3c864f7d8397c33ef07536fb3a8e9fd20be386bc
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Dec 5 16:23:13 2015 -0800

    Provide inequality methods when overloading __eq__
    
    Really Python? Seriously? Screw you too.
    
    When you provide a custom equality method in your class it effects '==' but
    *not* '!='...
    
      http://stackoverflow.com/a/4352272/1067192
    
    Python, I love you but that's just screwed up. Thankfully they fixed this in
    Python 3.x.
---
 docs/change_log.rst                    |    1 +
 stem/descriptor/microdescriptor.py     |    3 +++
 stem/descriptor/networkstatus.py       |   12 ++++++++++++
 stem/descriptor/router_status_entry.py |   12 ++++++++++++
 stem/descriptor/server_descriptor.py   |    6 ++++++
 stem/exit_policy.py                    |    9 +++++++++
 stem/manual.py                         |    3 +++
 stem/response/events.py                |    3 +++
 stem/version.py                        |    3 +++
 9 files changed, 52 insertions(+)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 080bc54..87ea053 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -52,6 +52,7 @@ The following are only available within Stem's `git repository
   * Added the replica attribute to the :class:`~stem.response.events.HSDescEvent` (:spec:`4989e73`)
   * IPv6 addresses could trigger errors in :func:`~stem.control.Controller.get_listeners`, :class:`~stem.response.events.ORConnEvent`, and quite a few other things (:trac:`16174`)
   * Don't obscure stacktraces, most notably :class:`~stem.control.Controller` getter methods with default values
+  * Classes with custom equality checks didn't provide a corresponding inequality method
 
  * **Descriptors**
 
diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py
index d626101..b190635 100644
--- a/stem/descriptor/microdescriptor.py
+++ b/stem/descriptor/microdescriptor.py
@@ -329,6 +329,9 @@ class Microdescriptor(Descriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index ce6dd73..103f72c 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -945,6 +945,9 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -1235,6 +1238,9 @@ class DirectoryAuthority(Descriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -1348,6 +1354,9 @@ class KeyCertificate(Descriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -1397,6 +1406,9 @@ class DocumentSignature(object):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py
index 1d0305a..991ebaa 100644
--- a/stem/descriptor/router_status_entry.py
+++ b/stem/descriptor/router_status_entry.py
@@ -485,6 +485,9 @@ class RouterStatusEntry(Descriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -525,6 +528,9 @@ class RouterStatusEntryV2(RouterStatusEntry):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -606,6 +612,9 @@ class RouterStatusEntryV3(RouterStatusEntry):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -663,6 +672,9 @@ class RouterStatusEntryMicroV3(RouterStatusEntry):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 07b224a..a555e12 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -764,6 +764,9 @@ class RelayDescriptor(ServerDescriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
@@ -881,6 +884,9 @@ class BridgeDescriptor(ServerDescriptor):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __lt__(self, other):
     return self._compare(other, lambda s, o: s < o)
 
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 4d0f249..717d856 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -527,6 +527,9 @@ class ExitPolicy(object):
     else:
       return False
 
+  def __ne__(self, other):
+    return not self == other
+
 
 class MicroExitPolicy(ExitPolicy):
   """
@@ -610,6 +613,9 @@ class MicroExitPolicy(ExitPolicy):
     else:
       return False
 
+  def __ne__(self, other):
+    return not self == other
+
 
 class ExitPolicyRule(object):
   """
@@ -1029,6 +1035,9 @@ class ExitPolicyRule(object):
     else:
       return False
 
+  def __ne__(self, other):
+    return not self == other
+
 
 def _address_type_to_int(address_type):
   return AddressType.index_of(address_type)
diff --git a/stem/manual.py b/stem/manual.py
index 24ec447..89b49eb 100644
--- a/stem/manual.py
+++ b/stem/manual.py
@@ -442,6 +442,9 @@ class Manual(object):
 
     return True
 
+  def __ne__(self, other):
+    return not self == other
+
 
 def _get_categories(content):
   """
diff --git a/stem/response/events.py b/stem/response/events.py
index 81a1b4c..de75ceb 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -416,6 +416,9 @@ class CircuitEvent(Event):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __gt__(self, other):
     return self._compare(other, lambda s, o: s > o)
 
diff --git a/stem/version.py b/stem/version.py
index a4e184a..68ee40f 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -234,6 +234,9 @@ class Version(object):
   def __eq__(self, other):
     return self._compare(other, lambda s, o: s == o)
 
+  def __ne__(self, other):
+    return not self == other
+
   def __gt__(self, other):
     """
     Checks if this version meets the requirements for a given feature. We can





More information about the tor-commits mailing list