Skip to content

Commit 65b8c1f

Browse files
authored
(Fix): Ruff now runs on jupyter notebooks by default (#850)
New in [v0.6.0](https://github.com/astral-sh/ruff/releases/tag/0.6.0). Added test and new versioned subcommand. `ruff-nbqa` basically becomes obsolete at this point. Also disabled trivy in this repo
1 parent bd59b6a commit 65b8c1f

5 files changed

Lines changed: 96 additions & 1 deletion

File tree

.trunk/trunk.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ lint:
4646
disabled:
4747
- pylint # pylint diagnostics are too strict
4848
- semgrep
49+
- trivy # investigating issues with shared cachedir
4950

5051
ignore:
5152
- linters: [ALL]

linters/ruff/plugin.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ tools:
99
lint:
1010
definitions:
1111
- name: ruff
12-
files: [python, python-interface]
1312
description: A Python linter and formatter
1413
commands:
14+
- name: lint
15+
# As of ruff v0.6.0, ruff runs by default on jupyter notebooks
16+
version: ">=0.6.0"
17+
files: [python, python-interface, jupyter]
18+
run: ruff check --cache-dir ${cachedir} --output-format json ${target}
19+
output: sarif
20+
parser:
21+
runtime: python
22+
run: python3 ${cwd}/ruff_to_sarif.py 0
23+
batch: true
24+
success_codes: [0, 1]
1525
- name: lint
1626
# As of ruff v0.1.0, --format is replaced with --output-format
1727
version: ">=0.1.0"
28+
files: [python, python-interface]
1829
run: ruff check --cache-dir ${cachedir} --output-format json ${target}
1930
output: sarif
2031
parser:
@@ -25,6 +36,7 @@ lint:
2536
- name: lint
2637
# As of ruff v0.0.266, column edits are 1-indexed
2738
version: ">=0.0.266"
39+
files: [python, python-interface]
2840
run: ruff check --cache-dir ${cachedir} --format json ${target}
2941
output: sarif
3042
parser:
@@ -33,6 +45,7 @@ lint:
3345
batch: true
3446
success_codes: [0, 1]
3547
- name: lint
48+
files: [python, python-interface]
3649
run: ruff check --cache-dir ${cachedir} --format json ${target}
3750
output: sarif
3851
parser:
@@ -41,6 +54,7 @@ lint:
4154
batch: true
4255
success_codes: [0, 1]
4356
- name: format
57+
files: [python, python-interface]
4458
output: rewrite
4559
run: ruff format ${target}
4660
success_codes: [0]
@@ -63,6 +77,7 @@ lint:
6377
parse_regex: ruff ${semver}
6478
run: ruff --version
6579

80+
# Not necessary if ruff>=0.6.0
6681
- name: ruff-nbqa
6782
description: A Python linter for Jupyter notebooks
6883
files: [jupyter]

linters/ruff/ruff.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
import semver from "semver";
12
import { linterCheckTest, linterFmtTest } from "tests";
23
import { TrunkLintDriver } from "tests/driver";
34
import { skipOS } from "tests/utils";
45

56
linterCheckTest({ linterName: "ruff", namedTestPrefixes: ["basic", "interface"] });
67

8+
const skipJupyterTestIf = (version?: string) => {
9+
if (!version || !semver.valid(version)) {
10+
// Run if version is KGV or a string, or error loudly if malformed.
11+
return false;
12+
}
13+
return semver.lt(version, "0.6.0");
14+
};
15+
16+
linterCheckTest({
17+
linterName: "ruff",
18+
namedTestPrefixes: ["basic_nb"],
19+
skipTestIf: skipJupyterTestIf,
20+
});
21+
722
// ruff-nbqa still runs correctly on Windows, but the diagnostics are slightly different from the assertions.
823
linterCheckTest({
924
linterName: "ruff-nbqa",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Testing linter ruff test basic_nb 1`] = `
4+
{
5+
"issues": [],
6+
"lintActions": [],
7+
"taskFailures": [],
8+
"unformattedFiles": [],
9+
}
10+
`;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Testing linter ruff test basic_nb 1`] = `
4+
{
5+
"issues": [
6+
{
7+
"autofixOptions": [
8+
{
9+
"message": "Remove unused import: \`os\`",
10+
"replacements": [
11+
{
12+
"filePath": "test_data/basic_nb.in.ipynb",
13+
"length": "2",
14+
},
15+
],
16+
},
17+
],
18+
"code": "F401",
19+
"column": "1",
20+
"file": "test_data/basic_nb.in.ipynb",
21+
"issueClass": "ISSUE_CLASS_EXISTING",
22+
"issueUrl": "https://docs.astral.sh/ruff/rules/#F401",
23+
"level": "LEVEL_HIGH",
24+
"line": "1",
25+
"linter": "ruff",
26+
"message": "\`os\` imported but unused",
27+
"targetType": "jupyter",
28+
},
29+
],
30+
"lintActions": [
31+
{
32+
"command": "lint",
33+
"fileGroupName": "jupyter",
34+
"linter": "ruff",
35+
"paths": [
36+
"test_data/basic_nb.in.ipynb",
37+
],
38+
"verb": "TRUNK_VERB_CHECK",
39+
},
40+
{
41+
"command": "lint",
42+
"fileGroupName": "jupyter",
43+
"linter": "ruff",
44+
"paths": [
45+
"test_data/basic_nb.in.ipynb",
46+
],
47+
"upstream": true,
48+
"verb": "TRUNK_VERB_CHECK",
49+
},
50+
],
51+
"taskFailures": [],
52+
"unformattedFiles": [],
53+
}
54+
`;

0 commit comments

Comments
 (0)