From afb0eb8bb3e1c6f7ea842d2cede684eb4aec3f3d Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Wed, 10 Mar 2021 08:29:05 +0000 Subject: [PATCH] Refs #24121 -- Added __repr__() to OrderedSet. --- django/utils/datastructures.py | 4 ++++ tests/utils_tests/test_datastructures.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 99f9ff2a72..e605ce0720 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -37,6 +37,10 @@ class OrderedSet: def __len__(self): return len(self.dict) + def __repr__(self): + data = repr(list(self.dict)) if self.dict else '' + return f'{self.__class__.__qualname__}({data})' + class MultiValueDictKeyError(KeyError): pass diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 465f1864dd..259ebb2f7b 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -60,6 +60,10 @@ class OrderedSetTests(SimpleTestCase): s.add(2) self.assertEqual(len(s), 2) + def test_repr(self): + self.assertEqual(repr(OrderedSet()), 'OrderedSet()') + self.assertEqual(repr(OrderedSet([2, 3, 2, 1])), 'OrderedSet([2, 3, 1])') + class MultiValueDictTests(SimpleTestCase):