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

WIP: Start with DEP100 #736

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions python/deptry/violations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ class ViolationsFinder(ABC):
imported_modules_with_locations: A list of ModuleLocations objects representing the
modules imported by the project and their locations.
dependencies: A list of Dependency objects representing the project's dependencies.
ignored_modules: A tuple of module names to ignore when scanning for issues. Defaults to an
modules_to_ignore: A tuple of module names to ignore when scanning for issues. Defaults to an
empty tuple.
used_ignores: a list to keep track of which values in 'modules_to_ignore' were actually used. This list should
be updated during the `find()` method, so that the set difference of `modules_to_ignore` and `used_ignores`
results in a set of modules in `modules_to_ignore` that no longer need to be ignored.

"""

violation: ClassVar[type[Violation]]
imported_modules_with_locations: list[ModuleLocations]
dependencies: list[Dependency]
ignored_modules: tuple[str, ...] = ()
modules_to_ignore: tuple[str, ...] = ()
used_ignores: list[str] = []

@abstractmethod
def find(self) -> list[Violation]:
Expand Down
3 changes: 2 additions & 1 deletion python/deptry/violations/dep001_missing/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def _is_missing(self, module: Module) -> bool:
]):
return False

if module.name in self.ignored_modules:
if module.name in self.modules_to_ignore:
logging.debug("Identified module '%s' as a missing dependency, but ignoring.", module.name)
self.used_ignores.append(module.name)
return False

logging.debug("No package found to import module '%s' from. Marked as a missing dependency.", module.name)
Expand Down
3 changes: 2 additions & 1 deletion python/deptry/violations/dep002_unused/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def _is_unused(self, dependency: Dependency) -> bool:
if self._dependency_found_in_imported_modules(dependency) or self._any_of_the_top_levels_imported(dependency):
return False

if dependency.name in self.ignored_modules:
if dependency.name in self.modules_to_ignore:
logging.debug("Dependency '%s' found to be unused, but ignoring.", dependency.name)
self.used_ignores.append(dependency.name)
return False

logging.debug("Dependency '%s' does not seem to be used.", dependency.name)
Expand Down
3 changes: 2 additions & 1 deletion python/deptry/violations/dep003_transitive/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def _is_transitive(self, module: Module) -> bool:
]):
return False

if module.name in self.ignored_modules:
if module.name in self.modules_to_ignore:
self.used_ignores.append(module.name)
logging.debug("Dependency '%s' found to be a transitive dependency, but ignoring.", module.package)
return False

Expand Down
3 changes: 2 additions & 1 deletion python/deptry/violations/dep004_misplaced_dev/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def _is_development_dependency(self, module: Module, corresponding_package_name:
if not module.is_provided_by_dev_dependency or module.is_provided_by_dependency:
return False

if module.name in self.ignored_modules:
if module.name in self.modules_to_ignore:
self.used_ignores.append(module.name)
logging.debug(
"Dependency '%s' found to be a misplaced development dependency, but ignoring.",
corresponding_package_name,
Expand Down
Empty file.
29 changes: 29 additions & 0 deletions python/deptry/violations/dep100_unused_ignores/finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING

from deptry.violations.dep004_misplaced_dev.violation import DEP

if TYPE_CHECKING:
from deptry.module import Module
from deptry.violations import Violation

violation: ClassVar[type[Violation]]
imported_modules_with_locations: list[ModuleLocations]
dependencies: list[Dependency]
modules_to_ignore: tuple[str, ...] = ()
used_ignores: list[str] = []

@abstractmethod
def find(self) -> list[Violation]:
"""Find issues about dependencies."""
raise NotImplementedError()


@dataclass
class DEP100UnusedIgnoresFinder:
"""
TODO
"""
19 changes: 19 additions & 0 deletions python/deptry/violations/dep100_unused_ignores/violation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, ClassVar

from deptry.violations.base import Violation

if TYPE_CHECKING:
from deptry.module import Module


@dataclass
class DEP004MisplacedDevDependencyViolation(Violation):
error_code: ClassVar[str] = "DEP100"
error_template: ClassVar[str] = "'{name}' TODO"
issue: Module

def get_error_message(self) -> str:
return self.error_template.format(name=self.issue.name)
2 changes: 1 addition & 1 deletion tests/unit/violations/dep001_missing/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_simple_with_ignore() -> None:
)
]

assert DEP001MissingDependenciesFinder(modules_locations, dependencies, ignored_modules=("foobar",)).find() == []
assert DEP001MissingDependenciesFinder(modules_locations, dependencies, modules_to_ignore=("foobar",)).find() == []


def test_no_error() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/violations/dep002_unused/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_simple_with_ignore() -> None:
)
]

assert DEP002UnusedDependenciesFinder(modules_locations, dependencies, ignored_modules=("click",)).find() == []
assert DEP002UnusedDependenciesFinder(modules_locations, dependencies, modules_to_ignore=("click",)).find() == []


def test_top_level() -> None:
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/violations/dep003_transitive/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ def test_simple_with_ignore() -> None:
)
]

assert DEP003TransitiveDependenciesFinder(modules_locations, dependencies, ignored_modules=("foobar",)).find() == []
assert (
DEP003TransitiveDependenciesFinder(modules_locations, dependencies, modules_to_ignore=("foobar",)).find() == []
)
Loading