Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

3 participants

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"})
@bmispelon Collaborator

Not important, but I think it should be {"key": "value"} (note the added space after the colon) per pep8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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(bool(empty))
@bmispelon Collaborator

You don't need to call bool since assertFalse does this (same goes for the next line).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bak1an

@bmispelon updated, thanks

@claudep
Collaborator

The patch is not wrong, but the docs recommend another pattern for __bool__: https://docs.djangoproject.com/en/dev/topics/python3/#boolean-evaluation

@bak1an

@claudep No problem, I will change patch according to the docs.

btw, why it is type(self).__bool__(self) not just self.__bool__()?
Maybe I'm out of my brain resources after coding all day long, but I can't find any use case for this right now.

@bak1an

updated and rebased

@claudep
Collaborator

Thanks, pushed in 59d127e

@claudep claudep closed this
@bak1an

@claudep thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 18, 2013
  1. @bak1an
This page is out of date. Refresh to see the latest.
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):
Something went wrong with that request. Please try again.