From 29a518006f7f96186483fa50e249e1c3f21728d5 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 2 Jun 2017 06:46:43 -0700 Subject: [PATCH] Fixed #28265 -- Prevented renderer warning on Widget.render() with **kwargs. --- django/forms/boundfield.py | 4 +-- docs/releases/1.11.3.txt | 4 ++- .../widget_tests/test_render_deprecation.py | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/forms_tests/widget_tests/test_render_deprecation.py diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 771661ba6a..83944422ff 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -6,7 +6,7 @@ from django.forms.widgets import Textarea, TextInput from django.utils.deprecation import RemovedInDjango21Warning from django.utils.functional import cached_property from django.utils.html import conditional_escape, format_html, html_safe -from django.utils.inspect import func_supports_parameter +from django.utils.inspect import func_accepts_kwargs, func_supports_parameter from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ @@ -103,7 +103,7 @@ class BoundField: name = self.html_initial_name kwargs = {} - if func_supports_parameter(widget.render, 'renderer'): + if func_supports_parameter(widget.render, 'renderer') or func_accepts_kwargs(widget.render): kwargs['renderer'] = self.form.renderer else: warnings.warn( diff --git a/docs/releases/1.11.3.txt b/docs/releases/1.11.3.txt index 7eacbd8a36..15200fafd5 100644 --- a/docs/releases/1.11.3.txt +++ b/docs/releases/1.11.3.txt @@ -9,4 +9,6 @@ Django 1.11.3 fixes several bugs in 1.11.2. Bugfixes ======== -* ... +* Removed an incorrect deprecation warning about a missing ``renderer`` + argument if a ``Widget.render()`` method accepts ``**kwargs`` + (:ticket:`28265`). diff --git a/tests/forms_tests/widget_tests/test_render_deprecation.py b/tests/forms_tests/widget_tests/test_render_deprecation.py new file mode 100644 index 0000000000..4059f043e3 --- /dev/null +++ b/tests/forms_tests/widget_tests/test_render_deprecation.py @@ -0,0 +1,35 @@ +from django import forms +from django.test import SimpleTestCase +from django.utils.deprecation import RemovedInDjango21Warning + + +class RenderDeprecationTests(SimpleTestCase): + def test_custom_widget_renderer_warning(self): + class CustomWidget1(forms.TextInput): + def render(self, name, value, attrs=None, renderer=None): + return super().render(name, value, attrs, renderer) + + class CustomWidget2(forms.TextInput): + def render(self, *args, **kwargs): + return super().render(*args, **kwargs) + + class CustomWidget3(forms.TextInput): + def render(self, name, value, attrs=None): + return super().render(name, value, attrs) + + class MyForm(forms.Form): + foo = forms.CharField(widget=CustomWidget1) + bar = forms.CharField(widget=CustomWidget2) + baz = forms.CharField(widget=CustomWidget3) + + form = MyForm() + str(form['foo']) # No warning. + str(form['bar']) # No warning. + msg = ( + "Add the `renderer` argument to the render() method of " + ".CustomWidget3'>. It will be mandatory in Django 2.1." + ) + with self.assertRaisesMessage(RemovedInDjango21Warning, msg): + str(form['baz'])