[Python-checkins] cpython (2.7): Issue #21481: Teach argparse equality tests to return NotImplemented when

raymond.hettinger python-checkins at python.org
Mon May 26 09:40:37 CEST 2014


http://hg.python.org/cpython/rev/ba84d1e9a742
changeset:   90842:ba84d1e9a742
branch:      2.7
parent:      90839:3615cdb3b86d
user:        Raymond Hettinger <python at rcn.com>
date:        Mon May 26 00:40:09 2014 -0700
summary:
  Issue #21481:  Teach argparse equality tests to return NotImplemented when comparing to unknown types.

files:
  Lib/argparse.py           |  4 ++++
  Lib/test/test_argparse.py |  6 ++++++
  Misc/NEWS                 |  3 +++
  3 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/argparse.py b/Lib/argparse.py
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1157,9 +1157,13 @@
     __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):
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4453,6 +4453,12 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@
 - 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
 -----
 

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


More information about the Python-checkins mailing list