Skip to content

Commit 3c98d3b

Browse files
chore(ci): pin ruff and add pre-commit hooks (PROF-15511) (#174)
* style: ruff-format python_safe_point_bias README Fixes ruff format --check CI failure (extra blank line in code block). * chore(ci): pin ruff 0.15.5 and add pre-commit hooks Pin CI ruff to match pyproject.toml expectations and add a minimal pre-commit config (ruff-format + ruff --fix) so local runs match CI. * chore: add scripts/lint and scripts/format for local ruff checks Mirror dd-trace-py's scripts/lint workflow without git hooks — works with Datadog global core.hooksPath. Matches the pinned CI ruff job. * fix(ci): run ruff --fix before format for convergence ruff check --fix can rewrite imports; formatting last avoids leaving files unformatted after a single scripts/format or pre-commit run. * chore(ci): single ruff pin via requirements-dev.txt - Add requirements-dev.txt as the only ruff version pin - CI and scripts reference it; local pre-commit hook runs scripts/lint - Document Python lint setup in README * refactor(scripts): share ruff setup in ruff-common.sh
1 parent 19a0a85 commit 3c98d3b

8 files changed

Lines changed: 73 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
with:
3030
python-version: "3.11"
3131
- name: Install ruff
32-
run: pip install --no-cache-dir ruff
32+
run: pip install --no-cache-dir -r requirements-dev.txt
3333
- name: Run ruff format check
3434
run: ruff format --check .
3535
- name: Run ruff lint check

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Optional git hooks for Python scenario files (same checks as CI — see scripts/lint).
2+
# Setup: pip install -r requirements-dev.txt pre-commit && pre-commit install
3+
# On Datadog laptops with global core.hooksPath, use ./scripts/lint instead of pre-commit install.
4+
repos:
5+
- repo: local
6+
hooks:
7+
- id: ruff
8+
name: ruff (scripts/lint)
9+
entry: scripts/lint
10+
language: system
11+
types: [python]

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ Checkout #profiling-library-pager to get notified of test failures.
1010
Install go >= 1.25.1: `brew install go`, `choco install go`, etc.
1111
Install docker.
1212

13+
### Python lint (scenario workloads)
14+
15+
CI and local checks use [Ruff](https://docs.astral.sh/ruff/). Install once:
16+
17+
```sh
18+
pip install -r requirements-dev.txt
19+
```
20+
21+
```sh
22+
./scripts/lint # check (matches the ci.yml ruff job)
23+
./scripts/format # auto-fix formatting and lint
24+
```
25+
26+
Optional git hooks (skip on Datadog laptops that use global `core.hooksPath` — run `./scripts/lint` manually instead):
27+
28+
```sh
29+
pip install pre-commit
30+
pre-commit install
31+
```
32+
33+
Ruff version is pinned only in `requirements-dev.txt`.
34+
1335
### Running Tests
1436

1537
```sh

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Python dev tools for repo-wide lint/format (ci.yml, scripts/lint, scripts/format, pre-commit).
2+
ruff==0.15.5

scenarios/python_safe_point_bias_3.11/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Verifies that the Python profiler correctly attributes CPU time to `slow_method`
1010
def empty_method() -> None:
1111
pass
1212

13+
1314
def slow_method() -> None:
1415
while time() < end_time:
1516
x = "h" + "e" + "l" + "l" + "o" + ","

scripts/format

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
source "$(dirname "${BASH_SOURCE[0]}")/ruff-common.sh"
5+
ruff_common_setup
6+
ruff_resolve_targets "$@"
7+
8+
ruff check --fix "${RUFF_TARGETS[@]}"
9+
ruff format "${RUFF_TARGETS[@]}"

scripts/lint

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Matches the ruff job in .github/workflows/ci.yml (see also scripts/format).
5+
source "$(dirname "${BASH_SOURCE[0]}")/ruff-common.sh"
6+
ruff_common_setup
7+
ruff_resolve_targets "$@"
8+
9+
ruff format --check "${RUFF_TARGETS[@]}"
10+
ruff check "${RUFF_TARGETS[@]}"

scripts/ruff-common.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Shared setup for scripts/lint and scripts/format.
2+
ruff_common_setup() {
3+
RUFF_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
4+
cd "$RUFF_ROOT"
5+
6+
if ! command -v ruff >/dev/null 2>&1; then
7+
echo "ruff not found; install with: pip install -r $RUFF_ROOT/requirements-dev.txt" >&2
8+
exit 1
9+
fi
10+
}
11+
12+
ruff_resolve_targets() {
13+
RUFF_TARGETS=("$@")
14+
if [ "${#RUFF_TARGETS[@]}" -eq 0 ]; then
15+
RUFF_TARGETS=(".")
16+
fi
17+
}

0 commit comments

Comments
 (0)