File: lint.py

package info (click to toggle)
python-syrupy 4.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,368 kB
  • sloc: python: 5,978; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from invoke import (
    exceptions,
    task,
)

from .utils import ctx_run

lint_commands = {
    "mypy": lambda _: "python -m mypy --strict src benchmarks",
    "ruff": lambda fix: f"python -m ruff check {'--fix' if fix else ''} .",
    "ruff-format": lambda fix: f"python -m ruff format {'' if fix else '--diff'} .",
}


def run_lint(ctx, section, fix):
    print(f"\033[1m[{section}]\033[0m")
    ctx_run(ctx, lint_commands[section](fix))


@task(default=True)
def all(ctx, fix=False):  # noqa: A001
    """
    Check and fix syntax using various linters
    """
    last_error = None
    for section in lint_commands:
        try:
            run_lint(ctx, section, fix)
            print()
        except exceptions.Failure as ex:
            last_error = ex
    if last_error:
        raise last_error


def add_individual_task(name: str):
    @task(name=name)
    def lint(ctx, fix=False):
        run_lint(ctx, name, fix)

    globals()[name] = lint


for section in lint_commands:
    add_individual_task(section)