Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dependency_getter): avoid error on empty dependencies #752

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/deptry/dependency_getter/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def _project_contains_pyproject_toml(self) -> bool:
@staticmethod
def _project_uses_poetry(pyproject_toml: dict[str, Any]) -> bool:
try:
pyproject_toml["tool"]["poetry"]["dependencies"]
pyproject_toml["tool"]["poetry"]
logging.debug(
"pyproject.toml contains a [tool.poetry.dependencies] section, so Poetry is used to specify the"
"pyproject.toml contains a [tool.poetry] section, so Poetry is used to specify the"
" project's dependencies."
)
except KeyError:
logging.debug(
"pyproject.toml does not contain a [tool.poetry.dependencies] section, so Poetry is not used to specify"
"pyproject.toml does not contain a [tool.poetry] section, so Poetry is not used to specify"
" the project's dependencies."
)
return False
Expand Down
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
2 changes: 1 addition & 1 deletion tests/unit/dependency_getter/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_dependency_specification_not_found_raises_exception(tmp_path: Path, cap
assert caplog.messages == [
"pyproject.toml found!",
(
"pyproject.toml does not contain a [tool.poetry.dependencies] section, so Poetry is not used to specify the"
"pyproject.toml does not contain a [tool.poetry] section, so Poetry is not used to specify the"
" project's dependencies."
),
(
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
Loading