diff --git a/deptry/core.py b/deptry/core.py index 9600c97e..c1afb7da 100644 --- a/deptry/core.py +++ b/deptry/core.py @@ -5,7 +5,7 @@ from typing import Dict, List, Tuple from deptry.dependency import Dependency -from deptry.dependency_getter.pyproject_toml import PyprojectTomlDependencyGetter +from deptry.dependency_getter.poetry import PoetryDependencyGetter from deptry.dependency_getter.requirements_txt import RequirementsTxtDependencyGetter from deptry.dependency_specification_detector import DependencySpecificationDetector from deptry.import_parser import ImportParser @@ -87,8 +87,8 @@ def _find_issues( def _get_dependencies(self, dependency_management_format: str) -> Tuple[List[Dependency], List[Dependency]]: if dependency_management_format == "pyproject_toml": - dependencies = PyprojectTomlDependencyGetter().get() - dev_dependencies = PyprojectTomlDependencyGetter(dev=True).get() + dependencies = PoetryDependencyGetter().get() + dev_dependencies = PoetryDependencyGetter(dev=True).get() elif dependency_management_format == "requirements_txt": dependencies = RequirementsTxtDependencyGetter(requirements_txt=self.requirements_txt).get() dev_dependencies = RequirementsTxtDependencyGetter( diff --git a/deptry/dependency_getter/pyproject_toml.py b/deptry/dependency_getter/poetry.py similarity index 67% rename from deptry/dependency_getter/pyproject_toml.py rename to deptry/dependency_getter/poetry.py index efa936f3..b73b272a 100644 --- a/deptry/dependency_getter/pyproject_toml.py +++ b/deptry/dependency_getter/poetry.py @@ -6,9 +6,9 @@ from deptry.utils import load_pyproject_toml -class PyprojectTomlDependencyGetter: +class PoetryDependencyGetter: """ - Class to get a project's list of dependencies from pyproject.toml. + Class to get Poetry dependencies from a project's pyproject.toml. Args: dev (bool): Read either the regular, or the dev dependencies, based on this argument. @@ -19,27 +19,29 @@ def __init__(self, dev: bool = False) -> None: def get(self) -> List[Dependency]: if self.dev: - pyproject_toml_dependencies = self._get_pyproject_toml_dev_dependencies() + poetry_dependencies = self._get_dev_dependencies() else: - pyproject_toml_dependencies = self._get_pyproject_toml_dependencies() + poetry_dependencies = self._get_dependencies() dependencies = [] - for dep, spec in pyproject_toml_dependencies.items(): + for dep, spec in poetry_dependencies.items(): # dep is the dependency name, spec is the version specification, e.g. "^0.2.2" or {"*", optional = true} if dep != "python": - optional = self._is_optional(dep, spec) - conditional = self._is_conditional(dep, spec) + optional = self._is_optional(spec) + conditional = self._is_conditional(spec) dependencies.append(Dependency(dep, conditional=conditional, optional=optional)) self._log_dependencies(dependencies) return dependencies - def _get_pyproject_toml_dependencies(self) -> Dict[str, Any]: + @staticmethod + def _get_dependencies() -> Dict[str, Any]: pyproject_data = load_pyproject_toml() dependencies: Dict[str, Any] = pyproject_data["tool"]["poetry"]["dependencies"] return dependencies - def _get_pyproject_toml_dev_dependencies(self) -> Dict[str, Any]: + @staticmethod + def _get_dev_dependencies() -> Dict[str, Any]: """ These can be either under; @@ -66,15 +68,11 @@ def _log_dependencies(self, dependencies: List[Dependency]) -> None: logging.debug("") @staticmethod - def _is_optional(dep: str, spec: Union[str, Dict[str, Any]]) -> bool: + def _is_optional(spec: Union[str, Dict[str, Any]]) -> bool: # if of the shape `isodate = {version = "*", optional = true}` mark as optional` - if isinstance(spec, dict) and "optional" in spec and spec["optional"]: - return True - return False + return bool(isinstance(spec, dict) and spec.get("optional")) @staticmethod - def _is_conditional(dep: str, spec: Union[str, Dict[str, Any]]) -> bool: + def _is_conditional(spec: Union[str, Dict[str, Any]]) -> bool: # if of the shape `tomli = { version = "^2.0.1", python = "<3.11" }`, mark as conditional. - if isinstance(spec, dict) and "python" in spec and "version" in spec: - return True - return False + return isinstance(spec, dict) and "python" in spec and "version" in spec diff --git a/tests/dependency_getter/__init__.py b/tests/dependency_getter/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_pyproject_toml_dependency_getter.py b/tests/dependency_getter/test_poetry.py similarity index 90% rename from tests/test_pyproject_toml_dependency_getter.py rename to tests/dependency_getter/test_poetry.py index fd228a49..9a892adb 100644 --- a/tests/test_pyproject_toml_dependency_getter.py +++ b/tests/dependency_getter/test_poetry.py @@ -1,4 +1,4 @@ -from deptry.dependency_getter.pyproject_toml import PyprojectTomlDependencyGetter +from deptry.dependency_getter.poetry import PoetryDependencyGetter from deptry.utils import run_within_dir @@ -14,7 +14,7 @@ def test_dependency_getter(tmp_path): with open("pyproject.toml", "w") as f: f.write(fake_pyproject_toml) - dependencies = PyprojectTomlDependencyGetter().get() + dependencies = PoetryDependencyGetter().get() assert len(dependencies) == 4 @@ -49,7 +49,7 @@ def test_dependency_getter_dev(tmp_path): with open("pyproject.toml", "w") as f: f.write(fake_pyproject_toml) - dependencies = PyprojectTomlDependencyGetter(dev=True).get() + dependencies = PoetryDependencyGetter(dev=True).get() assert len(dependencies) == 2 diff --git a/tests/test_requirements_txt_dependency_getter.py b/tests/dependency_getter/test_requirements_txt.py similarity index 100% rename from tests/test_requirements_txt_dependency_getter.py rename to tests/dependency_getter/test_requirements_txt.py diff --git a/tests/issue_finders/__init__.py b/tests/issue_finders/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_missing_dependencies_finder.py b/tests/issue_finders/test_missing.py similarity index 100% rename from tests/test_missing_dependencies_finder.py rename to tests/issue_finders/test_missing.py diff --git a/tests/test_obsolete_dependencies_finder.py b/tests/issue_finders/test_obsolete.py similarity index 100% rename from tests/test_obsolete_dependencies_finder.py rename to tests/issue_finders/test_obsolete.py diff --git a/tests/test_transitive_dependencies_finder.py b/tests/issue_finders/test_transitive.py similarity index 100% rename from tests/test_transitive_dependencies_finder.py rename to tests/issue_finders/test_transitive.py