From d3d95d645fc9f36355d27598475612de43587db1 Mon Sep 17 00:00:00 2001 From: saeedblanchette <33104967+saeedblanchette@users.noreply.github.com> Date: Tue, 11 May 2021 04:02:25 +0000 Subject: [PATCH] Refs #24121 -- Added __repr__() to Lookup. --- django/db/models/lookups.py | 3 +++ tests/lookup/test_lookups.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 8d3648b393..defa566f21 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -40,6 +40,9 @@ class Lookup: value = transform(value) return value + def __repr__(self): + return f'{self.__class__.__name__}({self.lhs!r}, {self.rhs!r})' + def batch_process_rhs(self, compiler, connection, rhs=None): if rhs is None: rhs = self.rhs diff --git a/tests/lookup/test_lookups.py b/tests/lookup/test_lookups.py index 4d90612048..6dfcf75943 100644 --- a/tests/lookup/test_lookups.py +++ b/tests/lookup/test_lookups.py @@ -20,6 +20,23 @@ class LookupTests(SimpleTestCase): self.assertNotEqual(lookup, Lookup(Value(3), lookup.rhs)) self.assertNotEqual(lookup, CustomLookup(lookup.lhs, lookup.rhs)) + def test_repr(self): + tests = [ + (Lookup(Value(1), Value('a')), "Lookup(Value(1), Value('a'))"), + ( + YearLookup( + Value(datetime(2010, 1, 1, 0, 0, 0)), + Value(datetime(2010, 1, 1, 23, 59, 59)), + ), + 'YearLookup(' + 'Value(datetime.datetime(2010, 1, 1, 0, 0)), ' + 'Value(datetime.datetime(2010, 1, 1, 23, 59, 59)))' + ), + ] + for lookup, expected in tests: + with self.subTest(lookup=lookup): + self.assertEqual(repr(lookup), expected) + def test_hash(self): lookup = Lookup(Value(1), Value(2)) self.assertEqual(hash(lookup), hash(lookup))