diff --git a/django/contrib/staticfiles/finders.py b/django/contrib/staticfiles/finders.py index c2087569db..80ec9e3c48 100644 --- a/django/contrib/staticfiles/finders.py +++ b/django/contrib/staticfiles/finders.py @@ -1,6 +1,5 @@ import functools import os -import warnings from django.apps import apps from django.conf import settings @@ -9,7 +8,6 @@ from django.core.checks import Error, Warning from django.core.exceptions import ImproperlyConfigured from django.core.files.storage import FileSystemStorage, Storage, default_storage from django.utils._os import safe_join -from django.utils.deprecation import RemovedInDjango61Warning from django.utils.functional import LazyObject, empty from django.utils.module_loading import import_string @@ -17,32 +15,6 @@ from django.utils.module_loading import import_string searched_locations = [] -# RemovedInDjango61Warning: When the deprecation ends, remove completely. -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") - msg = ( - "Passing the `all` argument to find() is deprecated. Use `find_all` " - "instead." - ) - warnings.warn(msg, RemovedInDjango61Warning, stacklevel=stacklevel) - - # If both `find_all` and `all` were given, raise TypeError. - if find_all is not False: - raise TypeError( - f"{method_name}() got multiple values for argument 'find_all'" - ) - - find_all = legacy_all - - if kwargs: # any remaining kwargs must be a TypeError - first = list(kwargs.keys()).pop() - raise TypeError(f"{method_name}() got an unexpected keyword argument '{first}'") - - return find_all - - class BaseFinder: """ A base file finder to be used for custom staticfiles finder classes. @@ -54,25 +26,6 @@ class BaseFinder: "configured correctly." ) - # RemovedInDjango61Warning: When the deprecation ends, remove completely. - def _check_deprecated_find_param(self, **kwargs): - return _check_deprecated_find_param( - class_name=self.__class__.__qualname__, stacklevel=4, **kwargs - ) - - # RemovedInDjango61Warning: When the deprecation ends, replace with: - # def find(self, path, find_all=False): - def find(self, path, find_all=False, **kwargs): - """ - Given a relative file path, find an absolute file path. - - If the ``find_all`` parameter is False (default) return only the first - found file path; if True, return a list of all found files paths. - """ - raise NotImplementedError( - "subclasses of BaseFinder must provide a find() method" - ) - def list(self, ignore_patterns): """ Given an optional list of paths to ignore, return a two item iterable @@ -149,15 +102,10 @@ class FileSystemFinder(BaseFinder): ) return errors - # RemovedInDjango61Warning: When the deprecation ends, replace with: - # def find(self, path, find_all=False): - def find(self, path, find_all=False, **kwargs): + def find(self, path, find_all=False): """ Look for files in the extra locations as defined in STATICFILES_DIRS. """ - # RemovedInDjango61Warning. - if kwargs: - find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs) matches = [] for prefix, root in self.locations: if root not in searched_locations: @@ -232,15 +180,10 @@ class AppDirectoriesFinder(BaseFinder): for path in utils.get_files(storage, ignore_patterns): yield path, storage - # RemovedInDjango61Warning: When the deprecation ends, replace with: - # def find(self, path, find_all=False): - def find(self, path, find_all=False, **kwargs): + def find(self, path, find_all=False): """ Look for files in the app directories. """ - # RemovedInDjango61Warning. - if kwargs: - find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs) matches = [] for app in self.apps: app_location = self.storages[app].location @@ -287,15 +230,10 @@ class BaseStorageFinder(BaseFinder): self.storage = self.storage() super().__init__(*args, **kwargs) - # RemovedInDjango61Warning: When the deprecation ends, replace with: - # def find(self, path, find_all=False): - def find(self, path, find_all=False, **kwargs): + def find(self, path, find_all=False): """ Look for files in the default file storage, if it's local. """ - # RemovedInDjango61Warning. - if kwargs: - find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs) try: self.storage.path("") except NotImplementedError: @@ -336,18 +274,13 @@ class DefaultStorageFinder(BaseStorageFinder): ) -# RemovedInDjango61Warning: When the deprecation ends, replace with: -# def find(path, find_all=False): -def find(path, find_all=False, **kwargs): +def find(path, find_all=False): """ Find a static file with the given path using all enabled finders. If ``find_all`` is ``False`` (default), return the first matching absolute path (or ``None`` if no match). Otherwise return a list. """ - # RemovedInDjango61Warning. - if kwargs: - find_all = _check_deprecated_find_param(find_all=find_all, **kwargs) searched_locations[:] = [] matches = [] for finder in get_finders(): diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt index 9c7efb7575..9ed7bbd8e6 100644 --- a/docs/releases/6.1.txt +++ b/docs/releases/6.1.txt @@ -266,4 +266,5 @@ in Django 6.1. See :ref:`deprecated-features-5.2` for details on these changes, including how to remove usage of these features. -* ... +* The ``all`` parameter for the ``django.contrib.staticfiles.finders.find()`` + function is removed in favor of the ``find_all`` parameter. diff --git a/tests/staticfiles_tests/test_finders.py b/tests/staticfiles_tests/test_finders.py index 2f1863a1d4..7efe86ab6a 100644 --- a/tests/staticfiles_tests/test_finders.py +++ b/tests/staticfiles_tests/test_finders.py @@ -4,15 +4,10 @@ from django.conf import settings from django.contrib.staticfiles import finders, storage from django.core.exceptions import ImproperlyConfigured from django.test import SimpleTestCase, override_settings -from django.utils.deprecation import RemovedInDjango61Warning from .cases import StaticFilesTestCase from .settings import TEST_ROOT -DEPRECATION_MSG = ( - "Passing the `all` argument to find() is deprecated. Use `find_all` instead." -) - class TestFinders: """ @@ -35,47 +30,6 @@ class TestFinders: dst = [os.path.normcase(d) for d in dst] self.assertEqual(found, dst) - def test_find_all_deprecated_param(self): - src, dst = self.find_all - 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 - msg = ( - f"{self.finder.__class__.__qualname__}.find() got multiple values for " - "argument 'find_all'" - ) - with ( - 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 - msg = ( - f"{self.finder.__class__.__qualname__}.find() got an unexpected keyword " - "argument 'wrong'" - ) - with ( - 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) - - with self.assertRaisesMessage(TypeError, msg): - self.finder.find(src, wrong=1) - class TestFileSystemFinder(TestFinders, StaticFilesTestCase): """ @@ -167,39 +121,6 @@ class TestMiscFinder(SimpleTestCase): [os.path.join(TEST_ROOT, "project", "documents")], ) - def test_searched_locations_deprecated_all(self): - 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) 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) 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) - - with self.assertRaisesMessage(TypeError, msg): - finders.find("spam", wrong=1) - @override_settings(MEDIA_ROOT="") def test_location_empty(self): msg = (