From ecfe95df528d65b6aa3a00ab167fc0ee0f15cd92 Mon Sep 17 00:00:00 2001 From: Reinout van Rees Date: Wed, 10 Apr 2024 18:34:12 +0200 Subject: [PATCH 1/2] Django settings: look at MIDDLEWARE and so in addition to INSTALLED_APPS --- z3c/dependencychecker/modules.py | 8 +++++--- .../tests/test_modules_django_settings.py | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/z3c/dependencychecker/modules.py b/z3c/dependencychecker/modules.py index b5a84d1..d55c7b1 100644 --- a/z3c/dependencychecker/modules.py +++ b/z3c/dependencychecker/modules.py @@ -415,7 +415,7 @@ def create_from_files(cls, top_dir): def scan(self): for node in ast.walk(self._get_tree()): if isinstance(node, ast.Assign): - if self._is_installed_apps_assignment(node): + if self._is_apps_assignment(node): if isinstance(node.value, (ast.Tuple, ast.List)): for element in node.value.elts: if isinstance(element, ast.Str): @@ -434,11 +434,13 @@ def scan(self): ) @staticmethod - def _is_installed_apps_assignment(node): + def _is_apps_assignment(node): + # Assignment to INSTALLED_APPS and other lists of apps-like dotted paths + APPS_LIKE_LISTS = ["INSTALLED_APPS", "MIDDLEWARE", "AUTHENTICATION_BACKENDS"] if ( len(node.targets) == 1 and isinstance(node.targets[0], ast.Name) - and node.targets[0].id == "INSTALLED_APPS" + and node.targets[0].id in APPS_LIKE_LISTS ): return True diff --git a/z3c/dependencychecker/tests/test_modules_django_settings.py b/z3c/dependencychecker/tests/test_modules_django_settings.py index d646526..1aa3a86 100644 --- a/z3c/dependencychecker/tests/test_modules_django_settings.py +++ b/z3c/dependencychecker/tests/test_modules_django_settings.py @@ -13,6 +13,7 @@ APPS_ASSIGNMENT_TO_LIST_MIXED = 'INSTALLED_APPS = ["random5", 3, ]' TEST_RUNNER_ASSIGNMENT_TO_LIST = 'TEST_RUNNER = ["random6", "random7", ]' TEST_RUNNER_ASSIGNMENT_TO_STRING = 'TEST_RUNNER = "random8"' +MIDDLEWARE_ASSIGNMENT_TO_LIST = 'MIDDLEWARE = ["something"]' def _get_imports_of_python_module(folder, source): @@ -148,3 +149,11 @@ def test_apps_assignment_to_string_mixed_details(tmpdir): TEST_RUNNER_ASSIGNMENT_TO_STRING, ) assert dotted_names[0] == "random8" + + +def test_middleware_assignment_to_list(tmpdir): + dotted_names = _get_imports_of_python_module( + tmpdir, + MIDDLEWARE_ASSIGNMENT_TO_LIST, + ) + assert dotted_names == ["something"] From 7d5f4ccad24ac4a87d1951dfb8f0886d4775a449 Mon Sep 17 00:00:00 2001 From: Reinout van Rees Date: Wed, 10 Apr 2024 18:35:42 +0200 Subject: [PATCH 2/2] Updated changelog --- CHANGES.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 0c37487..04b2dfc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,6 +11,10 @@ Changelog of z3c.dependencychecker you get that error, use a different python version... [gforcada] +- Django settings: look at ``MIDDLEWARE`` and so in addition to ``INSTALLED_APPS``, this + helps to detect more required packages. + + 2.14.3 (2023-12-28) -------------------