#34609: Deprecate format_html calls without args or kwargs
--------------------------------------+------------------------------------
     Reporter:  Adam Johnson          |                    Owner:  nobody
         Type:  Cleanup/optimization  |                   Status:  new
    Component:  Utilities             |                  Version:  dev
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Bhuvnesh):

 {{{#!diff
 diff --git a/django/utils/html.py b/django/utils/html.py
 index c32a36fa93..b2a0c3d3db 100644
 --- a/django/utils/html.py
 +++ b/django/utils/html.py
 @@ -100,6 +100,8 @@ def format_html(format_string, *args, **kwargs):
      and call mark_safe() on the result. This function should be used
 instead
      of str.format or % interpolation to build up small HTML fragments.
      """
 +    if not (args or kwargs):
 +        raise TypeError("Arguments are missing.")
      args_safe = map(conditional_escape, args)
      kwargs_safe = {k: conditional_escape(v) for (k, v) in kwargs.items()}
      return mark_safe(format_string.format(*args_safe, **kwargs_safe))
 diff --git a/tests/utils_tests/test_html.py
 b/tests/utils_tests/test_html.py
 index b7a7396075..c83fe7ddf6 100644
 --- a/tests/utils_tests/test_html.py
 +++ b/tests/utils_tests/test_html.py
 @@ -65,6 +65,16 @@ class TestUtilsHtml(SimpleTestCase):
              "&lt; Dangerous &gt; <b>safe</b> &lt; dangerous again <i>safe
 again</i>",
          )

 +    def test_format_html_no_args(self):
 +        msg = "Arguments are missing."
 +        with self.assertRaisesMessage(TypeError, msg):
 +            self.assertEqual(
 +                format_html(
 +                    "<i>{name}</i>",
 +                ),
 +                "<i>Adam</i>",
 +            )
 +
      def test_linebreaks(self):
          items = (
              ("para1\n\npara2\r\rpara3",
 "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
 }}}

 Are these changes relevant? I don't have much experience with templates,
 still a lot to learn .😅

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34609#comment:3>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070188724ee714-119c1b66-608f-494f-a3b4-7ca40c33339c-000000%40eu-central-1.amazonses.com.

Reply via email to