Skip to content

Commit

Permalink
fix(dependency_getter): handle strings for setuptools dynamic depen…
Browse files Browse the repository at this point in the history
…dencies (#945)
  • Loading branch information
mkniewallner authored Nov 15, 2024
1 parent 40765df commit 2a9da88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
13 changes: 9 additions & 4 deletions python/deptry/dependency_getter/pep621/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
40 changes: 37 additions & 3 deletions tests/unit/dependency_getter/test_pep_621.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def test_dependency_getter_with_setuptools_dynamic_dependencies(tmp_path: Path)
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"] }
"""

Expand Down Expand Up @@ -245,7 +246,8 @@ def test_dependency_getter_with_setuptools_dynamic_dependencies_without_build_ba
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"] }
"""

Expand Down Expand Up @@ -286,7 +288,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"] }
"""

Expand Down Expand Up @@ -344,3 +347,34 @@ def test_dependency_getter_with_setuptools_dynamic_dependencies_only_optional(tm

assert dependencies[0].name == "foo"
assert dev_dependencies[0].name == "dev-dep"


def test_dependency_getter_with_setuptools_dynamic_dependencies_only_dependencies(tmp_path: Path) -> None:
fake_pyproject_toml = """[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "foo"
dependencies = ["foo==1.2.3"]
dynamic = ["dependencies"]
[tool.setuptools.dynamic]
dependencies = { file = "requirements.txt" }
"""

with run_within_dir(tmp_path):
with Path("pyproject.toml").open("w") as f:
f.write(fake_pyproject_toml)

with Path("requirements.txt").open("w") as f:
f.write("foo==1.2.3")

extract = PEP621DependencyGetter(config=Path("pyproject.toml"), pep621_dev_dependency_groups=("dev",)).get()
dependencies = extract.dependencies
dev_dependencies = extract.dev_dependencies

assert len(dependencies) == 1
assert len(dev_dependencies) == 0

assert dependencies[0].name == "foo"

0 comments on commit 2a9da88

Please sign in to comment.