Browse files

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.
  • Loading branch information...
1 parent bfe25de commit 59d127e45f5ed31e346162a0ac312f672c096821 @bak1an bak1an committed with claudep Apr 17, 2013
Showing with 13 additions and 0 deletions.
  1. +6 −0 django/utils/datastructures.py
  2. +7 −0 tests/utils_tests/test_datastructures.py
View
6 django/utils/datastructures.py
@@ -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:
View
7 tests/utils_tests/test_datastructures.py
@@ -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):

0 comments on commit 59d127e

Please sign in to comment.