Skip to content

Commit bdb0908

Browse files
authored
fix: allow flake8 as a linter too (#757)
* fix: allow flake8 as a linter too Signed-off-by: Henry Schreiner <henryfs@princeton.edu> * fix: ensure PC191 doesn't trigger for flake8 Signed-off-by: Henry Schreiner <henryfs@princeton.edu> --------- Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent e2362b3 commit bdb0908

3 files changed

Lines changed: 56 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ Will not show up if using lefthook instead of pre-commit/prek.
394394
- [`PC160`](https://learn.scientific-python.org/development/guides/style#PC160): Uses a spell checker
395395
- [`PC170`](https://learn.scientific-python.org/development/guides/style#PC170): Uses PyGrep hooks (only needed if rST present)
396396
- [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses a markdown formatter
397-
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses Ruff
397+
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses a linter (Ruff/Flake8)
398398
- [`PC191`](https://learn.scientific-python.org/development/guides/style#PC191): Ruff show fixes if fixes enabled
399399
- [`PC192`](https://learn.scientific-python.org/development/guides/style#PC192): Ruff uses `ruff-check` instead of `ruff` (legacy)
400400
- [`PC901`](https://learn.scientific-python.org/development/guides/style#PC901): Custom pre-commit CI update message

src/sp_repo_review/checks/precommit.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PreCommit:
2828
url = mk_url("style")
2929
renamed: ClassVar[dict[str, str]] = {}
3030
repos: ClassVar[set[str]] = set()
31-
ids: ClassVar[dict[str, str]] = {}
31+
ids: ClassVar[dict[str, set[str]]] = {}
3232

3333
@property
3434
def describe(self) -> str:
@@ -53,7 +53,7 @@ def check(cls, precommit: dict[str, Any]) -> bool | None | str:
5353
if repo in cls.repos:
5454
if cls.ids and repo in cls.ids:
5555
if any(
56-
hook.get("id", "") == cls.ids[repo]
56+
hook.get("id", "") in cls.ids[repo]
5757
for hook in repo_item.get("hooks", {})
5858
):
5959
return True
@@ -83,7 +83,7 @@ class PC110(PreCommit):
8383
renamed = {
8484
"https://github.com/psf/black": "https://github.com/psf/black-pre-commit-mirror"
8585
}
86-
ids = {"https://github.com/astral-sh/ruff-pre-commit": "ruff-format"}
86+
ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-format"}}
8787

8888

8989
class PC111(PreCommit):
@@ -97,12 +97,17 @@ class PC111(PreCommit):
9797

9898

9999
class PC190(PreCommit):
100-
"Uses Ruff"
100+
"Uses a linter (Ruff/Flake8)"
101101

102-
repos = {"https://github.com/astral-sh/ruff-pre-commit"}
102+
repos = {
103+
"https://github.com/astral-sh/ruff-pre-commit",
104+
"https://github.com/pycqa/flake8",
105+
}
103106
renamed = {
104-
"https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit"
107+
"https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit",
108+
"https://gitlab.com/pycqa/flake8 ": "https://github.com/pycqa/flake8",
105109
}
110+
ids = {"https://github.com/astral-sh/ruff-pre-commit": {"ruff-check", "ruff"}}
106111

107112

108113
class PC140(PreCommit):
@@ -168,8 +173,7 @@ def check( # type: ignore[override]
168173
return "--show-fixes" in hook["args"] or (
169174
ruff is not None and "show-fixes" in ruff
170175
)
171-
return None
172-
return False
176+
return None
173177

174178

175179
class PC192(PreCommit):

tests/test_precommit.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,56 @@ def test_pc111_rename():
7676
assert "adamchainz" in res.err_msg
7777

7878

79-
def test_pc190():
79+
def test_pc190_ruff_check():
8080
precommit = yaml.safe_load("""
8181
repos:
8282
- repo: https://github.com/astral-sh/ruff-pre-commit
83+
hooks:
84+
- id: ruff-check
85+
""")
86+
assert compute_check("PC190", precommit=precommit).result
87+
88+
89+
def test_pc190_ruff():
90+
precommit = yaml.safe_load("""
91+
repos:
92+
- repo: https://github.com/astral-sh/ruff-pre-commit
93+
hooks:
94+
- id: ruff
95+
""")
96+
assert compute_check("PC190", precommit=precommit).result
97+
98+
99+
def test_pc190_flake8():
100+
precommit = yaml.safe_load("""
101+
repos:
102+
- repo: https://github.com/pycqa/flake8
83103
""")
84104
assert compute_check("PC190", precommit=precommit).result
85105

86106

87-
def test_pc190_rename():
107+
def test_pc190_rename_ruff():
88108
precommit = yaml.safe_load("""
89109
repos:
90110
- repo: https://github.com/charliermarsh/ruff-pre-commit
111+
hooks:
112+
- id: ruff-check
91113
""")
92114
res = compute_check("PC190", precommit=precommit)
93115
assert not res.result
94116
assert "astral-sh" in res.err_msg
95117

96118

119+
def test_pc190_rename_flake8():
120+
precommit = yaml.safe_load("""
121+
repos:
122+
- repo: https://gitlab.com/pycqa/flake8
123+
""")
124+
res = compute_check("PC190", precommit=precommit)
125+
assert not res.result
126+
assert "github" in res.err_msg
127+
128+
97129
def test_pc140():
98130
precommit = yaml.safe_load("""
99131
repos:
@@ -214,6 +246,15 @@ def test_pc191_no_show_fixes(ruff_check: str, ruffconfig):
214246
assert "--show-fixes" in res.err_msg
215247

216248

249+
def test_pc191_no_ruff():
250+
precommit = yaml.safe_load("""
251+
repos:
252+
- repo: https://github.com/pycqa/flake8
253+
""")
254+
res = compute_check("PC191", precommit=precommit, ruff={})
255+
assert res.result is None
256+
257+
217258
def test_pc192():
218259
precommit = yaml.safe_load("""
219260
repos:

0 commit comments

Comments
 (0)