]> 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:43:27 +0000 (00:43 -0700)
committerRaymond Hettinger <python@rcn.com>
Mon, 26 May 2014 07:43:27 +0000 (00:43 -0700)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS

index 5ad7e13a48072a6cebd280b803b52b6c31ac94c9..83878b15274154fcaa42060337f7f7cac233c08c 100644 (file)
@@ -1198,9 +1198,13 @@ class Namespace(_AttributeHolder):
             setattr(self, name, kwargs[name])
 
     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 5c77ec89d0b006d264e3040ef87629f65a1f554a..8a4fdb90a6d36f0088b915cd520f1466713bc246 100644 (file)
@@ -4551,6 +4551,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 cdce6f06ffa48ca9382bd66e0ed1816a36eeb2db..13ff667f1b763440daab24611e569f548d496aaf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Library
 - Issue #14710: pkgutil.find_loader() no longer raises an exception when a
   module doesn't exist.
 
+- Issue #21481:  Argparse equality and inequality tests now return 
+  NotImplemented when comparing to an unknown type.
+
 - Issue #8743: Fix interoperability between set objects and the
   collections.Set() abstract base class.