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: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb8899a597c5ef39706215a894fc8c5f5dbf85bc;p=python 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 a4009d0826..b5bb19a02f 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 fdf2b67dc4..0df66ad189 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 34c007489d..663f389254 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 -----