1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #22712 -- Adjusted deprecation warning stacklevel in staticfiles finders.

This commit is contained in:
Simon Charette 2024-08-09 12:35:28 -04:00 committed by nessita
parent 8ee17037ae
commit 6bd5d4f705
2 changed files with 15 additions and 9 deletions

View File

@ -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:

View File

@ -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)