Skip to content

Commit

Permalink
feat: sort violations per location and error code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed May 6, 2023
1 parent d30e90a commit 61cf744
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 98 deletions.
19 changes: 13 additions & 6 deletions deptry/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
import operator
import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -97,33 +98,39 @@ def run(self) -> None:
def _find_violations(
self, imported_modules_with_locations: list[ModuleLocations], dependencies: list[Dependency]
) -> list[Violation]:
result = []
violations = []

if not self.skip_obsolete:
result.extend(
violations.extend(
ObsoleteDependenciesFinder(imported_modules_with_locations, dependencies, self.ignore_obsolete).find()
)

if not self.skip_missing:
result.extend(
violations.extend(
MissingDependenciesFinder(imported_modules_with_locations, dependencies, self.ignore_missing).find()
)

if not self.skip_transitive:
result.extend(
violations.extend(
TransitiveDependenciesFinder(
imported_modules_with_locations, dependencies, self.ignore_transitive
).find()
)

if not self.skip_misplaced_dev:
result.extend(
violations.extend(
MisplacedDevDependenciesFinder(
imported_modules_with_locations, dependencies, self.ignore_misplaced_dev
).find()
)

return result
return self._get_sorted_violations(violations)

@staticmethod
def _get_sorted_violations(violations: list[Violation]) -> list[Violation]:
return sorted(
violations, key=operator.attrgetter("location.file", "location.line", "location.column", "error_code")
)

def _get_dependencies(self, dependency_management_format: DependencyManagementFormat) -> DependenciesExtract:
if dependency_management_format is DependencyManagementFormat.POETRY:
Expand Down
124 changes: 62 additions & 62 deletions tests/functional/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ def test_cli_returns_error(poetry_project_builder: ToolSpecificProjectBuilder) -
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down Expand Up @@ -116,25 +116,25 @@ def test_cli_ignore_notebooks(project_builder: ToolSpecificProjectBuilder) -> No
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down Expand Up @@ -199,25 +199,25 @@ def test_cli_exclude(project_builder: ToolSpecificProjectBuilder) -> None:
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down Expand Up @@ -268,25 +268,25 @@ def test_cli_extend_exclude(project_builder: ToolSpecificProjectBuilder) -> None
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down Expand Up @@ -371,25 +371,25 @@ def test_cli_not_verbose(project_builder: ToolSpecificProjectBuilder) -> None:
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down Expand Up @@ -431,25 +431,25 @@ def test_cli_verbose(project_builder: ToolSpecificProjectBuilder) -> None:
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand All @@ -469,9 +469,9 @@ def test_cli_with_not_json_output(project_builder: ToolSpecificProjectBuilder) -
result.stderr
== f"Scanning 2 files...\n\n{str(Path('pyproject.toml'))}: DEP002 isort defined as a dependency but not"
f" used in the codebase\n{str(Path('pyproject.toml'))}: DEP002 requests defined as a dependency but not"
f" used in the codebase\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the"
f" dependency definitions\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev"
" dependency\nFound 4 dependency issues.\n\nFor more information, see the documentation:"
f" used in the codebase\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev"
f" dependency\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the dependency"
" definitions\nFound 4 dependency issues.\n\nFor more information, see the documentation:"
" https://fpgmaas.github.io/deptry/\n"
)

Expand All @@ -485,9 +485,9 @@ def test_cli_with_json_output(project_builder: ToolSpecificProjectBuilder) -> No
result.stderr
== f"Scanning 2 files...\n\n{str(Path('pyproject.toml'))}: DEP002 isort defined as a dependency but not"
f" used in the codebase\n{str(Path('pyproject.toml'))}: DEP002 requests defined as a dependency but not"
f" used in the codebase\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the"
f" dependency definitions\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev"
" dependency\nFound 4 dependency issues.\n\nFor more information, see the documentation:"
f" used in the codebase\n{str(Path('src/main.py'))}:4:0: DEP004 black imported but declared as a dev"
f" dependency\n{str(Path('src/main.py'))}:6:0: DEP001 white imported but missing from the dependency"
" definitions\nFound 4 dependency issues.\n\nFor more information, see the documentation:"
" https://fpgmaas.github.io/deptry/\n"
)
assert get_issues_report("deptry.json") == [
Expand Down Expand Up @@ -517,25 +517,25 @@ def test_cli_with_json_output(project_builder: ToolSpecificProjectBuilder) -> No
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down
16 changes: 8 additions & 8 deletions tests/functional/cli/test_cli_pdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ def test_cli_with_pdm(pdm_project_builder: ToolSpecificProjectBuilder) -> None:
},
{
"error": {
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
},
"module": "white",
"module": "black",
"location": {
"file": str(Path("src/main.py")),
"line": 6,
"line": 4,
"column": 0,
},
},
{
"error": {
"code": "DEP004",
"message": "black imported but declared as a dev dependency",
"code": "DEP001",
"message": "white imported but missing from the dependency definitions",
},
"module": "black",
"module": "white",
"location": {
"file": str(Path("src/main.py")),
"line": 4,
"line": 6,
"column": 0,
},
},
Expand Down
Loading

0 comments on commit 61cf744

Please sign in to comment.