Fixed #20276 -- Implemented __bool__ for MergeDict

MergeDict evaluates now to False if all contained dicts are empty.
Thanks til for the report and the initial patch.
This commit is contained in:
Anton Baklanov 2013-04-17 18:20:31 +03:00 committed by Claude Paroz
parent bfe25de429
commit 59d127e45f
2 changed files with 13 additions and 0 deletions

View File

@ -14,6 +14,12 @@ class MergeDict(object):
def __init__(self, *dicts): def __init__(self, *dicts):
self.dicts = dicts self.dicts = dicts
def __bool__(self):
return any(self.dicts)
def __nonzero__(self):
return type(self).__bool__(self)
def __getitem__(self, key): def __getitem__(self, key):
for dict_ in self.dicts: for dict_ in self.dicts:
try: try:

View File

@ -203,6 +203,13 @@ class MergeDictTests(SimpleTestCase):
('key2', ['value2', 'value3']), ('key2', ['value2', 'value3']),
('key4', ['value5', 'value6'])]) ('key4', ['value5', 'value6'])])
def test_bool_casting(self):
empty = MergeDict({}, {}, {})
not_empty = MergeDict({}, {}, {"key": "value"})
self.assertFalse(empty)
self.assertTrue(not_empty)
class MultiValueDictTests(SimpleTestCase): class MultiValueDictTests(SimpleTestCase):
def test_multivaluedict(self): def test_multivaluedict(self):