From 2b71b2c8dcd40f2604310bb3914077320035b399 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 27 Aug 2024 19:14:50 +0100 Subject: [PATCH] Refs #34609 -- Fixed deprecation warning stack level in format_html(). Co-authored-by: Simon Charette --- django/utils/html.py | 3 ++- tests/utils_tests/test_html.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/django/utils/html.py b/django/utils/html.py index 3ad920aca029..576eabc68390 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -130,10 +130,11 @@ def format_html(format_string, *args, **kwargs): """ if not (args or kwargs): # RemovedInDjango60Warning: when the deprecation ends, replace with: - # raise ValueError("args or kwargs must be provided.") + # raise TypeError("args or kwargs must be provided.") warnings.warn( "Calling format_html() without passing args or kwargs is deprecated.", RemovedInDjango60Warning, + stacklevel=2, ) args_safe = map(conditional_escape, args) kwargs_safe = {k: conditional_escape(v) for (k, v) in kwargs.items()} diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index f6373e304896..befa73a555f4 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -71,10 +71,11 @@ def test_format_html_no_params(self): msg = "Calling format_html() without passing args or kwargs is deprecated." # RemovedInDjango60Warning: when the deprecation ends, replace with: # msg = "args or kwargs must be provided." - # with self.assertRaisesMessage(ValueError, msg): - with self.assertWarnsMessage(RemovedInDjango60Warning, msg): + # with self.assertRaisesMessage(TypeError, msg): + with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: name = "Adam" self.assertEqual(format_html(f"{name}"), "Adam") + self.assertEqual(ctx.filename, __file__) def test_format_html_join_with_positional_arguments(self): self.assertEqual(