Skip to content

Commit

Permalink
Refs #35844 -- Fixed OtherModelFormTests.test_prefetch_related_querys…
Browse files Browse the repository at this point in the history
…et() test on Python 3.14+.

python/cpython@5a23994
  • Loading branch information
felixxm authored and sarahboyce committed Dec 20, 2024
1 parent f05edb2 commit fcd9d08
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions tests/model_forms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from django.test.utils import isolate_apps
from django.utils.choices import BlankChoiceIterator
from django.utils.deprecation import RemovedInDjango60Warning
from django.utils.version import PYPY
from django.utils.version import PY314, PYPY

from .models import (
Article,
Expand Down Expand Up @@ -3048,10 +3048,11 @@ def label_from_instance(self, obj):
return ", ".join(c.name for c in obj.colours.all())

field = ColorModelChoiceField(ColourfulItem.objects.prefetch_related("colours"))
# CPython calls ModelChoiceField.__len__() when coercing to tuple. PyPy
# doesn't call __len__() and so .count() isn't called on the QuerySet.
# The following would trigger an extra query if prefetch were ignored.
with self.assertNumQueries(2 if PYPY else 3):
# CPython < 3.14 calls ModelChoiceField.__len__() when coercing to
# tuple. PyPy and Python 3.14+ don't call __len__() and so .count()
# isn't called on the QuerySet. The following would trigger an extra
# query if prefetch were ignored.
with self.assertNumQueries(2 if PYPY or PY314 else 3):
self.assertEqual(
tuple(field.choices),
(
Expand Down

0 comments on commit fcd9d08

Please sign in to comment.