diff --git a/python/deptry/violations/base.py b/python/deptry/violations/base.py index f513e9a5..9227731c 100644 --- a/python/deptry/violations/base.py +++ b/python/deptry/violations/base.py @@ -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]: diff --git a/python/deptry/violations/dep001_missing/finder.py b/python/deptry/violations/dep001_missing/finder.py index 45f18547..f9275bdc 100644 --- a/python/deptry/violations/dep001_missing/finder.py +++ b/python/deptry/violations/dep001_missing/finder.py @@ -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) diff --git a/python/deptry/violations/dep002_unused/finder.py b/python/deptry/violations/dep002_unused/finder.py index f364e39a..825c0636 100644 --- a/python/deptry/violations/dep002_unused/finder.py +++ b/python/deptry/violations/dep002_unused/finder.py @@ -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) diff --git a/python/deptry/violations/dep003_transitive/finder.py b/python/deptry/violations/dep003_transitive/finder.py index dcca3a4c..e20d3eb1 100644 --- a/python/deptry/violations/dep003_transitive/finder.py +++ b/python/deptry/violations/dep003_transitive/finder.py @@ -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 diff --git a/python/deptry/violations/dep004_misplaced_dev/finder.py b/python/deptry/violations/dep004_misplaced_dev/finder.py index 6fe506ad..415b830f 100644 --- a/python/deptry/violations/dep004_misplaced_dev/finder.py +++ b/python/deptry/violations/dep004_misplaced_dev/finder.py @@ -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, diff --git a/python/deptry/violations/dep100_unused_ignores/__init__.py b/python/deptry/violations/dep100_unused_ignores/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/deptry/violations/dep100_unused_ignores/finder.py b/python/deptry/violations/dep100_unused_ignores/finder.py new file mode 100644 index 00000000..95539642 --- /dev/null +++ b/python/deptry/violations/dep100_unused_ignores/finder.py @@ -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 + """ \ No newline at end of file diff --git a/python/deptry/violations/dep100_unused_ignores/violation.py b/python/deptry/violations/dep100_unused_ignores/violation.py new file mode 100644 index 00000000..330320b3 --- /dev/null +++ b/python/deptry/violations/dep100_unused_ignores/violation.py @@ -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) diff --git a/tests/unit/violations/dep001_missing/test_finder.py b/tests/unit/violations/dep001_missing/test_finder.py index 0c970091..41788490 100644 --- a/tests/unit/violations/dep001_missing/test_finder.py +++ b/tests/unit/violations/dep001_missing/test_finder.py @@ -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: diff --git a/tests/unit/violations/dep002_unused/test_finder.py b/tests/unit/violations/dep002_unused/test_finder.py index 14b3c4aa..8319081c 100644 --- a/tests/unit/violations/dep002_unused/test_finder.py +++ b/tests/unit/violations/dep002_unused/test_finder.py @@ -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: diff --git a/tests/unit/violations/dep003_transitive/test_finder.py b/tests/unit/violations/dep003_transitive/test_finder.py index 31090fb0..9a163a8d 100644 --- a/tests/unit/violations/dep003_transitive/test_finder.py +++ b/tests/unit/violations/dep003_transitive/test_finder.py @@ -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() == [] + )