mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Issue #21481: Teach argparse equality tests to return NotImplemented when comparing to unknown types.
This commit is contained in:
parent
dd5e53a086
commit
dea46ec965
3 changed files with 13 additions and 0 deletions
|
@ -1198,9 +1198,13 @@ class Namespace(_AttributeHolder):
|
||||||
setattr(self, name, kwargs[name])
|
setattr(self, name, kwargs[name])
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, Namespace):
|
||||||
|
return NotImplemented
|
||||||
return vars(self) == vars(other)
|
return vars(self) == vars(other)
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
|
if not isinstance(other, Namespace):
|
||||||
|
return NotImplemented
|
||||||
return not (self == other)
|
return not (self == other)
|
||||||
|
|
||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
|
|
|
@ -4551,6 +4551,12 @@ class TestNamespace(TestCase):
|
||||||
self.assertTrue(ns2 != ns3)
|
self.assertTrue(ns2 != ns3)
|
||||||
self.assertTrue(ns2 != ns4)
|
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
|
# File encoding tests
|
||||||
|
|
|
@ -24,6 +24,9 @@ Library
|
||||||
- Issue #14710: pkgutil.find_loader() no longer raises an exception when a
|
- Issue #14710: pkgutil.find_loader() no longer raises an exception when a
|
||||||
module doesn't exist.
|
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
|
- Issue #8743: Fix interoperability between set objects and the
|
||||||
collections.Set() abstract base class.
|
collections.Set() abstract base class.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue