[Python-checkins] cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__

nick.coghlan python-checkins at python.org
Sat Jul 7 15:08:42 CEST 2012


http://hg.python.org/cpython/rev/2e9cba1d1554
changeset:   77970:2e9cba1d1554
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sat Jul 07 23:05:59 2012 +1000
summary:
  Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__

files:
  Lib/ipaddress.py           |  12 ++++++------
  Lib/test/test_ipaddress.py |  17 ++++++++++++++++-
  Misc/NEWS                  |   3 +++
  3 files changed, 25 insertions(+), 7 deletions(-)


diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -651,12 +651,12 @@
         return not lt
 
     def __eq__(self, other):
-        if not isinstance(other, _BaseNetwork):
-            raise TypeError('%s and %s are not of the same type' % (
-                             self, other))
-        return (self._version == other._version and
-                self.network_address == other.network_address and
-                int(self.netmask) == int(other.netmask))
+        try:
+            return (self._version == other._version and
+                    self.network_address == other.network_address and
+                    int(self.netmask) == int(other.netmask))
+        except AttributeError:
+            return NotImplemented
 
     def __ne__(self, other):
         eq = self.__eq__(other)
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -462,7 +462,6 @@
         self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
         self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
         self.assertEqual('0x1020318', hex(self.ipv4_network))
-        self.assertRaises(TypeError, self.ipv4_network.__eq__, object())
 
     def testMissingAddressVersion(self):
         class Broken(ipaddress._BaseAddress):
@@ -496,6 +495,22 @@
         self.assertEqual(str(self.ipv6_network.hostmask),
                          '::ffff:ffff:ffff:ffff')
 
+    def testEqualityChecks(self):
+        # __eq__ should never raise TypeError directly
+        other = object()
+        def assertEqualityNotImplemented(instance):
+            self.assertEqual(instance.__eq__(other), NotImplemented)
+            self.assertEqual(instance.__ne__(other), NotImplemented)
+            self.assertFalse(instance == other)
+            self.assertTrue(instance != other)
+
+        assertEqualityNotImplemented(self.ipv4_address)
+        assertEqualityNotImplemented(self.ipv4_network)
+        assertEqualityNotImplemented(self.ipv4_interface)
+        assertEqualityNotImplemented(self.ipv6_address)
+        assertEqualityNotImplemented(self.ipv6_network)
+        assertEqualityNotImplemented(self.ipv6_interface)
+
     def testBadVersionComparison(self):
         # These should always raise TypeError
         v4addr = ipaddress.ip_address('1.1.1.1')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@
 Library
 -------
 
+- Issue #14814: ipaddress network objects correctly return NotImplemented
+  when compared to arbitrary objects instead of raising TypeError
+
 - Issue #14990: Correctly fail with SyntaxError on invalid encoding
   declaration.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list