Skip to content
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test-unit: ## Run unit tests.
.PHONY: test-functional
test-functional: ## Run functional tests.
@echo "🚀 Running functional tests"
@uv run pytest tests/functional -n auto --dist loadgroup
@uv run pytest tests/functional

.PHONY: build
build: ## Build wheel and sdist files using maturin.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ docs = [
"mkdocs-material==9.7.2",
]
test = [
"inline-snapshot==0.32.2",
"pdm==2.26.6",
"poetry==2.3.2",
"pytest==9.0.2",
Expand Down
9 changes: 9 additions & 0 deletions python/deptry/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ def display_deptry_version(ctx: click.Context, _param: click.Parameter, value: b
is_flag=True,
help="Enable experimental support for namespace package (PEP 420) when detecting local modules (https://peps.python.org/pep-0420/).",
)
# This flag is not exposed because it is used in functional tests to have consistent output between platforms.
@click.option(
"--enforce-posix-paths",
is_flag=True,
hidden=True,
help="Enforce posix paths in reporters.",
)
@click.pass_context
def cli(
ctx: click.Context,
Expand All @@ -288,6 +295,7 @@ def cli(
pep621_dev_dependency_groups: tuple[str, ...],
optional_dependencies_dev_groups: tuple[str, ...],
experimental_namespace_package: bool,
enforce_posix_paths: bool,
) -> None:
"""Find dependency issues in your Python project.

Expand Down Expand Up @@ -325,6 +333,7 @@ def cli(
package_module_name_map=package_module_name_map,
optional_dependencies_dev_groups=pep621_dev_dependency_groups or optional_dependencies_dev_groups,
experimental_namespace_package=experimental_namespace_package,
enforce_posix_paths=enforce_posix_paths,
).run()


Expand Down
11 changes: 8 additions & 3 deletions python/deptry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Core:
package_module_name_map: Mapping[str, tuple[str, ...]]
optional_dependencies_dev_groups: tuple[str, ...]
experimental_namespace_package: bool
enforce_posix_paths: bool
github_output: bool
github_warning_errors: tuple[str, ...]

Expand Down Expand Up @@ -84,13 +85,17 @@ def run(self) -> None:
self.per_rule_ignores,
standard_library_modules,
)
TextReporter(violations, use_ansi=not self.no_ansi).report()
TextReporter(violations, enforce_posix_paths=self.enforce_posix_paths, use_ansi=not self.no_ansi).report()

if self.json_output:
JSONReporter(violations, self.json_output).report()
JSONReporter(
violations, enforce_posix_paths=self.enforce_posix_paths, json_output=self.json_output
).report()

if self.github_output:
GithubReporter(violations, warning_ids=self.github_warning_errors).report()
GithubReporter(
violations, enforce_posix_paths=self.enforce_posix_paths, warning_ids=self.github_warning_errors
).report()

self._exit(violations)

Expand Down
8 changes: 8 additions & 0 deletions python/deptry/reporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path

from deptry.violations import Violation


Expand All @@ -13,7 +15,13 @@ class Reporter(ABC):
"""Base class for all violation reporters."""

violations: list[Violation]
enforce_posix_paths: bool

@abstractmethod
def report(self) -> None:
raise NotImplementedError()

def _format_path(self, path: Path) -> str:
if self.enforce_posix_paths:
return str(path.as_posix())
return str(path)
4 changes: 2 additions & 2 deletions python/deptry/reporters/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def _log_violations(self, violations: list[Violation]) -> None:

def _print_github_annotation(self, violation: Violation) -> None:
annotation_severity = "warning" if violation.error_code in self.warning_ids else "error"
file_name = violation.location.file

ret = _build_workflow_command(
annotation_severity,
violation.error_code,
violation.get_error_message(),
str(file_name),
self._format_path(violation.location.file),
# For dependency files (like "pyproject.toml"), we don't extract a line. Setting the first line in that case
# allows a comment to be added in GitHub, even if it's not on the proper line, otherwise it doesn't appear
# at all.
line=violation.location.line or 1,
column=violation.location.column,
)

logging.info(ret)


Expand Down
2 changes: 1 addition & 1 deletion python/deptry/reporters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def report(self) -> None:
},
"module": violation.issue.name,
"location": {
"file": str(violation.location.file),
"file": self._format_path(violation.location.file),
"line": violation.location.line,
"column": violation.location.column,
},
Expand Down
6 changes: 4 additions & 2 deletions python/deptry/reporters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ def _format_error(self, violation: Violation) -> str:
)

def _format_location(self, location: Location) -> str:
path = self._format_path(location.file)

if location.line is not None and location.column is not None:
return self._stylize(
"{BOLD}{file}{RESET}{CYAN}:{RESET}{line}{CYAN}:{RESET}{column}",
file=location.file,
file=path,
line=location.line,
column=location.column,
)
return self._stylize("{BOLD}{file}{RESET}", file=location.file)
return self._stylize("{BOLD}{file}{RESET}", file=path)

def _stylize(self, text: str, **kwargs: Any) -> str:
return text.format(**kwargs, **self._get_colors())
Expand Down
Loading