Skip to content

Commit

Permalink
Merge pull request #138 from reinout/reinout-django-settings-improvem…
Browse files Browse the repository at this point in the history
…ents

Django settings improvements: also look at MIDDLEWARE and so
  • Loading branch information
reinout committed Apr 11, 2024
2 parents 6edfff0 + 7d5f4cc commit 845d5a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-------------------

Expand Down
8 changes: 5 additions & 3 deletions z3c/dependencychecker/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions z3c/dependencychecker/tests/test_modules_django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"]

0 comments on commit 845d5a7

Please sign in to comment.