Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Fix): Ruff now runs on jupyter notebooks by default #850

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ lint:
disabled:
- pylint # pylint diagnostics are too strict
- semgrep
- trivy # investigating issues with shared cachedir

ignore:
- linters: [ALL]
Expand Down
17 changes: 16 additions & 1 deletion linters/ruff/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ tools:
lint:
definitions:
- name: ruff
files: [python, python-interface]
description: A Python linter and formatter
commands:
- name: lint
# As of ruff v0.6.0, ruff runs by default on jupyter notebooks
version: ">=0.6.0"
files: [python, python-interface, jupyter]
run: ruff check --cache-dir ${cachedir} --output-format json ${target}
output: sarif
parser:
runtime: python
run: python3 ${cwd}/ruff_to_sarif.py 0
batch: true
success_codes: [0, 1]
- name: lint
# As of ruff v0.1.0, --format is replaced with --output-format
version: ">=0.1.0"
files: [python, python-interface]
run: ruff check --cache-dir ${cachedir} --output-format json ${target}
output: sarif
parser:
Expand All @@ -25,6 +36,7 @@ lint:
- name: lint
# As of ruff v0.0.266, column edits are 1-indexed
version: ">=0.0.266"
files: [python, python-interface]
run: ruff check --cache-dir ${cachedir} --format json ${target}
output: sarif
parser:
Expand All @@ -33,6 +45,7 @@ lint:
batch: true
success_codes: [0, 1]
- name: lint
files: [python, python-interface]
run: ruff check --cache-dir ${cachedir} --format json ${target}
output: sarif
parser:
Expand All @@ -41,6 +54,7 @@ lint:
batch: true
success_codes: [0, 1]
- name: format
files: [python, python-interface]
output: rewrite
run: ruff format ${target}
success_codes: [0]
Expand All @@ -63,6 +77,7 @@ lint:
parse_regex: ruff ${semver}
run: ruff --version

# Not necessary if ruff>=0.6.0
- name: ruff-nbqa
description: A Python linter for Jupyter notebooks
files: [jupyter]
Expand Down
15 changes: 15 additions & 0 deletions linters/ruff/ruff.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import semver from "semver";
import { linterCheckTest, linterFmtTest } from "tests";
import { TrunkLintDriver } from "tests/driver";
import { skipOS } from "tests/utils";

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

const skipJupyterTestIf = (version?: string) => {
if (!version || !semver.valid(version)) {
// Run if version is KGV or a string, or error loudly if malformed.
return false;
}
return semver.lt(version, "0.6.0");
};

linterCheckTest({
linterName: "ruff",
namedTestPrefixes: ["basic_nb"],
skipTestIf: skipJupyterTestIf,
});

// ruff-nbqa still runs correctly on Windows, but the diagnostics are slightly different from the assertions.
linterCheckTest({
linterName: "ruff-nbqa",
Expand Down
10 changes: 10 additions & 0 deletions linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter ruff test basic_nb 1`] = `
{
"issues": [],
"lintActions": [],
"taskFailures": [],
"unformattedFiles": [],
}
`;
54 changes: 54 additions & 0 deletions linters/ruff/test_data/ruff_v0.6.0_basic_nb.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter ruff test basic_nb 1`] = `
{
"issues": [
{
"autofixOptions": [
{
"message": "Remove unused import: \`os\`",
"replacements": [
{
"filePath": "test_data/basic_nb.in.ipynb",
"length": "2",
},
],
},
],
"code": "F401",
"column": "1",
"file": "test_data/basic_nb.in.ipynb",
"issueClass": "ISSUE_CLASS_EXISTING",
"issueUrl": "https://docs.astral.sh/ruff/rules/#F401",
"level": "LEVEL_HIGH",
"line": "1",
"linter": "ruff",
"message": "\`os\` imported but unused",
"targetType": "jupyter",
},
],
"lintActions": [
{
"command": "lint",
"fileGroupName": "jupyter",
"linter": "ruff",
"paths": [
"test_data/basic_nb.in.ipynb",
],
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "lint",
"fileGroupName": "jupyter",
"linter": "ruff",
"paths": [
"test_data/basic_nb.in.ipynb",
],
"upstream": true,
"verb": "TRUNK_VERB_CHECK",
},
],
"taskFailures": [],
"unformattedFiles": [],
}
`;
Loading