diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 871b016715..99f9ff2a72 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -25,6 +25,9 @@ class OrderedSet: def __iter__(self): return iter(self.dict) + def __reversed__(self): + return reversed(self.dict) + def __contains__(self, item): return item in self.dict diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 45d172f984..465f1864dd 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -1,7 +1,7 @@ """ Tests for stuff in django.utils.datastructures. """ - +import collections.abc import copy import pickle @@ -34,6 +34,11 @@ class OrderedSetTests(SimpleTestCase): s.discard(2) self.assertEqual(len(s), 1) + def test_reversed(self): + s = reversed(OrderedSet([1, 2, 3])) + self.assertIsInstance(s, collections.abc.Iterator) + self.assertEqual(list(s), [3, 2, 1]) + def test_contains(self): s = OrderedSet() self.assertEqual(len(s), 0)