From: Raymond Hettinger Date: Mon, 26 May 2014 07:40:09 +0000 (-0700) Subject: Issue #21481: Teach argparse equality tests to return NotImplemented when comparing... X-Git-Tag: v2.7.8~37^2~81 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fb8899a597c5ef39706215a894fc8c5f5dbf85bc;p=thirdparty%2FPython%2Fcpython.git Issue #21481: Teach argparse equality tests to return NotImplemented when comparing to unknown types. --- diff --git a/Lib/argparse.py b/Lib/argparse.py index a4009d0826ff..b5bb19a02f52 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index fdf2b67dc479..0df66ad18940 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -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 diff --git a/Misc/NEWS b/Misc/NEWS index 34c007489dcd..663f389254c5 100644 --- 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 -----