From 6bd5d4f7055c999e5ef1f51f08bc48c8853a4e0a Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 9 Aug 2024 12:35:28 -0400 Subject: [PATCH] Refs #22712 -- Adjusted deprecation warning stacklevel in staticfiles finders. --- django/contrib/staticfiles/finders.py | 6 +++--- tests/staticfiles_tests/test_finders.py | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/django/contrib/staticfiles/finders.py b/django/contrib/staticfiles/finders.py index 8051650daf..c2087569db 100644 --- a/django/contrib/staticfiles/finders.py +++ b/django/contrib/staticfiles/finders.py @@ -18,7 +18,7 @@ searched_locations = [] # RemovedInDjango61Warning: When the deprecation ends, remove completely. -def _check_deprecated_find_param(class_name="", find_all=False, **kwargs): +def _check_deprecated_find_param(class_name="", find_all=False, stacklevel=3, **kwargs): method_name = "find" if not class_name else f"{class_name}.find" if "all" in kwargs: legacy_all = kwargs.pop("all") @@ -26,7 +26,7 @@ def _check_deprecated_find_param(class_name="", find_all=False, **kwargs): "Passing the `all` argument to find() is deprecated. Use `find_all` " "instead." ) - warnings.warn(msg, RemovedInDjango61Warning, stacklevel=2) + warnings.warn(msg, RemovedInDjango61Warning, stacklevel=stacklevel) # If both `find_all` and `all` were given, raise TypeError. if find_all is not False: @@ -57,7 +57,7 @@ class BaseFinder: # RemovedInDjango61Warning: When the deprecation ends, remove completely. def _check_deprecated_find_param(self, **kwargs): return _check_deprecated_find_param( - class_name=self.__class__.__qualname__, **kwargs + class_name=self.__class__.__qualname__, stacklevel=4, **kwargs ) # RemovedInDjango61Warning: When the deprecation ends, replace with: diff --git a/tests/staticfiles_tests/test_finders.py b/tests/staticfiles_tests/test_finders.py index ddae508c5c..2f1863a1d4 100644 --- a/tests/staticfiles_tests/test_finders.py +++ b/tests/staticfiles_tests/test_finders.py @@ -37,11 +37,12 @@ class TestFinders: def test_find_all_deprecated_param(self): src, dst = self.find_all - with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG): + with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx: found = self.finder.find(src, all=True) found = [os.path.normcase(f) for f in found] dst = [os.path.normcase(d) for d in dst] self.assertEqual(found, dst) + self.assertEqual(ctx.filename, __file__) def test_find_all_conflicting_params(self): src, dst = self.find_all @@ -50,10 +51,11 @@ class TestFinders: "argument 'find_all'" ) with ( - self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG), + self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, self.assertRaisesMessage(TypeError, msg), ): self.finder.find(src, find_all=True, all=True) + self.assertEqual(ctx.filename, __file__) def test_find_all_unexpected_params(self): src, dst = self.find_all @@ -62,10 +64,11 @@ class TestFinders: "argument 'wrong'" ) with ( - self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG), + self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, self.assertRaisesMessage(TypeError, msg), ): self.finder.find(src, all=True, wrong=1) + self.assertEqual(ctx.filename, __file__) with self.assertRaisesMessage(TypeError, msg): self.finder.find(src, find_all=True, wrong=1) @@ -165,28 +168,31 @@ class TestMiscFinder(SimpleTestCase): ) def test_searched_locations_deprecated_all(self): - with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG): + with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx: finders.find("spam", all=True) self.assertEqual( finders.searched_locations, [os.path.join(TEST_ROOT, "project", "documents")], ) + self.assertEqual(ctx.filename, __file__) def test_searched_locations_conflicting_params(self): msg = "find() got multiple values for argument 'find_all'" with ( - self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG), + self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, self.assertRaisesMessage(TypeError, msg), ): finders.find("spam", find_all=True, all=True) + self.assertEqual(ctx.filename, __file__) def test_searched_locations_unexpected_params(self): msg = "find() got an unexpected keyword argument 'wrong'" with ( - self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG), + self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx, self.assertRaisesMessage(TypeError, msg), ): finders.find("spam", all=True, wrong=1) + self.assertEqual(ctx.filename, __file__) with self.assertRaisesMessage(TypeError, msg): finders.find("spam", find_all=True, wrong=1)