﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33134	Circular contexts when rendering Form with BoundFields.	Mariusz Felisiak	David Smith	"I noticed really strange error when trying to test `django-debug-toolbar` with Django 4.0a1. The following form:
{{{
class TemplateReprForm(forms.Form):
    user = forms.ModelChoiceField(queryset=User.objects.all())

    def __repr__(self):
        return str(self)
}}}
causes `Fatal Python error: Cannot recover from stack overflow`:
{{{
$ export TOXENV=py38-dj40-sqlite
$ tox tests.panels.test_template.TemplatesPanelTestCase.test_template_repr
...
test_template_repr (tests.panels.test_template.TemplatesPanelTestCase) ... Fatal Python error: Cannot recover from stack overflow.
Python runtime state: initialized
 
Current thread 0x00007f28e059d740 (most recent call first):
  File ""/django-debug-toolbar/tests/forms.py"", line 9 in __repr__
  File ""/usr/lib/python3.8/pprint.py"", line 569 in _safe_repr
  File ""/usr/lib/python3.8/pprint.py"", line 67 in saferepr
  File ""/django-debug-toolbar/debug_toolbar/panels/templates/panel.py"", line 123 in _store_template_info
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/dispatch/dispatcher.py"", line 171 in <listcomp>
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/dispatch/dispatcher.py"", line 170 in send
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/test/utils.py"", line 100 in instrumented_test_render
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/template/base.py"", line 176 in render
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/template/backends/django.py"", line 61 in render
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/forms/renderers.py"", line 23 in render
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/forms/utils.py"", line 53 in render
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/forms/boundfield.py"", line 186 in label_tag
  File ""/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-packages/django/template/base.py"", line 891 in _resolve_lookup
  ...
Aborted (core dumped)
make: *** [Makefile:41: coverage] Error 134
}}}

Removing `form` from `context` in `BoundField.label_tag()` fixes this issue for me:
{{{
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py
index d1e98719d2..5bbfcbe41c 100644
--- a/django/forms/boundfield.py
+++ b/django/forms/boundfield.py
@@ -177,7 +177,6 @@ class BoundField:
                 else:
                     attrs['class'] = self.form.required_css_class
         context = {
-            'form': self.form,
             'field': self,
             'label': contents,
             'attrs': attrs,
}}}

I will try to debug it later."	Bug	closed	Forms	4.0	Release blocker	fixed		David Smith Nick Pope tim-schilling Matthias Kestenholz	Ready for checkin	1	0	0	0	0	0
