]> granicus.if.org Git - python/commitdiff
Issue #21481: Teach argparse equality tests to return NotImplemented when comparing...
authorRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 07:40:09 +0000 (00:40 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 07:40:09 +0000 (00:40 -0700)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS

index a4009d0826ff4f6b2828662ed2f48a85b14fc891..b5bb19a02f5283015b8fe023759cc7f0a1fd124e 100644 (file)
@@ -1157,9 +1157,13 @@ class Namespace(_AttributeHolder):
     __hash__ = None
 
     def __eq__(self, other):
+        if not isinstance(other, Namespace):
+            return NotImplemented
         return vars(self) == vars(other)
 
     def __ne__(self, other):
+        if not isinstance(other, Namespace):
+            return NotImplemented
         return not (self == other)
 
     def __contains__(self, key):
index fdf2b67dc47998906b05f83706ebf0108ac6b787..0df66ad18940dbf014b7ca1a8faa88e40f325bb5 100644 (file)
@@ -4453,6 +4453,12 @@ class TestNamespace(TestCase):
         self.assertTrue(ns2 != ns3)
         self.assertTrue(ns2 != ns4)
 
+    def test_equality_returns_notimplemeted(self):
+        # See issue 21481
+        ns = argparse.Namespace(a=1, b=2)
+        self.assertIs(ns.__eq__(None), NotImplemented)
+        self.assertIs(ns.__ne__(None), NotImplemented)
+
 
 # ===================
 # File encoding tests
index 34c007489dcd84e841aa1873eb92920338348278..663f389254c5d0e561271f9de36ad5c05957c3f2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Library
 - Issue #8743: Fix interoperability between set objects and the
   collections.Set() abstract base class.
 
+- Issue #21481:  Argparse equality and inequality tests now return 
+  NotImplemented when comparing to an unknown type.
+
 Tests
 -----