Fixing #20276. MergeDict should have __nonzero__ / __bool__ properly implemented. #1017

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+13 −0
Split
@@ -14,6 +14,12 @@ class MergeDict(object):
def __init__(self, *dicts):
self.dicts = dicts
+ def __bool__(self):
+ return any(self.dicts)
+
+ def __nonzero__(self):
+ return type(self).__bool__(self)
+
def __getitem__(self, key):
for dict_ in self.dicts:
try:
@@ -203,6 +203,13 @@ def test_mergedict_merges_multivaluedict(self):
('key2', ['value2', 'value3']),
('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):
def test_multivaluedict(self):