From 06688fc28888646c74c8414b72e127a01ae59b2a Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Mon, 26 Aug 2024 09:10:25 +0200 Subject: [PATCH] ruff: load all the error codes at once --- vcs-diff-lint-csdiff-ruff | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vcs-diff-lint-csdiff-ruff b/vcs-diff-lint-csdiff-ruff index f4f53ae..f3a2575 100755 --- a/vcs-diff-lint-csdiff-ruff +++ b/vcs-diff-lint-csdiff-ruff @@ -9,6 +9,7 @@ The script accepts the same parameters as `ruff check` itself. """ import os +from functools import cache import sys import json from subprocess import Popen, PIPE @@ -24,16 +25,19 @@ def ruff_check(): return json.loads(out) -def ruff_code_to_name(code): +@cache +def ruff_code_to_name(): """ - Convert noqa code e.g. F401 to its human-readable name, e.g. unused-import + Introspect ruff and map all possible noqa codes to a human-readable names. """ - # This implementation will likely kill all ruff performance benefits - cmd = ["ruff", "rule", code, "--output-format=json"] + cmd = ["ruff", "rule", "--all", "--output-format=json"] with Popen(cmd, stdout=PIPE) as proc: out, _err = proc.communicate(timeout=60) - data = json.loads(out) - return data["name"] + the_map = {} + array = json.loads(out) + for rule in array: + the_map[rule["code"]] = rule['name'] + return the_map def main(): @@ -46,7 +50,7 @@ def main(): column = defect["location"]["column"] or "" colsep = ":" if column else "" event = "{0}[{1}]".format( - defect["code"], ruff_code_to_name(defect["code"])) + defect["code"], ruff_code_to_name()[defect["code"]]) print("Error: RUFF_WARNING:") print("{file}:{line}{colsep}{column}: {event}: {msg}".format(