From 31878b4d7387835c5cec5e7fc5b473b737065a58 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 11 Jan 2023 06:07:16 +0100 Subject: [PATCH] Refs #31026 -- Removed ability to return string when rendering ErrorDict/ErrorList. Per deprecation timeline. --- django/forms/forms.py | 12 ---- docs/ref/forms/api.txt | 5 -- docs/releases/5.0.txt | 3 + .../tests/test_deprecation_forms.py | 55 ------------------- 4 files changed, 3 insertions(+), 72 deletions(-) delete mode 100644 tests/forms_tests/tests/test_deprecation_forms.py diff --git a/django/forms/forms.py b/django/forms/forms.py index 02ac26d9ae..b99ec8248a 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -4,16 +4,13 @@ Form classes import copy import datetime -import warnings from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.forms.fields import Field, FileField from django.forms.utils import ErrorDict, ErrorList, RenderableFormMixin from django.forms.widgets import Media, MediaDefiningClass from django.utils.datastructures import MultiValueDict -from django.utils.deprecation import RemovedInDjango50Warning from django.utils.functional import cached_property -from django.utils.safestring import SafeString, mark_safe from django.utils.translation import gettext as _ from .renderers import get_default_renderer @@ -238,15 +235,6 @@ class BaseForm(RenderableFormMixin): hidden_fields.append(bf) else: errors_str = str(bf_errors) - # RemovedInDjango50Warning. - if not isinstance(errors_str, SafeString): - warnings.warn( - f"Returning a plain string from " - f"{self.error_class.__name__} is deprecated. Please " - f"customize via the template system instead.", - RemovedInDjango50Warning, - ) - errors_str = mark_safe(errors_str) fields.append((bf, errors_str)) return { "form": self, diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 0bb3730daa..d74fba7476 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -1015,11 +1015,6 @@ Customizing the error list format overriding the default template, see also :ref:`overriding-built-in-form-templates`. -.. deprecated:: 4.0 - - The ability to return a ``str`` when calling the ``__str__`` method is - deprecated. Use the template engine instead which returns a ``SafeString``. - More granular output ==================== diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index e3c1919072..9fc612f418 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -299,6 +299,9 @@ to remove usage of these features. * The undocumented ``BaseForm._html_output()`` method is removed. +* The ability to return a ``str``, rather than a ``SafeString``, when rendering + an ``ErrorDict`` and ``ErrorList`` is removed. + See :ref:`deprecated-features-4.1` for details on these changes, including how to remove usage of these features. diff --git a/tests/forms_tests/tests/test_deprecation_forms.py b/tests/forms_tests/tests/test_deprecation_forms.py deleted file mode 100644 index dbc751bae8..0000000000 --- a/tests/forms_tests/tests/test_deprecation_forms.py +++ /dev/null @@ -1,55 +0,0 @@ -# RemovedInDjango50 -from django.forms import CharField, EmailField, Form -from django.forms.utils import ErrorList -from django.test import SimpleTestCase, ignore_warnings -from django.utils.deprecation import RemovedInDjango50Warning - - -class DivErrorList(ErrorList): - def __str__(self): - return self.as_divs() - - def as_divs(self): - if not self: - return "" - return '
%s
' % "".join( - f'
{error}
' for error in self - ) - - -class DeprecationTests(SimpleTestCase): - def test_deprecation_warning_error_list(self): - class EmailForm(Form): - email = EmailField() - comment = CharField() - - data = {"email": "invalid"} - f = EmailForm(data, error_class=DivErrorList) - msg = ( - "Returning a plain string from DivErrorList is deprecated. Please " - "customize via the template system instead." - ) - with self.assertRaisesMessage(RemovedInDjango50Warning, msg): - f.as_p() - - -@ignore_warnings(category=RemovedInDjango50Warning) -class DeprecatedTests(SimpleTestCase): - def test_errorlist_override_str(self): - class CommentForm(Form): - name = CharField(max_length=50, required=False) - email = EmailField() - comment = CharField() - - data = {"email": "invalid"} - f = CommentForm(data, auto_id=False, error_class=DivErrorList) - self.assertHTMLEqual( - f.as_p(), - '

Name:

' - '
' - '
Enter a valid email address.
' - '

Email:

' - '
' - '
This field is required.
' - '

Comment:

', - )