Skip to content

Commit

Permalink
style: nitpick --suggest
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Nov 21, 2023
1 parent eb9e595 commit 9a74319
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/conjuring/spells/aws.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""AWS: ECR login."""
from __future__ import annotations

import os
from typing import Optional
from urllib.parse import urlparse

import typer
Expand All @@ -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}')")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/conjuring/spells/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/conjuring/spells/k8s.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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],
Expand Down
5 changes: 3 additions & 2 deletions src/conjuring/spells/mr.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions src/conjuring/spells/pre_commit.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/conjuring/spells/py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down
23 changes: 14 additions & 9 deletions src/conjuring/visibility.py
Original file line number Diff line number Diff line change
@@ -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]

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a74319

Please sign in to comment.