From da8a99739791ebdb6e6628e345a7e9808e4210ff Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Thu, 7 Nov 2024 21:15:27 +0100 Subject: [PATCH] dev: Switch to ruff for python linting & formatting It combines multiple tools and is much faster. --- .vscode/extensions.json | 5 ++--- flake.nix | 13 ++++++++----- src/disko/cli.py | 2 +- src/disko/mode_dev.py | 1 - src/disko_lib/messages/msgs.py | 9 ++++----- tests/disko_lib/test_types_disk.py | 1 - 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index e36dd5d..c568ac0 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -4,8 +4,7 @@ "jnoortheen.nix-ide", "ms-python.python", "ms-python.debugpy", - "ms-python.black-formatter", - "ms-python.isort", - "ms-python.mypy-type-checker" + "ms-python.mypy-type-checker", + "charliermarsh.ruff" ] } \ No newline at end of file diff --git a/flake.nix b/flake.nix index 19150b4..4b795b4 100644 --- a/flake.nix +++ b/flake.nix @@ -80,15 +80,13 @@ { default = pkgs.mkShell { name = "disko-dev"; - packages = with pkgs; [ + packages = (with pkgs; [ + ruff # Formatter and linter (python3.withPackages (ps: [ - ps.black # Code formatter - ps.isort # Import sorter ps.mypy # Static type checker - ps.autoflake # Remove unused imports automatically ps.pytest # Test runner ])) - ]; + ]); }; }); @@ -110,6 +108,7 @@ nixpkgs-fmt deno deadnix + ruff ]; text = '' showUsage() { @@ -154,12 +153,16 @@ nixpkgs-fmt -- "''${files[@]}" deno fmt -- "''${files[@]}" deadnix --edit -- "''${files[@]}" + ruff check --fix + ruff format else set -o xtrace nixpkgs-fmt --check -- "''${files[@]}" deno fmt --check -- "''${files[@]}" deadnix -- "''${files[@]}" + ruff check + ruff format --check fi } diff --git a/src/disko/cli.py b/src/disko/cli.py index 011eaef..1ec6791 100644 --- a/src/disko/cli.py +++ b/src/disko/cli.py @@ -112,7 +112,7 @@ def parse_args() -> argparse.Namespace: add_common_apply_args(parser) # Other commands - generate_parser = mode_parsers.add_parser( + _generate_parser = mode_parsers.add_parser( "generate", help=MODE_DESCRIPTION["generate"], ) diff --git a/src/disko/mode_dev.py b/src/disko/mode_dev.py index 9b541dc..869ac68 100644 --- a/src/disko/mode_dev.py +++ b/src/disko/mode_dev.py @@ -1,6 +1,5 @@ import argparse import json -from logging import info from typing import Any, assert_never from disko_lib.ansi import Colors diff --git a/src/disko_lib/messages/msgs.py b/src/disko_lib/messages/msgs.py index 1f6c273..4bebac0 100644 --- a/src/disko_lib/messages/msgs.py +++ b/src/disko_lib/messages/msgs.py @@ -2,7 +2,7 @@ import json from pathlib import Path from typing import Any from disko_lib.logging import ReadableMessage -from .colors import * +from .colors import PLACEHOLDER, RESET, FLAG, COMMAND, INVALID, FILE, VALUE, EM, EM_WARN ERR_ARGUMENTS_HELP_TXT = f"Provide either {PLACEHOLDER}disko_file{RESET} as the second argument or \ {FLAG}--flake{RESET}/{FLAG}-f{RESET} {PLACEHOLDER}flake-uri{RESET}." @@ -12,7 +12,7 @@ def err_command_failed(*, command: str, exit_code: int, stderr: str) -> Readable return ReadableMessage( "error", f""" - Command failed: {COMMAND}{command}{COMMAND} + Command failed: {COMMAND}{command}{RESET} Exit code: {INVALID}{exit_code}{RESET} stderr: {stderr} """, @@ -20,7 +20,6 @@ def err_command_failed(*, command: str, exit_code: int, stderr: str) -> Readable def err_eval_config_failed(*, args: dict[str, Any], stderr: str) -> ReadableMessage: - return ReadableMessage( "error", f""" @@ -51,7 +50,7 @@ def err_missing_arguments() -> list[ReadableMessage]: return [ ReadableMessage( "error", - f"Missing arguments!", + "Missing arguments!", ), ReadableMessage("help", ERR_ARGUMENTS_HELP_TXT), ] @@ -61,7 +60,7 @@ def err_too_many_arguments() -> list[ReadableMessage]: return [ ReadableMessage( "error", - f"Too many arguments!", + "Too many arguments!", ), ReadableMessage("help", ERR_ARGUMENTS_HELP_TXT), ] diff --git a/tests/disko_lib/test_types_disk.py b/tests/disko_lib/test_types_disk.py index dc29a3f..9afcc79 100644 --- a/tests/disko_lib/test_types_disk.py +++ b/tests/disko_lib/test_types_disk.py @@ -1,6 +1,5 @@ import json from pathlib import Path, PosixPath -import pytest from disko_lib.messages import err_unsupported_pttype, warn_generate_partial_failure from disko_lib.result import DiskoError, DiskoSuccess