Skip to content

Commit

Permalink
Rename pyproject_toml in dependency_getter to poetry (#151)
Browse files Browse the repository at this point in the history
* refactor(dependency_getter): simplify some methods

* refactor: rename `pyproject_toml` to `poetry`

* refactor: move dependency_getter tests to own module

* refactor: move issue_finders tests to own module

* refactor(dependency_getter): make some methods static
  • Loading branch information
mkniewallner authored Oct 2, 2022
1 parent 9c886fe commit 1b12183
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 23 deletions.
6 changes: 3 additions & 3 deletions deptry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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
Empty file.
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
Empty file added tests/issue_finders/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1b12183

Please sign in to comment.