Commit fb8899a5 authored by Raymond Hettinger's avatar Raymond Hettinger

Issue #21481: Teach argparse equality tests to return NotImplemented when...

Issue #21481:  Teach argparse equality tests to return NotImplemented when comparing to unknown types.
parent f643b9a9
......@@ -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):
......
......@@ -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
......
......@@ -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
-----
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment