-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve violation finder (#655)
* refactor(violations): link violation class to finders * refactor(violations): move finder to dedicated module
- Loading branch information
1 parent
a40f47b
commit af87217
Showing
10 changed files
with
100 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
import operator | ||
from typing import TYPE_CHECKING | ||
|
||
from deptry.violations import ( | ||
DEP001MissingDependenciesFinder, | ||
DEP002UnusedDependenciesFinder, | ||
DEP003TransitiveDependenciesFinder, | ||
DEP004MisplacedDevDependenciesFinder, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from typing import Mapping | ||
|
||
from deptry.dependency import Dependency | ||
from deptry.module import ModuleLocations | ||
from deptry.violations import Violation, ViolationsFinder | ||
|
||
|
||
_VIOLATIONS_FINDERS: tuple[type[ViolationsFinder], ...] = ( | ||
DEP001MissingDependenciesFinder, | ||
DEP002UnusedDependenciesFinder, | ||
DEP003TransitiveDependenciesFinder, | ||
DEP004MisplacedDevDependenciesFinder, | ||
) | ||
|
||
|
||
def find_violations( | ||
imported_modules_with_locations: list[ModuleLocations], | ||
dependencies: list[Dependency], | ||
ignore: tuple[str, ...], | ||
per_rule_ignores: Mapping[str, tuple[str, ...]], | ||
) -> list[Violation]: | ||
violations = [] | ||
|
||
for violation_finder in _VIOLATIONS_FINDERS: | ||
if violation_finder.violation.error_code not in ignore: | ||
violations.extend( | ||
violation_finder( | ||
imported_modules_with_locations, | ||
dependencies, | ||
per_rule_ignores.get(violation_finder.violation.error_code, ()), | ||
).find() | ||
) | ||
|
||
return _get_sorted_violations(violations) | ||
|
||
|
||
def _get_sorted_violations(violations: list[Violation]) -> list[Violation]: | ||
return sorted( | ||
violations, key=operator.attrgetter("location.file", "location.line", "location.column", "error_code") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
from deptry.imports.location import Location | ||
from deptry.module import Module | ||
from deptry.violations import DEP001MissingDependencyViolation, DEP004MisplacedDevDependencyViolation | ||
from deptry.violations.finder import _get_sorted_violations | ||
|
||
|
||
def test__get_sorted_violations() -> None: | ||
violations = [ | ||
DEP004MisplacedDevDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 0)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("foo.py"), 2, 0)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 0)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 3, 1)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 2, 1)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 3, 0)), | ||
] | ||
|
||
assert _get_sorted_violations(violations) == [ | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 2, 1)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 3, 0)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("bar.py"), 3, 1)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 0)), | ||
DEP004MisplacedDevDependencyViolation(Module("foo"), Location(Path("foo.py"), 1, 0)), | ||
DEP001MissingDependencyViolation(Module("foo"), Location(Path("foo.py"), 2, 0)), | ||
] |