Skip to content

Commit fc13c8e

Browse files
authored
ENH: determine dependency group for pre-commit (#116)
* DX: run `pyright` with `venv` * ENH: use sparce checkout in `get-skipped-pre-commit-hooks` * FIX: install `PyYAML` in `style group * FIX: install `style` group in `dev` dependencies
1 parent 9bd72cc commit fc13c8e

File tree

6 files changed

+80
-8
lines changed

6 files changed

+80
-8
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"noreply",
5050
"numprocesses",
5151
"pyright",
52+
"rtoml",
5253
"taplo",
5354
"tomllib"
5455
],

.github/workflows/pre-commit.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ on:
1919

2020
env:
2121
DISABLE_PRE_COMMIT_UV_PATCH: True
22+
FORCE_COLOR: true
2223

2324
jobs:
2425
determine-hooks:
@@ -51,10 +52,6 @@ jobs:
5152
**/pyproject.toml
5253
**/uv.lock
5354
enable-cache: true
54-
- name: Determine if repository is Python package
55-
run: |
56-
uv_command=$(uv pip install -e '.[sty]' > /dev/null && uv sync --group style --no-dev && echo "uv run --group style --no-dev" || echo uvx)
57-
echo "UV_COMMAND=$uv_command" | tee -a "$GITHUB_ENV"
5855
- name: Fetch pre-commit cache
5956
uses: actions/cache@v4
6057
with:
@@ -63,8 +60,10 @@ jobs:
6360
restore-keys: |
6461
pre-commit-${{ runner.os }}-py${{ inputs.python-version }}
6562
path: ~/.cache/pre-commit/
63+
- id: uv-run
64+
uses: ComPWA/actions/run-pre-commit@v2
6665
- if: needs.determine-hooks.outputs.skipped-hooks == 'ALL'
67-
run: ${{ env.UV_COMMAND }} --with pre-commit-uv pre-commit run --all-files --color always
66+
run: ${{ steps.uv-run.outputs.cmd }} --with pre-commit-uv pre-commit run --all-files
6867
- if: needs.determine-hooks.outputs.skipped-hooks != 'ALL'
6968
name: Run pre-commit hooks that don't work on pre-commit.ci
7069
run: |-
@@ -73,7 +72,7 @@ jobs:
7372
export PRETTIER_LEGACY_CLI=1
7473
fi
7574
for hook in $skipped_hooks; do
76-
${{ env.UV_COMMAND }} --with pre-commit-uv pre-commit run $hook --all-files --color always
75+
${{ steps.uv-run.outputs.cmd }} --with pre-commit-uv pre-commit run $hook --all-files
7776
done
7877
- id: diff
7978
if: always()

get-skipped-pre-commit-hooks/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ runs:
1515
using: composite
1616
steps:
1717
- uses: actions/checkout@v4
18+
with:
19+
sparse-checkout: .pre-commit-config.yaml
1820
- uses: actions/setup-python@v5
1921
with:
2022
python-version: "3.12"

pyproject.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ content-type = "text/markdown"
1515
file = "README.md"
1616

1717
[dependency-groups]
18-
dev = ["ruff"]
19-
style = ["packaging"]
18+
dev = [
19+
"ruff",
20+
{include-group = "style"},
21+
]
22+
style = [
23+
"PyYAML",
24+
"packaging",
25+
"rtoml",
26+
]
2027

2128
[tool.setuptools]
2229
include-package-data = false
@@ -32,6 +39,8 @@ reportUnknownMemberType = false
3239
reportUnknownParameterType = false
3340
reportUnknownVariableType = false
3441
typeCheckingMode = "strict"
42+
venv = ".venv"
43+
venvPath = "."
3544

3645
[tool.ruff]
3746
preview = true

run-pre-commit/action.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Run pre-commit hooks with style environment
2+
description: >-
3+
Run local pre-commit hooks that may require the installation of a virtual environment.
4+
5+
outputs:
6+
cmd:
7+
description: >-
8+
The uv run command to use for `pre-commit`
9+
value: ${{ steps.uv-run.outputs.cmd }}
10+
11+
runs:
12+
using: composite
13+
steps:
14+
- env:
15+
UV_SYSTEM_PYTHON: 1
16+
id: uv-run
17+
name: Determine skipped hooks
18+
run: echo "cmd=$(uv run -p3.12 $GITHUB_ACTION_PATH/main.py)" | tee -a $GITHUB_OUTPUT
19+
shell: bash

run-pre-commit/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Determine which pre-commit hooks should be skipped."""
2+
3+
# /// script
4+
# dependencies = ["rtoml"]
5+
# ///
6+
from pathlib import Path
7+
8+
import rtoml
9+
10+
11+
def main() -> None:
12+
cmd = _get_uv_run_command()
13+
print(cmd)
14+
15+
16+
def _get_uv_run_command() -> str:
17+
pyproject_path = Path("pyproject.toml")
18+
if pyproject_path.is_file():
19+
pyproject = rtoml.load(pyproject_path)
20+
dependency_groups = pyproject.get("dependency-groups")
21+
candidate_groups = [
22+
"style",
23+
"sty",
24+
"lint",
25+
]
26+
if dependency_groups is not None:
27+
for group in candidate_groups:
28+
if group in dependency_groups:
29+
return f"uv run --group {group} --no-dev"
30+
project_table = pyproject.get("project", {})
31+
extras = project_table.get("optional-dependencies")
32+
if extras is not None:
33+
for extra in candidate_groups:
34+
if extra in extras:
35+
return f"uv run --extra {extra} --no-dev"
36+
if "dependencies" in project_table:
37+
return "uv run --no-dev"
38+
return "uvx"
39+
40+
41+
if __name__ == "__main__":
42+
raise SystemExit(main())

0 commit comments

Comments
 (0)