Skip to content

Commit

Permalink
Replace flake8 and isort with ruff (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored May 4, 2023
1 parent 6947773 commit 7a6ae1f
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 140 deletions.
27 changes: 8 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
ci:
skip: [markdownlint_docker]
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.264"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/pre-commit/mirrors-prettier
# keep it before yamllint
rev: v3.0.0-alpha.6
rev: v3.0.0-alpha.9-for-vscode
hooks:
- id: prettier
always_run: true
Expand All @@ -16,13 +21,6 @@ repos:
rev: v0.12.0
hooks:
- id: markdownlint_docker
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args:
# https://github.com/pre-commit/mirrors-isort/issues/9#issuecomment-624404082
- --filter-files
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
Expand All @@ -39,24 +37,15 @@ repos:
- id: check-merge-conflict
- id: debug-statements
language_version: python3
- repo: https://github.com/pycqa/flake8.git
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-absolute-import
- flake8-black>=0.1.1
- flake8-docstrings>=1.5.0
language_version: python3
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.30.0
rev: v1.31.0
hooks:
- id: yamllint
files: \.(yaml|yml)$
types: [file, yaml]
entry: yamllint --strict
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
rev: v1.2.0
hooks:
- id: mypy
# mypy args needed in order to match mypy cli behavior
Expand Down
42 changes: 42 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ show_missing = true
[tool.isort]
profile = "black"

[tool.mypy]
python_version = 3.9
strict = true
color_output = true
error_summary = true
no_incremental = true

[tool.pylint."MESSAGES CONTROL"]
# increase from default is 50 which is too aggressive
max-statements = 60
Expand All @@ -103,6 +110,41 @@ disable = [
"too-few-public-methods",
]

[tool.ruff]
ignore = [
# temporary disabled until we fix them:
"ANN",
"B",
"T",
"D",
"E",
"PT",
"ERA",
"PTH",
"C901",
"ARG",
"FBT",
"SIM",
"PGH",
"TCH",
"PLR",
"INP",
"RET",
]
select = ["ALL"]
target-version = "py39"
# Same as Black.
line-length = 88

[tool.ruff.flake8-pytest-style]
parametrize-values-type = "tuple"

[tool.ruff.isort]
known-first-party = ["mk"]

[tool.ruff.per-file-ignores]
"test/**/*.py" = ["S"]

[tool.setuptools.dynamic]
optional-dependencies.test = { file = [".config/requirements-test.txt"] }
optional-dependencies.docs = { file = [".config/requirements-docs.txt"] }
Expand Down
49 changes: 35 additions & 14 deletions src/mk/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Main module."""
from __future__ import annotations

import argparse
import itertools
import logging
import os
import shlex
from typing import Any, Dict, List
from typing import Any

import typer
from rich.console import Console
Expand All @@ -14,7 +16,7 @@
from mk._typer import CustomTyper
from mk.ctx import ctx

handlers: List[logging.Handler]
handlers: list[logging.Handler]
console_err = Console(stderr=True)
app = CustomTyper(width=console_err.width, rich_markup_mode="rich")

Expand All @@ -24,7 +26,12 @@
else:
level = logging.DEBUG
handlers = [
RichHandler(console=console_err, show_time=False, show_path=False, markup=False)
RichHandler(
console=console_err,
show_time=False,
show_path=False,
markup=False,
),
]

logging.basicConfig(
Expand All @@ -37,18 +44,25 @@
def version_callback(value: bool) -> None:
if value:
typer.echo(f"mk {__version__}")
raise typer.Exit()
raise typer.Exit


@app.callback(invoke_without_command=True)
def main(
click_ctx: typer.Context,
# pylint: disable=unused-argument
version: bool = typer.Option(
None, "--version", callback=version_callback, is_eager=True
), # noqa: B008
None,
"--version",
callback=version_callback,
is_eager=True,
),
verbose: int = typer.Option(
0, "--verbose", "-v", count=True, help="Increase verbosity."
0,
"--verbose",
"-v",
count=True,
help="Increase verbosity.",
),
) -> None:
# enforce coloring because some tools like npm may auto disable it due to
Expand All @@ -72,31 +86,38 @@ def commands() -> None:
print(action.name)


def cli() -> None:
def cli() -> None: # pylint: disable=too-many-locals
parser = argparse.ArgumentParser(
description="Preprocess arguments to set log level.",
add_help=False,
)
parser.add_argument(
"-v", "--verbose", action="append_const", const=1, dest="verbosity"
"-v",
"--verbose",
action="append_const",
const=1,
dest="verbosity",
)
opt, _ = parser.parse_known_args()
opt.verbosity = 0 if opt.verbosity is None else sum(opt.verbosity)
if opt.verbosity:
log_level = logging.INFO if opt.verbosity == 1 else logging.DEBUG
logging.getLogger().setLevel(log_level)
logging.log(level=log_level, msg=f"Reconfigured logging level to {log_level}")
msg = f"Reconfigured logging level to {log_level}"
logging.log(level=log_level, msg=msg)

existing_commands = []
for command_info in app.registered_commands:
command = typer.main.get_command_from_info(
command_info, pretty_exceptions_short=False, rich_markup_mode="rich"
command_info,
pretty_exceptions_short=False,
rich_markup_mode="rich",
)
existing_commands.append(command.name)

# command = get_command_from_info(command_info=command_info)

action_map: Dict[str, Any] = {}
action_map: dict[str, Any] = {}
for action in ctx.runner.actions:
# Currently we rename action that overlap but in the future we may
# want to allow one to shadow others or we may want to chain them
Expand Down Expand Up @@ -135,9 +156,9 @@ def cli() -> None:
for x, action in action_map.items():
alias = x[0:alias_len]
# pylint: disable=consider-iterating-dictionary
if alias in action_map.keys():
if alias in action_map:
continue
if sum(1 for name in action_map.keys() if name.startswith(alias)) == 1:
if sum(1 for name in action_map if name.startswith(alias)) == 1:
app.command(
name=alias,
short_help=f"Alias for [dim]mk {x}[/dim]",
Expand Down
5 changes: 4 additions & 1 deletion src/mk/_typer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ def command(
**kwargs,
):
return super().command(
*args, cls=cls, context_settings=context_settings, **kwargs
*args,
cls=cls,
context_settings=context_settings,
**kwargs,
)
12 changes: 8 additions & 4 deletions src/mk/exec.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import logging
import subprocess
import sys
from os import environ
from typing import Dict, Optional

import subprocess_tee

Expand All @@ -18,9 +19,9 @@ def run(
check=False,
cwd=None,
tee=False,
env_overrides: Optional[Dict[str, str]] = None,
env_overrides: dict[str, str] | None = None,
) -> subprocess.CompletedProcess:
env: Optional[Dict[str, str]] = None
env: dict[str, str] | None = None
if env_overrides:
env = environ.copy()
env.update(env_overrides)
Expand Down Expand Up @@ -49,7 +50,10 @@ def run_or_raise(*args, cwd=None, tee=False) -> subprocess.CompletedProcess:


def run_or_fail(
*args, cwd=None, tee=False, env_overrides: Optional[Dict[str, str]] = None
*args,
cwd=None,
tee=False,
env_overrides: dict[str, str] | None = None,
) -> subprocess.CompletedProcess:
try:
return run(*args, check=True, cwd=cwd, tee=tee, env_overrides=env_overrides)
Expand Down
10 changes: 6 additions & 4 deletions src/mk/runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import hashlib
import logging
import sys
Expand All @@ -8,7 +10,7 @@
from cached_property import cached_property # type: ignore

from pathlib import Path
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from mk.tools import Action
Expand All @@ -21,7 +23,7 @@

class Runner:
def __init__(self) -> None:
self.root: Optional[Path] = None
self.root: Path | None = None
try:
self.repo = Repo(".", search_parent_directories=True)
except GitError:
Expand All @@ -38,7 +40,7 @@ def __init__(self) -> None:

self.root = Path(self.repo.working_dir)
hash_key = f"{sys.version_info.major}{sys.version_info.minor}{self.root}"
self.hash = hashlib.sha1(hash_key.encode("UTF-8")).hexdigest()[:5]
self.hash = hashlib.sha256(hash_key.encode("UTF-8")).hexdigest()[:5]
self.cache = Cache(f"~/.cache/mk.{self.hash}/")

if self.repo.is_dirty():
Expand All @@ -52,7 +54,7 @@ def pm(self) -> pluggy.PluginManager:
return pm

@cached_property
def actions(self) -> List["Action"]:
def actions(self) -> list[Action]:
"""List of discovered actions."""
if not self.root:
return []
Expand Down
20 changes: 11 additions & 9 deletions src/mk/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, List, Optional
from typing import Any


@dataclass(order=True)
class Action:
name: str
_name: str = field(default="undefined", init=False, compare=True, repr=False)

tool: Optional["Tool"] = field(default=None, compare=False)
description: Optional[str] = field(default="...", compare=False)
cwd: Optional[str] = field(default=None, compare=False)
filename: Optional[str] = field(default=None, compare=False)
args: Optional[List[Any]] = field(default_factory=list, compare=False)
tool: Tool | None = field(default=None, compare=False)
description: str | None = field(default="...", compare=False)
cwd: str | None = field(default=None, compare=False)
filename: str | None = field(default=None, compare=False)
args: list[Any] | None = field(default_factory=list, compare=False)

# https://github.com/florimondmanca/www/issues/102#issuecomment-817279834
@property # type: ignore
Expand All @@ -39,13 +41,13 @@ def is_present(self, path: Path) -> bool:
"""Return True if the tool configuration is present in the given path."""
return False

def actions(self) -> List[Action]:
def actions(self) -> list[Action]:
return []

def run(self, action: Optional[Action] = None):
def run(self, action: Action | None = None):
pass

def __repr__(self):
def __repr__(self) -> str:
return self.name

def __rich__(self):
Expand Down
Loading

0 comments on commit 7a6ae1f

Please sign in to comment.