From 88965d5778671a2c0e722c864c1d7108294904a1 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Fri, 15 Nov 2024 22:25:54 +0100 Subject: [PATCH] fix(dependency_getter): handle strings for `setuptools` dynamic dependencies --- python/deptry/dependency_getter/pep621/base.py | 13 +++++++++---- .../pyproject.toml | 3 ++- tests/unit/dependency_getter/test_pep_621.py | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/python/deptry/dependency_getter/pep621/base.py b/python/deptry/dependency_getter/pep621/base.py index 1118adfc1..d400ca78e 100644 --- a/python/deptry/dependency_getter/pep621/base.py +++ b/python/deptry/dependency_getter/pep621/base.py @@ -64,9 +64,11 @@ def _get_dependencies(self) -> list[Dependency]: if self._project_uses_setuptools(pyproject_data) and "dependencies" in pyproject_data["project"].get( "dynamic", {} ): - return get_dependencies_from_requirements_files( - pyproject_data["tool"]["setuptools"]["dynamic"]["dependencies"]["file"], self.package_module_name_map - ) + dependencies_files = pyproject_data["tool"]["setuptools"]["dynamic"]["dependencies"]["file"] + if isinstance(dependencies_files, str): + dependencies_files = [dependencies_files] + + return get_dependencies_from_requirements_files(dependencies_files, self.package_module_name_map) dependency_strings: list[str] = pyproject_data["project"].get("dependencies", []) return self._extract_pep_508_dependencies(dependency_strings) @@ -79,7 +81,10 @@ def _get_optional_dependencies(self) -> dict[str, list[Dependency]]: "dynamic", {} ): return { - group: get_dependencies_from_requirements_files(specification["file"], self.package_module_name_map) + group: get_dependencies_from_requirements_files( + [specification["file"]] if isinstance(specification["file"], str) else specification["file"], + self.package_module_name_map, + ) for group, specification in pyproject_data["tool"]["setuptools"]["dynamic"] .get("optional-dependencies", {}) .items() diff --git a/tests/fixtures/project_with_setuptools_dynamic_dependencies/pyproject.toml b/tests/fixtures/project_with_setuptools_dynamic_dependencies/pyproject.toml index 5cdd3c7b1..dbaa4a3e8 100644 --- a/tests/fixtures/project_with_setuptools_dynamic_dependencies/pyproject.toml +++ b/tests/fixtures/project_with_setuptools_dynamic_dependencies/pyproject.toml @@ -12,7 +12,8 @@ dynamic = ["dependencies", "optional-dependencies"] dependencies = { file = ["requirements.txt", "requirements-2.txt"] } [tool.setuptools.dynamic.optional-dependencies] -cli = { file = ["cli-requirements.txt"] } +# Both strings and list of strings are accepted. +cli = { file = "cli-requirements.txt" } dev = { file = ["dev-requirements.txt"] } [tool.deptry] diff --git a/tests/unit/dependency_getter/test_pep_621.py b/tests/unit/dependency_getter/test_pep_621.py index 62ff3f0a7..87f8ad49c 100644 --- a/tests/unit/dependency_getter/test_pep_621.py +++ b/tests/unit/dependency_getter/test_pep_621.py @@ -286,7 +286,8 @@ def test_dependency_getter_with_setuptools_dynamic_dependencies_with_another_bui dependencies = { file = ["requirements.txt", "requirements-2.txt"] } [tool.setuptools.dynamic.optional-dependencies] -cli = { file = ["cli-requirements.txt"] } +# Both strings and list of strings are accepted. +cli = { file = "cli-requirements.txt" } dev = { file = ["dev-requirements.txt"] } """