Skip to content

Commit 4bc2312

Browse files
authored
feat: check for ruff-check instead of ruff (#605)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 8f5533c commit 4bc2312

File tree

6 files changed

+74
-14
lines changed

6 files changed

+74
-14
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repos:
3131
- repo: https://github.com/astral-sh/ruff-pre-commit
3232
rev: "v0.11.12"
3333
hooks:
34-
- id: ruff
34+
- id: ruff-check
3535
args: ["--fix", "--show-fixes"]
3636
- id: ruff-format
3737

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ for family, grp in itertools.groupby(collected.checks.items(), key=lambda x: x[1
364364
- [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses a markdown formatter
365365
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses Ruff
366366
- [`PC191`](https://learn.scientific-python.org/development/guides/style#PC191): Ruff show fixes if fixes enabled
367+
- [`PC192`](https://learn.scientific-python.org/development/guides/style#PC192): Ruff uses `ruff-check` instead of `ruff` (legacy)
367368
- [`PC901`](https://learn.scientific-python.org/development/guides/style#PC901): Custom pre-commit CI message
368369

369370
### Ruff

docs/pages/guides/style.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Here is the snippet to add the formatter to your `.pre-commit-config.yml`
113113
- repo: https://github.com/astral-sh/ruff-pre-commit
114114
rev: "v0.11.12"
115115
hooks:
116-
# id: ruff would go here if using both
116+
# id: ruff-check would go here if using both
117117
- id: ruff-format
118118
```
119119

@@ -203,12 +203,14 @@ pre-commit hook.
203203
- repo: https://github.com/astral-sh/ruff-pre-commit
204204
rev: "v0.11.12"
205205
hooks:
206-
- id: ruff
206+
- id: ruff-check
207207
args: ["--fix", "--show-fixes"]
208208
```
209209
210-
{% rr PC191 %} The `--fix` argument is optional, but recommended, since you can
211-
inspect and undo changes in git.
210+
{% rr PC192 %} The hook is named `ruff-check`. {% rr PC191 %} The `--fix`
211+
argument is optional, but recommended, since you can inspect and undo changes in
212+
git. `--show-fixes` is highly recommended if `--fix` is present, otherwise it
213+
won't tell you what or why it fixed things.
212214

213215
{% rr RF001 %} Ruff is configured in your `pyproject.toml`. Here's an example:
214216

@@ -240,7 +242,6 @@ extend-select = [
240242
"YTT", # flake8-2020
241243
]
242244
ignore = [
243-
"ISC001", # Conflicts with formatter
244245
"PLR09", # Too many <...>
245246
"PLR2004", # Magic value used in comparison
246247
]

src/sp_repo_review/checks/precommit.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def check(cls, precommit: dict[str, Any]) -> bool | None:
153153
if "repo" in repo and repo["repo"].lower() in cls.repos:
154154
for hook in repo["hooks"]:
155155
if (
156-
hook["id"] == "ruff"
156+
hook["id"] in {"ruff", "ruff-check"}
157157
and "args" in hook
158158
and "--fix" in hook["args"]
159159
):
@@ -162,6 +162,34 @@ def check(cls, precommit: dict[str, Any]) -> bool | None:
162162
return False
163163

164164

165+
class PC192(PreCommit):
166+
"Ruff uses `ruff-check` instead of `ruff` (legacy)"
167+
168+
requires = {"PC190"}
169+
repos = {"https://github.com/astral-sh/ruff-pre-commit"}
170+
171+
@classmethod
172+
def check(cls, precommit: dict[str, Any]) -> bool | None:
173+
"""
174+
Use `ruff-check` instead of `ruff` (legacy).
175+
"""
176+
for repo in precommit.get("repos", {}):
177+
if (
178+
"repo" in repo
179+
and repo["repo"].lower() in cls.repos
180+
and any(hook["id"] == "ruff" for hook in repo["hooks"])
181+
):
182+
return False
183+
for repo in precommit.get("repos", {}):
184+
if (
185+
"repo" in repo
186+
and repo["repo"].lower() in cls.repos
187+
and any(hook["id"] == "ruff-check" for hook in repo["hooks"])
188+
):
189+
return True
190+
return None
191+
192+
165193
class PC901(PreCommit):
166194
"Custom pre-commit CI message"
167195

tests/test_precommit.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import pytest
12
import yaml
23
from repo_review.testing import compute_check
34

45

6+
@pytest.fixture(params=["ruff", "ruff-check"])
7+
def ruff_check(request: pytest.FixtureRequest) -> str:
8+
return request.param # type: ignore[no-any-return]
9+
10+
511
def test_pc100():
612
precommit = yaml.safe_load("""
713
repos:
@@ -132,30 +138,54 @@ def test_pc180_alt_1():
132138
assert compute_check("PC180", precommit=precommit).result
133139

134140

135-
def test_pc191():
136-
precommit = yaml.safe_load("""
141+
def test_pc191(ruff_check: str):
142+
precommit = yaml.safe_load(f"""
137143
repos:
138144
- repo: https://github.com/astral-sh/ruff-pre-commit
139145
hooks:
140-
- id: ruff
146+
- id: {ruff_check}
141147
args: ["--fix", "--show-fixes"]
142148
""")
143149
assert compute_check("PC191", precommit=precommit).result
144150

145151

146-
def test_pc191_no_show_fixes():
147-
precommit = yaml.safe_load("""
152+
def test_pc191_no_show_fixes(ruff_check: str):
153+
precommit = yaml.safe_load(f"""
148154
repos:
149155
- repo: https://github.com/astral-sh/ruff-pre-commit
150156
hooks:
151-
- id: ruff
157+
- id: {ruff_check}
152158
args: ["--fix"]
153159
""")
154160
res = compute_check("PC191", precommit=precommit)
155161
assert not res.result
156162
assert "--show-fixes" in res.err_msg
157163

158164

165+
def test_pc192():
166+
precommit = yaml.safe_load("""
167+
repos:
168+
- repo: https://github.com/astral-sh/ruff-pre-commit
169+
hooks:
170+
- id: ruff
171+
args: ["--fix"]
172+
""")
173+
res = compute_check("PC192", precommit=precommit)
174+
assert not res.result
175+
assert "ruff-check" in res.err_msg
176+
177+
178+
def test_pc192_not_needed():
179+
precommit = yaml.safe_load("""
180+
repos:
181+
- repo: https://github.com/astral-sh/ruff-pre-commit
182+
hooks:
183+
- id: ruff-format
184+
""")
185+
res = compute_check("PC192", precommit=precommit)
186+
assert res.result is None
187+
188+
159189
def test_pc901():
160190
precommit = yaml.safe_load("""
161191
ci:

{{cookiecutter.project_name}}/.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
- repo: https://github.com/astral-sh/ruff-pre-commit
4545
rev: "v0.11.12"
4646
hooks:
47-
- id: ruff
47+
- id: ruff-check
4848
args: ["--fix", "--show-fixes"]
4949
- id: ruff-format
5050

0 commit comments

Comments
 (0)