From ab32284bef8dd6296f36f7b937d20b4f6eeb0b35 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Tue, 26 Mar 2024 08:34:42 +0100 Subject: [PATCH] fix(violations): show module name for DEP003 (#644) --- .../violations/dep003_transitive/violation.py | 2 +- tests/unit/reporters/test_json.py | 8 ++++---- tests/unit/reporters/test_text.py | 18 +++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/python/deptry/violations/dep003_transitive/violation.py b/python/deptry/violations/dep003_transitive/violation.py index bdbbe87d..8ac8243f 100644 --- a/python/deptry/violations/dep003_transitive/violation.py +++ b/python/deptry/violations/dep003_transitive/violation.py @@ -16,4 +16,4 @@ class DEP003TransitiveDependencyViolation(Violation): issue: Module def get_error_message(self) -> str: - return self.error_template.format(name=self.issue.package) + return self.error_template.format(name=self.issue.name) diff --git a/tests/unit/reporters/test_json.py b/tests/unit/reporters/test_json.py index ed2dab72..044dec05 100644 --- a/tests/unit/reporters/test_json.py +++ b/tests/unit/reporters/test_json.py @@ -20,15 +20,15 @@ def test_simple(tmp_path: Path) -> None: with run_within_dir(tmp_path): JSONReporter( [ - DEP001MissingDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)), + DEP001MissingDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)), DEP002UnusedDependencyViolation( Dependency("foo", Path("pyproject.toml")), Location(Path("pyproject.toml")) ), DEP003TransitiveDependencyViolation( - Module("foo", package="foo_package"), Location(Path("foo/bar.py"), 1, 2) + Module("foo", package="foo-package"), Location(Path("foo/bar.py"), 1, 2) ), DEP004MisplacedDevDependencyViolation( - Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2) + Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2) ), ], "output.json", @@ -57,7 +57,7 @@ def test_simple(tmp_path: Path) -> None: }, }, { - "error": {"code": "DEP003", "message": "'foo_package' imported but it is a transitive dependency"}, + "error": {"code": "DEP003", "message": "'foo' imported but it is a transitive dependency"}, "module": "foo", "location": { "file": str(Path("foo/bar.py")), diff --git a/tests/unit/reporters/test_text.py b/tests/unit/reporters/test_text.py index 92c47598..396addc1 100644 --- a/tests/unit/reporters/test_text.py +++ b/tests/unit/reporters/test_text.py @@ -26,14 +26,14 @@ def test_logging_number_multiple(caplog: LogCaptureFixture) -> None: with caplog.at_level(logging.INFO): violations = [ - DEP001MissingDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)), + DEP001MissingDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)), DEP002UnusedDependencyViolation( Dependency("foo", Path("pyproject.toml")), Location(Path("pyproject.toml")) ), DEP003TransitiveDependencyViolation( - Module("foo", package="foo_package"), Location(Path("foo/bar.py"), 1, 2) + Module("foo", package="foo-package"), Location(Path("foo/bar.py"), 1, 2) ), - DEP004MisplacedDevDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)), + DEP004MisplacedDevDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)), ] TextReporter(violations).report() @@ -54,7 +54,7 @@ def test_logging_number_multiple(caplog: LogCaptureFixture) -> None: file=Path("pyproject.toml"), ), stylize( - "{BOLD}{file}{RESET}{CYAN}:{RESET}1{CYAN}:{RESET}2{CYAN}:{RESET} {BOLD}{RED}DEP003{RESET} 'foo_package'" + "{BOLD}{file}{RESET}{CYAN}:{RESET}1{CYAN}:{RESET}2{CYAN}:{RESET} {BOLD}{RED}DEP003{RESET} 'foo'" " imported but it is a transitive dependency", file=Path("foo/bar.py"), ), @@ -71,7 +71,7 @@ def test_logging_number_multiple(caplog: LogCaptureFixture) -> None: def test_logging_number_single(caplog: LogCaptureFixture) -> None: with caplog.at_level(logging.INFO): TextReporter([ - DEP001MissingDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)) + DEP001MissingDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)) ]).report() assert caplog.messages == [ @@ -99,14 +99,14 @@ def test_logging_number_none(caplog: LogCaptureFixture) -> None: def test_logging_no_ansi(caplog: LogCaptureFixture) -> None: with caplog.at_level(logging.INFO): violations = [ - DEP001MissingDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)), + DEP001MissingDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)), DEP002UnusedDependencyViolation( Dependency("foo", Path("pyproject.toml")), Location(Path("pyproject.toml")) ), DEP003TransitiveDependencyViolation( - Module("foo", package="foo_package"), Location(Path("foo/bar.py"), 1, 2) + Module("foo", package="foo-package"), Location(Path("foo/bar.py"), 1, 2) ), - DEP004MisplacedDevDependencyViolation(Module("foo", package="foo_package"), Location(Path("foo.py"), 1, 2)), + DEP004MisplacedDevDependencyViolation(Module("foo", package="foo-package"), Location(Path("foo.py"), 1, 2)), ] TextReporter(violations, use_ansi=False).report() @@ -118,7 +118,7 @@ def test_logging_no_ansi(caplog: LogCaptureFixture) -> None: "", f"{Path('foo.py')}:1:2: DEP001 'foo' imported but missing from the dependency definitions", f"{Path('pyproject.toml')}: DEP002 'foo' defined as a dependency but not used in the codebase", - f"{Path('foo/bar.py')}:1:2: DEP003 'foo_package' imported but it is a transitive dependency", + f"{Path('foo/bar.py')}:1:2: DEP003 'foo' imported but it is a transitive dependency", f"{Path('foo.py')}:1:2: DEP004 'foo' imported but declared as a dev dependency", "Found 4 dependency issues.", "\nFor more information, see the documentation: https://deptry.com/",