Skip to content

Commit

Permalink
Fixed #35603 -- Prevented F.__contains__() from hanging.
Browse files Browse the repository at this point in the history
Regression in 94b6f10.
  • Loading branch information
charettes authored and sarahboyce committed Jul 18, 2024
1 parent 182f262 commit 6b3f554
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions django/db/models/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ def __repr__(self):
def __getitem__(self, subscript):
return Sliced(self, subscript)

def __contains__(self, other):
# Disable old-style iteration protocol inherited from implementing
# __getitem__() to prevent this method from hanging.
raise TypeError(f"argument of type '{self.__class__.__name__}' is not iterable")

def resolve_expression(
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
):
Expand Down
5 changes: 5 additions & 0 deletions tests/expressions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,11 @@ def test_not_equal_Value(self):
self.assertNotEqual(f, value)
self.assertNotEqual(value, f)

def test_contains(self):
msg = "argument of type 'F' is not iterable"
with self.assertRaisesMessage(TypeError, msg):
"" in F("name")


class ExpressionsTests(TestCase):
def test_F_reuse(self):
Expand Down

0 comments on commit 6b3f554

Please sign in to comment.