Skip to content

Commit

Permalink
fix(dependency_getter): avoid error on empty dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Jul 4, 2024
1 parent 9ffb0eb commit 8999134
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/deptry/dependency_getter/pep_621.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get(self) -> DependenciesExtract:

def _get_dependencies(self) -> list[Dependency]:
pyproject_data = load_pyproject_toml(self.config)
dependency_strings: list[str] = pyproject_data["project"]["dependencies"]
dependency_strings: list[str] = pyproject_data["project"].get("dependencies", [])
return self._extract_pep_508_dependencies(dependency_strings, self.package_module_name_map)

def _get_optional_dependencies(self) -> dict[str, list[Dependency]]:
Expand Down
2 changes: 1 addition & 1 deletion python/deptry/dependency_getter/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get(self) -> DependenciesExtract:

def _get_poetry_dependencies(self) -> list[Dependency]:
pyproject_data = load_pyproject_toml(self.config)
dependencies: dict[str, Any] = pyproject_data["tool"]["poetry"]["dependencies"]
dependencies: dict[str, Any] = pyproject_data["tool"]["poetry"].get("dependencies", {})
return self._get_dependencies(dependencies, self.package_module_name_map)

def _get_poetry_dev_dependencies(self) -> list[Dependency]:
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/dependency_getter/test_pep_621.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ def test_dependency_getter_with_incorrect_dev_group(tmp_path: Path, caplog: LogC

assert dependencies[2].name == "barfoo"
assert "barfoo" in dependencies[2].top_levels


def test_dependency_getter_empty_dependencies(tmp_path: Path) -> None:
fake_pyproject_toml = """[project]
# PEP 621 project metadata
# See https://www.python.org/dev/peps/pep-0621/
"""

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

getter = PEP621DependencyGetter(config=Path("pyproject.toml"))
dependencies_extract = getter.get()

assert len(dependencies_extract.dependencies) == 0
assert len(dependencies_extract.dev_dependencies) == 0
20 changes: 20 additions & 0 deletions tests/unit/dependency_getter/test_poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

def test_dependency_getter(tmp_path: Path) -> None:
fake_pyproject_toml = """
[tool.poetry]
name = "foo"
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
bar = { version = ">=2.5.1,<4.0.0", python = ">3.7" }
Expand Down Expand Up @@ -68,3 +71,20 @@ def test_dependency_getter(tmp_path: Path) -> None:

assert dev_dependencies[5].name == "pytest-cov"
assert "pytest_cov" in dev_dependencies[5].top_levels


def test_dependency_getter_empty_dependencies(tmp_path: Path) -> None:
fake_pyproject_toml = """
[tool.poetry]
name = "foo"
"""

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

getter = PoetryDependencyGetter(config=Path("pyproject.toml"))
dependencies_extract = getter.get()

assert len(dependencies_extract.dependencies) == 0
assert len(dependencies_extract.dev_dependencies) == 0

0 comments on commit 8999134

Please sign in to comment.