From 947a1adf1a7d578af02a980593e75048ebe7471e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 26 Mar 2026 10:19:28 -0400 Subject: [PATCH 1/2] fix: allow flake8 as a linter too Signed-off-by: Henry Schreiner --- README.md | 2 +- src/sp_repo_review/checks/precommit.py | 17 +++++++----- tests/test_precommit.py | 36 ++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fc65f972..7ca60b55 100644 --- a/README.md +++ b/README.md @@ -394,7 +394,7 @@ Will not show up if using lefthook instead of pre-commit/prek. - [`PC160`](https://learn.scientific-python.org/development/guides/style#PC160): Uses a spell checker - [`PC170`](https://learn.scientific-python.org/development/guides/style#PC170): Uses PyGrep hooks (only needed if rST present) - [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses a markdown formatter -- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses Ruff +- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses a linter (Ruff/Flake8) - [`PC191`](https://learn.scientific-python.org/development/guides/style#PC191): Ruff show fixes if fixes enabled - [`PC192`](https://learn.scientific-python.org/development/guides/style#PC192): Ruff uses `ruff-check` instead of `ruff` (legacy) - [`PC901`](https://learn.scientific-python.org/development/guides/style#PC901): Custom pre-commit CI update message diff --git a/src/sp_repo_review/checks/precommit.py b/src/sp_repo_review/checks/precommit.py index d6c2ee5c..1fee58b4 100644 --- a/src/sp_repo_review/checks/precommit.py +++ b/src/sp_repo_review/checks/precommit.py @@ -28,7 +28,7 @@ class PreCommit: url = mk_url("style") renamed: ClassVar[dict[str, str]] = {} repos: ClassVar[set[str]] = set() - ids: ClassVar[dict[str, str]] = {} + ids: ClassVar[dict[str, set[str]]] = {} @property def describe(self) -> str: @@ -53,7 +53,7 @@ def check(cls, precommit: dict[str, Any]) -> bool | None | str: if repo in cls.repos: if cls.ids and repo in cls.ids: if any( - hook.get("id", "") == cls.ids[repo] + hook.get("id", "") in cls.ids[repo] for hook in repo_item.get("hooks", {}) ): return True @@ -83,7 +83,7 @@ class PC110(PreCommit): renamed = { "https://github.com/psf/black": "https://github.com/psf/black-pre-commit-mirror" } - ids = {"https://github.com/astral-sh/ruff-pre-commit": "ruff-format"} + ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-format"}} class PC111(PreCommit): @@ -97,12 +97,17 @@ class PC111(PreCommit): class PC190(PreCommit): - "Uses Ruff" + "Uses a linter (Ruff/Flake8)" - repos = {"https://github.com/astral-sh/ruff-pre-commit"} + repos = { + "https://github.com/astral-sh/ruff-pre-commit", + "https://github.com/pycqa/flake8", + } renamed = { - "https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit" + "https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit", + "https://gitlab.com/pycqa/flake8 ": "https://github.com/pycqa/flake8", } + ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-check", "ruff"}} class PC140(PreCommit): diff --git a/tests/test_precommit.py b/tests/test_precommit.py index 595c6bc3..ba62d3bc 100644 --- a/tests/test_precommit.py +++ b/tests/test_precommit.py @@ -76,24 +76,56 @@ def test_pc111_rename(): assert "adamchainz" in res.err_msg -def test_pc190(): +def test_pc190_ruff_check(): precommit = yaml.safe_load(""" repos: - repo: https://github.com/astral-sh/ruff-pre-commit + hooks: + - id: ruff-check + """) + assert compute_check("PC190", precommit=precommit).result + + +def test_pc190_ruff(): + precommit = yaml.safe_load(""" + repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + hooks: + - id: ruff + """) + assert compute_check("PC190", precommit=precommit).result + + +def test_pc190_flake8(): + precommit = yaml.safe_load(""" + repos: + - repo: https://github.com/pycqa/flake8 """) assert compute_check("PC190", precommit=precommit).result -def test_pc190_rename(): +def test_pc190_rename_ruff(): precommit = yaml.safe_load(""" repos: - repo: https://github.com/charliermarsh/ruff-pre-commit + hooks: + - id: ruff-check """) res = compute_check("PC190", precommit=precommit) assert not res.result assert "astral-sh" in res.err_msg +def test_pc190_rename_flake8(): + precommit = yaml.safe_load(""" + repos: + - repo: https://gitlab.com/pycqa/flake8 + """) + res = compute_check("PC190", precommit=precommit) + assert not res.result + assert "github" in res.err_msg + + def test_pc140(): precommit = yaml.safe_load(""" repos: From d4c7ba6352d9242db91b3a4375c75fe843a7f888 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 26 Mar 2026 10:50:32 -0400 Subject: [PATCH 2/2] fix: ensure PC191 doesn't trigger for flake8 Signed-off-by: Henry Schreiner --- src/sp_repo_review/checks/precommit.py | 3 +-- tests/test_precommit.py | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sp_repo_review/checks/precommit.py b/src/sp_repo_review/checks/precommit.py index 1fee58b4..a293b731 100644 --- a/src/sp_repo_review/checks/precommit.py +++ b/src/sp_repo_review/checks/precommit.py @@ -173,8 +173,7 @@ def check( # type: ignore[override] return "--show-fixes" in hook["args"] or ( ruff is not None and "show-fixes" in ruff ) - return None - return False + return None class PC192(PreCommit): diff --git a/tests/test_precommit.py b/tests/test_precommit.py index ba62d3bc..8262e0ff 100644 --- a/tests/test_precommit.py +++ b/tests/test_precommit.py @@ -246,6 +246,15 @@ def test_pc191_no_show_fixes(ruff_check: str, ruffconfig): assert "--show-fixes" in res.err_msg +def test_pc191_no_ruff(): + precommit = yaml.safe_load(""" + repos: + - repo: https://github.com/pycqa/flake8 + """) + res = compute_check("PC191", precommit=precommit, ruff={}) + assert res.result is None + + def test_pc192(): precommit = yaml.safe_load(""" repos: