From 0e199895729fcf79434a947878b77e6676541184 Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Mon, 19 Aug 2024 11:53:21 -0700 Subject: [PATCH 1/2] ruff-6 --- .trunk/trunk.yaml | 1 + linters/ruff/plugin.yaml | 17 +++++- linters/ruff/ruff.test.ts | 16 ++++++ .../test_data/ruff_v0.6.0_basic_nb.check.shot | 54 +++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 linters/ruff/test_data/ruff_v0.6.0_basic_nb.check.shot diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 7d761dc5d..09055eb7a 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -46,6 +46,7 @@ lint: disabled: - pylint # pylint diagnostics are too strict - semgrep + - trivy # investigating issues with shared cachedir ignore: - linters: [ALL] diff --git a/linters/ruff/plugin.yaml b/linters/ruff/plugin.yaml index 55fe9e39e..3d6988a2d 100644 --- a/linters/ruff/plugin.yaml +++ b/linters/ruff/plugin.yaml @@ -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: @@ -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: @@ -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: @@ -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] @@ -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] diff --git a/linters/ruff/ruff.test.ts b/linters/ruff/ruff.test.ts index 253490de7..174cd1fbc 100644 --- a/linters/ruff/ruff.test.ts +++ b/linters/ruff/ruff.test.ts @@ -1,9 +1,25 @@ +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) => { + console.log(`Jupyter version: ${version}`); + 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", diff --git a/linters/ruff/test_data/ruff_v0.6.0_basic_nb.check.shot b/linters/ruff/test_data/ruff_v0.6.0_basic_nb.check.shot new file mode 100644 index 000000000..74225b28f --- /dev/null +++ b/linters/ruff/test_data/ruff_v0.6.0_basic_nb.check.shot @@ -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": [], +} +`; From 2cc8b518e79f7be6f1676e04ad327dc42b8c566a Mon Sep 17 00:00:00 2001 From: Tyler Jang Date: Mon, 19 Aug 2024 12:28:48 -0700 Subject: [PATCH 2/2] fix test --- linters/ruff/ruff.test.ts | 1 - linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot | 10 ++++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot diff --git a/linters/ruff/ruff.test.ts b/linters/ruff/ruff.test.ts index 174cd1fbc..288de6565 100644 --- a/linters/ruff/ruff.test.ts +++ b/linters/ruff/ruff.test.ts @@ -6,7 +6,6 @@ import { skipOS } from "tests/utils"; linterCheckTest({ linterName: "ruff", namedTestPrefixes: ["basic", "interface"] }); const skipJupyterTestIf = (version?: string) => { - console.log(`Jupyter version: ${version}`); if (!version || !semver.valid(version)) { // Run if version is KGV or a string, or error loudly if malformed. return false; diff --git a/linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot b/linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot new file mode 100644 index 000000000..3fbd50f47 --- /dev/null +++ b/linters/ruff/test_data/ruff_v0.2.1_basic_nb.check.shot @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Testing linter ruff test basic_nb 1`] = ` +{ + "issues": [], + "lintActions": [], + "taskFailures": [], + "unformattedFiles": [], +} +`;