From 9a74319eed7a139cd1458678158bd4fee78579e1 Mon Sep 17 00:00:00 2001 From: "W. Augusto Andreoli" Date: Wed, 22 Nov 2023 00:04:38 +0100 Subject: [PATCH] style: nitpick --suggest --- pyproject.toml | 1 + src/conjuring/spells/aws.py | 9 +++++---- src/conjuring/spells/git.py | 2 +- src/conjuring/spells/k8s.py | 6 ++++-- src/conjuring/spells/mr.py | 5 +++-- src/conjuring/spells/pre_commit.py | 21 +++++++++++---------- src/conjuring/spells/py.py | 7 ++++--- src/conjuring/visibility.py | 23 ++++++++++++++--------- 8 files changed, 43 insertions(+), 31 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1f36257..c186fb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -128,6 +128,7 @@ ignore = [ "FBT001", # Boolean positional arg in function definition "FBT002", # Boolean default value in function definition "FBT003", # Boolean positional value in function call + "FIX002", # Line contains TO DO, consider resolving the issue "TD001", # Invalid TO DO tag "TD002", # Missing author https://beta.ruff.rs/docs/rules/#flake8-todos-td "TD003", # Missing issue link on the line following this diff --git a/src/conjuring/spells/aws.py b/src/conjuring/spells/aws.py index a40bb41..7a1fd77 100644 --- a/src/conjuring/spells/aws.py +++ b/src/conjuring/spells/aws.py @@ -1,6 +1,7 @@ """AWS: ECR login.""" +from __future__ import annotations + import os -from typing import Optional from urllib.parse import urlparse import typer @@ -19,7 +20,7 @@ def list_aws_profiles(c: Context) -> list[str]: return run_lines(c, LIST_AWS_PROFILES_COMMAND) -def fzf_aws_profile(c: Context, partial_name: Optional[str] = None) -> str: +def fzf_aws_profile(c: Context, partial_name: str | None = None) -> str: """Select an AWS profile from a partial profile name using fzf.""" if not partial_name and (aws_profile := os.environ.get("AWS_PROFILE")) and aws_profile: typer.echo(f"Using env variable AWS_PROFILE (set to '{aws_profile}')") @@ -38,12 +39,12 @@ def fzf_aws_region(c: Context) -> str: return run_with_fzf(c, f"rg -o '^region.+' {AWS_CONFIG} | tr -d ' ' | cut -d'=' -f 2 | sort -u") -def run_aws_vault(c: Context, *pieces: str, profile: Optional[str] = None) -> Result: +def run_aws_vault(c: Context, *pieces: str, profile: str | None = None) -> Result: """Run AWS vault commands in a subshell, or open a subshell if no commands were provided.""" return run_command(c, "aws-vault exec", fzf_aws_profile(c, profile), "--", *pieces, pty=False) -def clean_ecr_url(c: Context, url: Optional[str] = None) -> str: +def clean_ecr_url(c: Context, url: str | None = None) -> str: """Clean an AWS ECR URL.""" if not url: account = fzf_aws_account(c) diff --git a/src/conjuring/spells/git.py b/src/conjuring/spells/git.py index 0b10dbd..6d5ac52 100644 --- a/src/conjuring/spells/git.py +++ b/src/conjuring/spells/git.py @@ -59,7 +59,7 @@ def checkout(self, *branches: str) -> str: for branch in branches: try: self.context.run(f"git checkout {branch}") - except UnexpectedExit: + except UnexpectedExit: # noqa: PERF203 pass else: return branch diff --git a/src/conjuring/spells/k8s.py b/src/conjuring/spells/k8s.py index 41ed904..9583523 100644 --- a/src/conjuring/spells/k8s.py +++ b/src/conjuring/spells/k8s.py @@ -1,6 +1,8 @@ """[Kubernetes](https://kubernetes.io/): get pods, show variables from config maps, validate score and more.""" +from __future__ import annotations + from dataclasses import dataclass -from typing import Optional, cast +from typing import cast from invoke import Context, Result, task @@ -15,7 +17,7 @@ class Kubectl: context: Context - def choose_apps(self, partial_app_name: Optional[str] = None, *, multi: bool = False) -> list[str]: + def choose_apps(self, partial_app_name: str | None = None, *, multi: bool = False) -> list[str]: """Select apps from Kubernetes deployments, using a partial app name and fzf.""" return cast( list[str], diff --git a/src/conjuring/spells/mr.py b/src/conjuring/spells/mr.py index 391d1f0..1ec6a01 100644 --- a/src/conjuring/spells/mr.py +++ b/src/conjuring/spells/mr.py @@ -1,8 +1,9 @@ """[myrepos repository management tool](https://myrepos.branchable.com/): grep text in repos.""" +from __future__ import annotations + from dataclasses import dataclass from itertools import chain from pathlib import Path -from typing import Optional import typer from invoke import Context, task @@ -45,7 +46,7 @@ def find_configs(self, partial_name: str, echo: bool = False) -> list[Path]: return sorted({config_dir / c for c in chosen}) @staticmethod - def _find_dir_with_mrconfigs(glob_pattern: str) -> Optional[Path]: + def _find_dir_with_mrconfigs(glob_pattern: str) -> Path | None: for dir_ in chain([Path.cwd()], Path.cwd().parents): for _ in dir_.glob(glob_pattern): # Exit loop on the first file found; fzf will handle the rest diff --git a/src/conjuring/spells/pre_commit.py b/src/conjuring/spells/pre_commit.py index 4876d6a..60311b0 100644 --- a/src/conjuring/spells/pre_commit.py +++ b/src/conjuring/spells/pre_commit.py @@ -1,7 +1,8 @@ """[pre-commit](https://pre-commit.com/): install, uninstall, run/autoupdate selected hooks.""" +from __future__ import annotations + import itertools from pathlib import Path -from typing import Optional from invoke import Context, task @@ -42,7 +43,7 @@ def _patch_pre_commit_configs(before: list[str]) -> None: installed_hook.write_text("\n".join(new_lines)) -def get_hook_types(commit_msg: bool, desired_hooks: Optional[list[str]] = None) -> str: +def get_hook_types(commit_msg: bool, desired_hooks: list[str] | None = None) -> str: """Prepare a list of hook types to install/uninstall.""" hooks = ["pre-commit"] if desired_hooks: @@ -95,15 +96,15 @@ def run(c: Context, hooks: str) -> None: chosen_hooks.append("") break if not chosen_hooks: - for partial_hook in split_hooks: - chosen_hooks.append( - run_with_fzf( - c, - "yq e '.repos[].hooks[].id' .pre-commit-config.yaml | sort -u", - query=partial_hook, - dry=False, - ), + chosen_hooks = [ + run_with_fzf( + c, + "yq e '.repos[].hooks[].id' .pre-commit-config.yaml | sort -u", + query=partial_hook, + dry=False, ) + for partial_hook in split_hooks + ] for chosen_hook in chosen_hooks: run_command(c, "pre-commit run --all-files", chosen_hook, warn=True) diff --git a/src/conjuring/spells/py.py b/src/conjuring/spells/py.py index fde9883..b71a648 100644 --- a/src/conjuring/spells/py.py +++ b/src/conjuring/spells/py.py @@ -3,12 +3,13 @@ Install venvs, run tests and coverage, install debug tools, generate [Ruff](https://github.com/charliermarsh/ruff) config. """ +from __future__ import annotations + import re from collections import defaultdict from dataclasses import dataclass from pathlib import Path from textwrap import dedent -from typing import Optional import typer from invoke import Context, Result, task @@ -41,7 +42,7 @@ def set_local(self, python_version: str) -> Result: latest = self.list_versions(python_version)[-1] return self.context.run(f"pyenv local {latest}") - def list_versions(self, python_version: Optional[str] = None) -> list[str]: + def list_versions(self, python_version: str | None = None) -> list[str]: """List all installed Python versions, or only the ones matching the desired version.""" all_versions = run_lines(self.context, "pyenv versions --bare") if not python_version: @@ -95,7 +96,7 @@ def guess_python_version(self) -> str: if len(versions) > 1: print_error(f"Multiple Python versions found in {PYPROJECT_TOML}: {versions=}") raise SystemExit - return list(versions)[0] + return next(iter(versions)) def use_venv(self, python_version: str) -> Result: """Use a Poetry venv.""" diff --git a/src/conjuring/visibility.py b/src/conjuring/visibility.py index c89ccfe..0cf5bee 100644 --- a/src/conjuring/visibility.py +++ b/src/conjuring/visibility.py @@ -1,12 +1,17 @@ """Visibility predicates and a custom Invoke task that can be hidden.""" -from collections.abc import Iterable +from __future__ import annotations + from pathlib import Path -from typing import Any, Callable, Optional, Union +from typing import TYPE_CHECKING, Any, Callable from invoke import Task from conjuring.constants import PRE_COMMIT_CONFIG_YAML, PYPROJECT_TOML +if TYPE_CHECKING: + from collections.abc import Iterable + + TOOL_POETRY_SECTION = "[tool.poetry]" ShouldDisplayTasks = Callable[[], bool] @@ -58,18 +63,18 @@ class MagicTask(Task): def __init__( # noqa: PLR0913 self, body: Callable, - name: Optional[str] = None, + name: str | None = None, aliases: Iterable[str] = (), - positional: Optional[Iterable[str]] = None, + positional: Iterable[str] | None = None, optional: Iterable[str] = (), default: bool = False, auto_shortflags: bool = True, - help: Optional[dict[str, Any]] = None, # noqa: A002 - pre: Optional[Union[list[str], str]] = None, - post: Optional[Union[list[str], str]] = None, + help: dict[str, Any] | None = None, # noqa: A002 + pre: list[str] | str | None = None, + post: list[str] | str | None = None, autoprint: bool = False, - iterable: Optional[Iterable[str]] = None, - incrementable: Optional[Iterable[str]] = None, + iterable: Iterable[str] | None = None, + incrementable: Iterable[str] | None = None, should_display: ShouldDisplayTasks = always_visible, ) -> None: self.should_display: ShouldDisplayTasks = should_display