From 774a1c1e4c57a644c849a6dfe979ed6d5031878a Mon Sep 17 00:00:00 2001 From: agent-hackathon <18023767+tuyentran4992@users.noreply.github.com> Date: Tue, 6 Oct 2026 13:13:11 +0000 Subject: [PATCH 1/2] fix(python): report dependency declarations deptry cannot read instead of skipping silently A project that declares its dependencies only in setup.py, setup.cfg or a Pipfile was silently never checked: deptry failed with DependencySpecificationNotFoundError, the sensor printed [] and the run was reported clean. The sensor now reports a single parse-error finding that carries one issue per existing declaration file. A project with no declaration file, and every other deptry failure, behave exactly as before. --- .../sensors/deptry_sensor.py | 36 +++++++++ ...ry_cannot_read_are_not_silently_skipped.py | 76 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py diff --git a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py index 8cd739c..54df918 100644 --- a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py +++ b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py @@ -7,6 +7,8 @@ import tempfile from pathlib import Path +UNREADABLE_DECLARATION_NAMES = ("setup.py", "setup.cfg", "Pipfile") + def run_deptry(deptry: str, report: Path) -> subprocess.CompletedProcess[str]: return subprocess.run( @@ -25,6 +27,36 @@ def deptry_found_no_declaration(result: subprocess.CompletedProcess[str]) -> boo return "DependencySpecificationNotFoundError" in result.stderr +def unreadable_declarations() -> list[Path]: + return [ + Path(name) for name in UNREADABLE_DECLARATION_NAMES if Path(name).is_file() + ] + + +def unchecked_dependencies(declarations: list[Path]) -> list[dict]: + return [ + { + "smell": "parse-error", + "details": {}, + "issues": [ + { + "key": str(declaration), + "details": { + "file": str(declaration), + "message": ( + f"dependencies declared in {declaration} are never " + "checked — deptry reads pyproject.toml or requirements " + "files only" + ), + "source": "deptry:no-declaration", + }, + } + for declaration in declarations + ], + } + ] + + def unused_dependencies(report: Path) -> list[dict]: entries = json.loads(report.read_text(encoding="utf-8")) return [entry for entry in entries if entry["error"]["code"] == "DEP002"] @@ -61,6 +93,10 @@ def main() -> int: result = run_deptry(deptry, report) if deptry_crashed(result, report): if deptry_found_no_declaration(result): + declarations = unreadable_declarations() + if declarations: + print(json.dumps(unchecked_dependencies(declarations))) + return 0 print(json.dumps([])) return 0 sys.stderr.write(result.stderr) diff --git a/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py new file mode 100644 index 0000000..3f62784 --- /dev/null +++ b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py @@ -0,0 +1,76 @@ + +from __future__ import annotations + +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +SENSOR = ( + Path(__file__).resolve().parents[1] + / "src/habit_hooks_python/sensors/deptry_sensor.py" +) + +DECLARATIONS = ( + ( + "setup.py", + "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", + ), + ( + "setup.cfg", + "[metadata]\nname = demo\nversion = 0.0.0\n\n" + "[options]\ninstall_requires =\n attrs\n", + ), + ("Pipfile", "[packages]\nattrs = '*'\n"), +) + + +def _run_sensor(tmp_path: Path, deptry: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + [sys.executable, str(SENSOR), deptry], + cwd=tmp_path, + capture_output=True, + encoding="utf-8", + errors="replace", + ) + + +@pytest.mark.parametrize( + ("declaration", "contents"), DECLARATIONS, ids=[name for name, _ in DECLARATIONS] +) +def test_dependencies_deptry_cannot_read_are_not_silently_skipped( + tmp_path: Path, deptry: str, declaration: str, contents: str +) -> None: + (tmp_path / declaration).write_text(contents, encoding="utf-8") + (tmp_path / "app.py").write_text("import rich\n", encoding="utf-8") + + result = _run_sensor(tmp_path, deptry) + + assert result.returncode == 0 + findings = json.loads(result.stdout) + assert [finding["smell"] for finding in findings] == ["parse-error"] + assert [issue["details"]["file"] for issue in findings[0]["issues"]] == [ + declaration + ] + assert findings[0]["issues"][0]["details"]["source"] == "deptry:no-declaration" + + +def test_every_unreadable_declaration_is_named(tmp_path: Path, deptry: str) -> None: + (tmp_path / "setup.py").write_text( + "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", + encoding="utf-8", + ) + (tmp_path / "Pipfile").write_text("[packages]\nattrs = '*'\n", encoding="utf-8") + (tmp_path / "app.py").write_text("import rich\n", encoding="utf-8") + + result = _run_sensor(tmp_path, deptry) + + assert result.returncode == 0 + findings = json.loads(result.stdout) + assert [finding["smell"] for finding in findings] == ["parse-error"] + assert [issue["details"]["file"] for issue in findings[0]["issues"]] == [ + "setup.py", + "Pipfile", + ] From c43d5e291494ee3b4d02407a6bc5275d8785f240 Mon Sep 17 00:00:00 2001 From: agent-hackathon <18023767+tuyentran4992@users.noreply.github.com> Date: Fri, 16 Oct 2026 01:13:43 +0000 Subject: [PATCH 2/2] fix(python): keep the deptry test within max-args and render the explanation via details.content Addresses the two review points: the parametrized test keeps three parameters (fixture contents now live in a DECLARATION_CONTENTS mapping keyed by filename, satisfying PLR0913/max-args = 3), and the parse-error finding explains itself via details.content, which the shared guides render after the file. ruff check --config pyproject.toml src tests plugins: 0 errors; pytest plugins/python/tests: 7 passed. --- .../sensors/deptry_sensor.py | 2 +- ...ry_cannot_read_are_not_silently_skipped.py | 26 ++++++++----------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py index 54df918..d97a776 100644 --- a/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py +++ b/plugins/python/src/habit_hooks_python/sensors/deptry_sensor.py @@ -43,7 +43,7 @@ def unchecked_dependencies(declarations: list[Path]) -> list[dict]: "key": str(declaration), "details": { "file": str(declaration), - "message": ( + "content": ( f"dependencies declared in {declaration} are never " "checked — deptry reads pyproject.toml or requirements " "files only" diff --git a/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py index 3f62784..316cf42 100644 --- a/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py +++ b/plugins/python/tests/test_dependencies_deptry_cannot_read_are_not_silently_skipped.py @@ -13,18 +13,12 @@ / "src/habit_hooks_python/sensors/deptry_sensor.py" ) -DECLARATIONS = ( - ( - "setup.py", - "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", - ), - ( - "setup.cfg", - "[metadata]\nname = demo\nversion = 0.0.0\n\n" - "[options]\ninstall_requires =\n attrs\n", - ), - ("Pipfile", "[packages]\nattrs = '*'\n"), -) +DECLARATION_CONTENTS = { + "setup.py": "from setuptools import setup\n\nsetup(name='demo', version='0.0.0')\n", + "setup.cfg": "[metadata]\nname = demo\nversion = 0.0.0\n\n" + "[options]\ninstall_requires =\n attrs\n", + "Pipfile": "[packages]\nattrs = '*'\n", +} def _run_sensor(tmp_path: Path, deptry: str) -> subprocess.CompletedProcess[str]: @@ -38,12 +32,14 @@ def _run_sensor(tmp_path: Path, deptry: str) -> subprocess.CompletedProcess[str] @pytest.mark.parametrize( - ("declaration", "contents"), DECLARATIONS, ids=[name for name, _ in DECLARATIONS] + "declaration", DECLARATION_CONTENTS, ids=list(DECLARATION_CONTENTS) ) def test_dependencies_deptry_cannot_read_are_not_silently_skipped( - tmp_path: Path, deptry: str, declaration: str, contents: str + tmp_path: Path, deptry: str, declaration: str ) -> None: - (tmp_path / declaration).write_text(contents, encoding="utf-8") + (tmp_path / declaration).write_text( + DECLARATION_CONTENTS[declaration], encoding="utf-8" + ) (tmp_path / "app.py").write_text("import rich\n", encoding="utf-8") result = _run_sensor(tmp_path, deptry)