summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/query.py5
-rw-r--r--django/utils/datastructures.py5
-rw-r--r--tests/utils_tests/test_datastructures.py12
3 files changed, 19 insertions, 3 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 6dbca8369a..85f1981662 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -162,10 +162,13 @@ class QuerySet(object):
self._fetch_all()
return iter(self._result_cache)
- def __nonzero__(self):
+ def __bool__(self):
self._fetch_all()
return bool(self._result_cache)
+ def __nonzero__(self): # Python 2 compatibility
+ return type(self).__bool__(self)
+
def __getitem__(self, k):
"""
Retrieves an item or slice from the set of results.
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index 2efcd00eb0..49fd9d9848 100644
--- a/django/utils/datastructures.py
+++ b/django/utils/datastructures.py
@@ -271,9 +271,12 @@ class OrderedSet(object):
def __contains__(self, item):
return item in self.dict
- def __nonzero__(self):
+ def __bool__(self):
return bool(self.dict)
+ def __nonzero__(self): # Python 2 compatibility
+ return type(self).__bool__(self)
+
class MultiValueDictKeyError(KeyError):
pass
diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py
index a163310e19..ec4001cafc 100644
--- a/tests/utils_tests/test_datastructures.py
+++ b/tests/utils_tests/test_datastructures.py
@@ -8,7 +8,7 @@ import pickle
from django.test import SimpleTestCase
from django.test.utils import IgnoreDeprecationWarningsMixin
from django.utils.datastructures import (DictWrapper, ImmutableList,
- MultiValueDict, MultiValueDictKeyError, MergeDict, SortedDict)
+ MultiValueDict, MultiValueDictKeyError, MergeDict, OrderedSet, SortedDict)
from django.utils import six
@@ -206,6 +206,16 @@ class MergeDictTests(IgnoreDeprecationWarningsMixin, SimpleTestCase):
d1['key2']
+class OrderedSetTests(SimpleTestCase):
+
+ def test_bool(self):
+ # Refs #23664
+ s = OrderedSet()
+ self.assertFalse(s)
+ s.add(1)
+ self.assertTrue(s)
+
+
class MultiValueDictTests(SimpleTestCase):
def test_multivaluedict(self):