Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mm committed Oct 23, 2023
2 parents 71d9ab4 + 171f91d commit 545e752
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 48 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.0
rev: 23.10.0
hooks:
- id: black
exclude: \.py-tpl$
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.13.0
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies:
- black==23.9.0
- black==23.10.0
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
Expand All @@ -19,6 +19,6 @@ repos:
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.49.0
rev: v8.52.0
hooks:
- id: eslint
31 changes: 11 additions & 20 deletions django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from django.db.models.constants import LOOKUP_SEP
from django.db.models.query_utils import DeferredAttribute, RegisterLookupMixin
from django.utils import timezone
from django.utils.choices import CallableChoiceIterator, normalize_choices
from django.utils.choices import (
BlankChoiceIterator,
CallableChoiceIterator,
flatten_choices,
normalize_choices,
)
from django.utils.datastructures import DictWrapper
from django.utils.dateparse import (
parse_date,
Expand Down Expand Up @@ -1051,14 +1056,9 @@ def get_choices(
as <select> choices for this field.
"""
if self.choices is not None:
choices = list(self.choices)
if include_blank:
blank_defined = any(
choice in ("", None) for choice, _ in self.flatchoices
)
if not blank_defined:
choices = blank_choice + choices
return choices
return BlankChoiceIterator(self.choices, blank_choice)
return self.choices
rel_model = self.remote_field.model
limit_choices_to = limit_choices_to or self.get_limit_choices_to()
choice_func = operator.attrgetter(
Expand All @@ -1080,19 +1080,10 @@ def value_to_string(self, obj):
"""
return str(self.value_from_object(obj))

def _get_flatchoices(self):
@property
def flatchoices(self):
"""Flattened version of choices tuple."""
if self.choices is None:
return []
flat = []
for choice, value in self.choices:
if isinstance(value, (list, tuple)):
flat.extend(value)
else:
flat.append((choice, value))
return flat

flatchoices = property(_get_flatchoices)
return list(flatten_choices(self.choices))

def save_form_data(self, instance, data):
setattr(instance, self.name, data)
Expand Down
2 changes: 1 addition & 1 deletion django/test/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def _init_worker(
django.setup()
setup_test_environment(debug=debug_mode)

db_aliases = used_aliases or connections
db_aliases = used_aliases if used_aliases is not None else connections
for alias in db_aliases:
connection = connections[alias]
if start_method == "spawn":
Expand Down
51 changes: 51 additions & 0 deletions django/utils/choices.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,53 @@
from collections.abc import Callable, Iterable, Iterator, Mapping
from itertools import islice, tee, zip_longest

from django.utils.functional import Promise

__all__ = [
"BaseChoiceIterator",
"BlankChoiceIterator",
"CallableChoiceIterator",
"flatten_choices",
"normalize_choices",
]


class BaseChoiceIterator:
"""Base class for lazy iterators for choices."""

def __eq__(self, other):
if isinstance(other, Iterable):
return all(a == b for a, b in zip_longest(self, other, fillvalue=object()))
return super().__eq__(other)

def __getitem__(self, index):
if index < 0:
# Suboptimally consume whole iterator to handle negative index.
return list(self)[index]
try:
return next(islice(self, index, index + 1))
except StopIteration:
raise IndexError("index out of range") from None

def __iter__(self):
raise NotImplementedError(
"BaseChoiceIterator subclasses must implement __iter__()."
)


class BlankChoiceIterator(BaseChoiceIterator):
"""Iterator to lazily inject a blank choice."""

def __init__(self, choices, blank_choice):
self.choices = choices
self.blank_choice = blank_choice

def __iter__(self):
choices, other = tee(self.choices)
if not any(value in ("", None) for value, _ in flatten_choices(other)):
yield from self.blank_choice
yield from choices


class CallableChoiceIterator(BaseChoiceIterator):
"""Iterator to lazily normalize choices generated by a callable."""
Expand All @@ -17,6 +59,15 @@ def __iter__(self):
yield from normalize_choices(self.func())


def flatten_choices(choices):
"""Flatten choices by removing nested values."""
for value_or_group, label_or_nested in choices or ():
if isinstance(label_or_nested, (list, tuple)):
yield from label_or_nested
else:
yield value_or_group, label_or_nested


def normalize_choices(value, *, depth=0):
"""Normalize choices values consistently for fields and widgets."""
# Avoid circular import when importing django.forms.
Expand Down
20 changes: 8 additions & 12 deletions django/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ def __init__(self, get_response):
if get_response is None:
raise ValueError("get_response must be provided.")
self.get_response = get_response
self._async_check()
# If get_response is a coroutine function, turns us into async mode so
# a thread is not consumed during a whole request.
self.async_mode = iscoroutinefunction(self.get_response)
if self.async_mode:
# Mark the class as async-capable, but do the actual switch inside
# __call__ to avoid swapping out dunder methods.
markcoroutinefunction(self)
super().__init__()

def __repr__(self):
Expand All @@ -113,19 +119,9 @@ def __repr__(self):
),
)

def _async_check(self):
"""
If get_response is a coroutine function, turns us into async mode so
a thread is not consumed during a whole request.
"""
if iscoroutinefunction(self.get_response):
# Mark the class as async-capable, but do the actual switch
# inside __call__ to avoid swapping out dunder methods
markcoroutinefunction(self)

def __call__(self, request):
# Exit out to async mode, if needed
if iscoroutinefunction(self):
if self.async_mode:
return self.__acall__(request)
response = None
if hasattr(self, "process_request"):
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"npm": ">=1.3.0"
},
"devDependencies": {
"eslint": "^8.49.0",
"puppeteer": "^21.1.1",
"eslint": "^8.52.0",
"puppeteer": "^21.4.0",
"grunt": "^1.6.1",
"grunt-cli": "^1.4.3",
"grunt-contrib-qunit": "^7.0.0",
"qunit": "^2.19.4"
"grunt-contrib-qunit": "^8.0.1",
"qunit": "^2.20.0"
}
}
33 changes: 33 additions & 0 deletions tests/model_forms/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.template import Context, Template
from django.test import SimpleTestCase, TestCase, ignore_warnings, skipUnlessDBFeature
from django.test.utils import isolate_apps
from django.utils.choices import BlankChoiceIterator
from django.utils.deprecation import RemovedInDjango60Warning

from .models import (
Expand Down Expand Up @@ -2012,6 +2013,38 @@ def test_runtime_choicefield_populated(self):
),
)

@isolate_apps("model_forms")
def test_callable_choices_are_lazy(self):
call_count = 0

def get_animal_choices():
nonlocal call_count
call_count += 1
return [("LION", "Lion"), ("ZEBRA", "Zebra")]

class ZooKeeper(models.Model):
animal = models.CharField(
blank=True,
choices=get_animal_choices,
max_length=5,
)

class ZooKeeperForm(forms.ModelForm):
class Meta:
model = ZooKeeper
fields = ["animal"]

self.assertEqual(call_count, 0)
form = ZooKeeperForm()
self.assertEqual(call_count, 0)
self.assertIsInstance(form.fields["animal"].choices, BlankChoiceIterator)
self.assertEqual(call_count, 0)
self.assertEqual(
form.fields["animal"].choices,
models.BLANK_CHOICE_DASH + [("LION", "Lion"), ("ZEBRA", "Zebra")],
)
self.assertEqual(call_count, 1)

def test_recleaning_model_form_instance(self):
"""
Re-cleaning an instance that was added via a ModelForm shouldn't raise
Expand Down
Loading

0 comments on commit 545e752

Please sign in to comment.