refactor: fix compat with Django 5.2 Finder.find() (#1121)

* refactor: fix compat with Django 5.2 Finder.find()

* refactor: fix tests and linters
This commit is contained in:
Juro Oravec 2025-04-12 09:35:33 +02:00 committed by GitHub
parent d37291a3b6
commit ad402fc619
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 53 additions and 3 deletions

View file

@ -1,5 +1,11 @@
# Release notes # Release notes
## v0.139
#### Fix
- Fix bug: Fix compatibility with `Finder.find()` in Django 5.2 ([#1119](https://github.com/django-components/django-components/issues/1119))
## v0.138 ## v0.138
#### Fix #### Fix

View file

@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "django_components" name = "django_components"
version = "0.138" version = "0.139"
requires-python = ">=3.8, <4.0" requires-python = ">=3.8, <4.0"
description = "A way to create simple reusable template components in Django." description = "A way to create simple reusable template components in Django."
keywords = ["django", "components", "css", "js", "html"] keywords = ["django", "components", "css", "js", "html"]

View file

@ -2,6 +2,7 @@ import os
import re import re
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
from django import VERSION as DJANGO_VERSION
from django.contrib.staticfiles.finders import BaseFinder from django.contrib.staticfiles.finders import BaseFinder
from django.contrib.staticfiles.utils import get_files from django.contrib.staticfiles.utils import get_files
from django.core.checks import CheckMessage, Error, Warning from django.core.checks import CheckMessage, Error, Warning
@ -90,17 +91,32 @@ class ComponentsFileSystemFinder(BaseFinder):
return errors return errors
# NOTE: Same as `FileSystemFinder.find` # NOTE: Same as `FileSystemFinder.find`
def find(self, path: str, all: bool = False) -> Union[List[str], str]: def find(self, path: str, **kwargs: Any) -> Union[List[str], str]:
""" """
Look for files in the extra locations as defined in COMPONENTS.dirs. Look for files in the extra locations as defined in COMPONENTS.dirs.
""" """
# Handle deprecated `all` parameter:
# - In Django 5.2, the `all` parameter was deprecated in favour of `find_all`.
# - Between Django 5.2 (inclusive) and 6.1 (exclusive), the `all` parameter was still
# supported, but an error was raised if both were provided.
# - In Django 6.1, the `all` parameter was removed.
#
# See https://github.com/django/django/blob/5.2/django/contrib/staticfiles/finders.py#L58C9-L58C37
# And https://github.com/django-components/django-components/issues/1119
if DJANGO_VERSION >= (5, 2) and DJANGO_VERSION < (6, 1):
find_all = self._check_deprecated_find_param(**kwargs) # type: ignore
elif DJANGO_VERSION >= (6, 1):
find_all = kwargs.get("find_all", False)
else:
find_all = kwargs.get("all", False)
matches: List[str] = [] matches: List[str] = []
for prefix, root in self.locations: for prefix, root in self.locations:
if root not in searched_locations: if root not in searched_locations:
searched_locations.append(root) searched_locations.append(root)
matched_path = self.find_location(root, path, prefix) matched_path = self.find_location(root, path, prefix)
if matched_path: if matched_path:
if not all: if not find_all:
return matched_path return matched_path
matches.append(matched_path) matches.append(matched_path)
return matches return matches

View file

@ -1,6 +1,7 @@
import re import re
from pathlib import Path from pathlib import Path
from django.contrib.staticfiles import finders
from django.contrib.staticfiles.management.commands.collectstatic import Command from django.contrib.staticfiles.management.commands.collectstatic import Command
from django.test import SimpleTestCase from django.test import SimpleTestCase
@ -224,3 +225,30 @@ class StaticFilesFinderTests(SimpleTestCase):
self.assertListEqual(collected["unmodified"], []) self.assertListEqual(collected["unmodified"], [])
self.assertListEqual(collected["post_processed"], []) self.assertListEqual(collected["post_processed"], [])
# Handle deprecated `all` parameter:
# - In Django 5.2, the `all` parameter was deprecated in favour of `find_all`.
# - Between Django 5.2 (inclusive) and 6.1 (exclusive), the `all` parameter was still
# supported, but an error was raised if both were provided.
# - In Django 6.1, the `all` parameter was removed.
#
# See https://github.com/django/django/blob/5.2/django/contrib/staticfiles/finders.py#L58C9-L58C37
# And https://github.com/django-components/django-components/issues/1119
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
},
components_settings=COMPONENTS,
)
def test_find_compat(self):
# NOTE: This would raise an error in Django 5.2 without a fix
result = finders.find("staticfiles/staticfiles.css")
assert Path(result) == Path("./tests/components/staticfiles/staticfiles.css").resolve()