#31961: forms.ChoiceField calls choices callback multiple times. ---------------------------+-------------------------------------- Reporter: Roy Smith | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 2.2 Severity: Normal | Resolution: Keywords: | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 ---------------------------+-------------------------------------- Description changed by Roy Smith:
Old description: > I'm running: > > Python 3.7 > Django 2.2 > Debian 4.9 > > If I configure a forms ChoiceField with a callback function for choices, > it gets called twice each time I render the form: > > {{{ > from unittest import TestCase > from django import forms > > def callback(): > print("callback") > return [('foo', 'bar')] > > class MyForm(forms.Form): > f = forms.ChoiceField(choices=callback) > > class FormTest(TestCase): > MyForm().as_p() > }}} > > prints: > > {{{ > ./manage.py test spi.test_f > callback > callback > System check identified no issues (0 silenced). > > ---------------------------------------------------------------------- > Ran 0 tests in 0.000s > > OK > }}} > > This is the same sort of problem described in #26665 and/or #11390. It's > an obvious performance issue, but more than that, if can beak behavior if > the callback is non-idempotent. > > I discovered this because I had patched my callback using unttest.mock to > return a sequence of return values and got hard-to-diagnose test > failures. Specifically, my test failed because it raised StopIteration > when it ran out of values to return. New description: I'm running: Python 3.7 Django 2.2 Debian 4.9 If I configure a forms ChoiceField with a callback function for choices, it gets called twice each time I render the form: {{{ from unittest import TestCase from django import forms def callback(): print("callback") return [('foo', 'bar')] class MyForm(forms.Form): f = forms.ChoiceField(choices=callback) class FormTest(TestCase): MyForm().as_p() }}} prints: {{{ ./manage.py test spi.test_f callback callback System check identified no issues (0 silenced). ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK }}} This is the same sort of problem described in #26665 and/or #11390. It's an obvious performance issue, but more than that, it can beak behavior if the callback is non-idempotent. I discovered this because I had patched my callback using unttest.mock to return a sequence of return values and got hard-to-diagnose test failures. Specifically, my test failed because it raised StopIteration when it ran out of values to return. -- -- Ticket URL: <https://code.djangoproject.com/ticket/31961#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.d8965a7f34bf78ef2400c141c0eaeb02%40djangoproject.com.