diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 6c60675d38..76f7459032 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -37,6 +37,9 @@ class OrderedSet(object): def __nonzero__(self): # Python 2 compatibility return type(self).__bool__(self) + def __len__(self): + return len(self.dict) + class MultiValueDictKeyError(KeyError): pass diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index c0ed3435eb..99f7a1d05f 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -21,6 +21,14 @@ class OrderedSetTests(SimpleTestCase): s.add(1) self.assertTrue(s) + def test_len(self): + s = OrderedSet() + self.assertEqual(len(s), 0) + s.add(1) + s.add(2) + s.add(2) + self.assertEqual(len(s), 2) + class MultiValueDictTests(SimpleTestCase):