Skip to content

Commit 236a596

Browse files
Move to standard pre-commit configuration where possible (#1017)
* Move to standard pre-commit configuration where possible Revert changes to `uv`-based pre-commits to avoid pinning `uv` * Handle aliases for `ruff-check` * Modify tests to reflect new expected behaviour * Modify tests to reflect new expected behaviour
1 parent 3d662cb commit 236a596

16 files changed

Lines changed: 134 additions & 181 deletions

File tree

.pre-commit-config.yaml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,37 @@ repos:
33
rev: v0.4.0
44
hooks:
55
- id: sync-with-uv
6-
- repo: https://github.com/astral-sh/uv-pre-commit
7-
rev: 0.9.1
6+
- repo: local
87
hooks:
98
- id: uv-lock
9+
name: uv-lock
10+
entry: uv lock --offline
11+
files: ^(uv\.lock|pyproject\.toml|uv\.toml)$
12+
language: system
13+
always_run: true
14+
pass_filenames: false
15+
- id: uv-sync
16+
name: uv-sync
17+
entry: uv sync --no-active --offline
18+
args: [--locked]
19+
language: system
20+
always_run: true
21+
pass_filenames: false
22+
stages: [manual, pre-commit, pre-push, post-checkout, post-merge, post-rewrite]
1023
- id: uv-export
11-
- id: uv-export
24+
name: uv-export
25+
files: ^uv\.lock$
26+
entry: uv export --frozen --offline --quiet -o=requirements.txt
27+
language: system
28+
pass_filenames: false
29+
require_serial: true
30+
- id: uv-export-docs
1231
name: uv-export-docs
13-
args: [--only-group, doc, -o=docs/requirements.txt]
14-
- id: uv-sync
32+
files: ^uv\.lock$
33+
entry: uv export --frozen --offline --quiet --only-group doc -o='docs/requirements.txt'
34+
language: system
35+
pass_filenames: false
36+
require_serial: true
1537
- repo: https://github.com/pre-commit/pre-commit-hooks
1638
rev: v5.0.0
1739
hooks:

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was autogenerated by uv via the following command:
2-
# uv export --only-group doc -o=docs/requirements.txt
2+
# uv export --frozen --offline --only-group doc -o=docs/requirements.txt
33
babel==2.17.0 \
44
--hash=sha256:0c54cffb19f690cdcc52a3b50bcbf71e07a808d1c80d549f2459b9d2cf0afb9d \
55
--hash=sha256:4d0b53093fdfb4b21c92b5213dba5a1b23885afa8383709427046b21c366e5f2

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ dev = [
6161
"prek>=0.2.4",
6262
"pyright[nodejs]>=1.1.399",
6363
"ruff>=0.11.5",
64-
"uv>=0.9.1",
6564
]
6665
test = [
6766
"click>=8.1.8",

requirements.txt

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was autogenerated by uv via the following command:
2-
# uv export --frozen --output-file=requirements.txt
2+
# uv export --frozen --offline -o=requirements.txt
33
-e .
44
annotated-types==0.7.0 \
55
--hash=sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53 \
@@ -950,26 +950,6 @@ urllib3==2.5.0 \
950950
--hash=sha256:3fc47733c7e419d4bc3f6b3dc2b4f890bb743906a30d56ba4a5bfa4bbff92760 \
951951
--hash=sha256:e6b01673c0fa6a13e374b50871808eb3bf7046c4b125b216f6bf1cc604cff0dc
952952
# via requests
953-
uv==0.9.1 \
954-
--hash=sha256:0a0ef25f76889ce115d761e411f895edc11198dc480cf8897d169c548617cc9e \
955-
--hash=sha256:0fbc6314a9466cb3205e99cd8ce74b247a21d4a669fd68948a343882423a644b \
956-
--hash=sha256:21901c8e1450e8b8e5261e1bd0f70b4e6e177befe7b4c45195e29e7c933dc7a9 \
957-
--hash=sha256:2b066c3a8cd5ae6836e703ace02973d1180fbfdc359eed37d7009e691f82a631 \
958-
--hash=sha256:2b483d8a5c184b425ecc750b1a7299ab3f35a0ce0d23d71dbc936a15d53cc171 \
959-
--hash=sha256:341ddf77399665b5bff418a2ba6f28b8aa2adf5214a4a0eb117ce9fa54b2146f \
960-
--hash=sha256:549e2df5f9bf89d1c89f0c7e47cfac3f7f91ca29cade8163a5e3d7caeb21cd22 \
961-
--hash=sha256:5fa03e40598ca86ce5bd1d50de764bfd2617758dfc45a80652385c53d4d4bb3c \
962-
--hash=sha256:61c336460532a9a484478021985f13dd8b2d8d79eae6cf9c5eec40e1a588363c \
963-
--hash=sha256:6afc4ac065a4128fe59de71f51cee1cca6bd4191e236a996aaf3956312cccf18 \
964-
--hash=sha256:6b96da925a8cd573d7c48fe281d7980e7dd2f14ea16fd2c781642a1eaf95d0cf \
965-
--hash=sha256:6de817ee4e2e8d69a84f335d83ac26e4abb8fecf063f0b332da4d1826cab82b8 \
966-
--hash=sha256:6fa81bdd8bb1e98d5c6cb0df82a5114a69a444c2243fdf6ee8f7bf2bcd59024e \
967-
--hash=sha256:7bd675feba773de5b4252ac152a0641c4ee6357f101adf57205168d93a325359 \
968-
--hash=sha256:ba781dfe5cd33f12bf332d582d0c31c8c97cecc022605c3fa2d7a1166f98581d \
969-
--hash=sha256:bdf3cc509a5ccef4edf776beac72d50718ff900fb55a5a9b6411f87bad972857 \
970-
--hash=sha256:c49a71cb22db8e3ba80c2b417bef8c0ad05ef41b5222ededc000f4dcb48bab92 \
971-
--hash=sha256:c9e1418972a70d2aec065015b76977cce2d1ee8e36bbe659ca5e376a773a4231 \
972-
--hash=sha256:f315a6068c5dc99aa4f50aaa52af3a1f38ea3d3ee03342d5f8439299e2d402ca
973953
watchdog==6.0.0 \
974954
--hash=sha256:07df1fdd701c5d4c8e55ef6cf55b8f0120fe1aef7ef39a1c6fc6bc2e606d517a \
975955
--hash=sha256:20ffe5b202af80ab4266dcd3e91aae72bf2da48c0d33bdb15c66658e685e94e2 \

src/usethis/_integrations/pre_commit/hooks.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"validate-pyproject",
2828
"uv-export",
2929
"pyproject-fmt",
30-
"ruff", # ruff followed by ruff-format seems to be the recommended way by Astral
30+
"ruff", # Alias used for ruff-check
31+
"ruff-check", # ruff-check followed by ruff-format seems to be the recommended way by Astral
3132
"ruff-format",
3233
"deptry",
3334
"import-linter",
@@ -246,7 +247,7 @@ def extract_hook_ids(model: JsonSchemaForPreCommitConfigYaml) -> list[str]:
246247
return hook_ids
247248

248249

249-
def _hooks_are_equivalent(hook: HookDefinition, other: HookDefinition) -> bool:
250+
def hooks_are_equivalent(hook: HookDefinition, other: HookDefinition) -> bool:
250251
"""Check if two hooks are equivalent."""
251252
if hook_ids_are_equivalent(hook.id, other.id):
252253
return True
@@ -265,6 +266,11 @@ def hook_ids_are_equivalent(hook_id: str | None, other: str | None) -> bool:
265266

266267
# Same name up to case differences
267268
if isinstance(hook_id, str) and isinstance(other, str):
268-
return hook_id.lower() == other.lower()
269+
hook_id_str = hook_id.lower()
270+
other_str = other.lower()
271+
if hook_id_str == other_str:
272+
return True
273+
if {hook_id_str, other_str} == {"ruff", "ruff-check"}:
274+
return True
269275

270276
return False

src/usethis/_tool/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ def remove_pre_commit_repo_configs(self) -> None:
258258

259259
# Remove the config for this specific tool.
260260
for hook in repo_config.hooks:
261-
if hook.id in get_hook_ids():
262-
remove_hook(hook.id)
261+
for other_hook_id in get_hook_ids():
262+
if hook.id is not None and hook_ids_are_equivalent(
263+
hook.id, other_hook_id
264+
):
265+
remove_hook(hook.id)
263266

264267
def migrate_config_to_pre_commit(self) -> None:
265268
"""Migrate the tool's configuration to pre-commit."""

src/usethis/_tool/impl/codespell.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
if TYPE_CHECKING:
2828
from usethis._io import KeyValueFileManager
2929

30+
_CODESPELL_VERSION = "v2.4.1" # Manually bump this version when necessary
31+
3032

3133
class CodespellTool(Tool):
3234
# https://github.com/codespell-project/codespell
@@ -127,7 +129,7 @@ def get_pre_commit_config(self) -> PreCommitConfig:
127129
return PreCommitConfig.from_single_repo(
128130
UriRepo(
129131
repo="https://github.com/codespell-project/codespell",
130-
rev="v2.4.1", # Manually bump this version when necessary
132+
rev=_CODESPELL_VERSION,
131133
hooks=[
132134
HookDefinition(id="codespell", additional_dependencies=["tomli"])
133135
],

src/usethis/_tool/impl/ruff.py

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
from usethis._integrations.ci.bitbucket.schema import Step as BitbucketStep
1818
from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
1919
from usethis._integrations.pre_commit.schema import (
20-
FileType,
21-
FileTypes,
2220
HookDefinition,
23-
Language,
24-
LocalRepo,
21+
UriRepo,
2522
)
2623
from usethis._tool.base import Tool
2724
from usethis._tool.config import (
@@ -38,6 +35,8 @@
3835
from usethis._io import KeyValueFileManager
3936
from usethis._tool.rule import Rule, RuleConfig
4037

38+
_RUFF_VERSION = "v0.14.0" # Manually bump this version when necessary
39+
4140

4241
class RuffTool(Tool):
4342
# https://github.com/astral-sh/ruff
@@ -243,54 +242,24 @@ def get_pre_commit_config(self) -> PreCommitConfig:
243242
if self.is_linter_used():
244243
repo_configs.append(
245244
PreCommitRepoConfig(
246-
repo=LocalRepo(
247-
repo="local",
248-
hooks=[
249-
HookDefinition(
250-
id="ruff",
251-
name="ruff",
252-
entry="uv run --frozen --offline ruff check --fix --force-exclude",
253-
language=Language("system"),
254-
types_or=FileTypes(
255-
[
256-
FileType("python"),
257-
FileType("pyi"),
258-
FileType("jupyter"),
259-
]
260-
),
261-
always_run=True,
262-
require_serial=True,
263-
),
264-
],
245+
repo=UriRepo(
246+
repo="https://github.com/astral-sh/ruff-pre-commit",
247+
rev=_RUFF_VERSION,
248+
hooks=[HookDefinition(id="ruff-check")],
265249
),
266-
requires_venv=True,
267-
)
250+
requires_venv=False,
251+
),
268252
)
269253
if self.is_formatter_used():
270254
repo_configs.append(
271255
PreCommitRepoConfig(
272-
repo=LocalRepo(
273-
repo="local",
274-
hooks=[
275-
HookDefinition(
276-
id="ruff-format",
277-
name="ruff-format",
278-
entry="uv run --frozen --offline ruff format --force-exclude",
279-
language=Language("system"),
280-
types_or=FileTypes(
281-
[
282-
FileType("python"),
283-
FileType("pyi"),
284-
FileType("jupyter"),
285-
]
286-
),
287-
always_run=True,
288-
require_serial=True,
289-
),
290-
],
256+
repo=UriRepo(
257+
repo="https://github.com/astral-sh/ruff-pre-commit",
258+
rev=_RUFF_VERSION,
259+
hooks=[HookDefinition(id="ruff-format")],
291260
),
292-
requires_venv=True,
293-
)
261+
requires_venv=False,
262+
),
294263
)
295264
return PreCommitConfig(
296265
repo_configs=repo_configs,

0 commit comments

Comments
 (0)