From 9c708a22fb82ced9b6ec0b2eec69f9049d8fb72d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:24:32 +0200 Subject: [PATCH] ci: add ruff to lint process (#908) --- poetry.lock | 29 ++++++++++++++++++++++++++++- pyproject.toml | 1 + src/syrupy/extensions/base.py | 13 +++++++------ tasks/benchmark.py | 2 +- tasks/build.py | 6 +++--- tasks/lint.py | 1 + tasks/test.py | 2 -- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/poetry.lock b/poetry.lock index 2bf4c4da..0b10b87c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1203,6 +1203,33 @@ typing-extensions = {version = ">=4.0.0,<5.0", markers = "python_version < \"3.9 [package.extras] jupyter = ["ipywidgets (>=7.5.1,<9)"] +[[package]] +name = "ruff" +version = "0.7.0" +description = "An extremely fast Python linter and code formatter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.7.0-py3-none-linux_armv6l.whl", hash = "sha256:0cdf20c2b6ff98e37df47b2b0bd3a34aaa155f59a11182c1303cce79be715628"}, + {file = "ruff-0.7.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:496494d350c7fdeb36ca4ef1c9f21d80d182423718782222c29b3e72b3512737"}, + {file = "ruff-0.7.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:214b88498684e20b6b2b8852c01d50f0651f3cc6118dfa113b4def9f14faaf06"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:630fce3fefe9844e91ea5bbf7ceadab4f9981f42b704fae011bb8efcaf5d84be"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:211d877674e9373d4bb0f1c80f97a0201c61bcd1e9d045b6e9726adc42c156aa"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:194d6c46c98c73949a106425ed40a576f52291c12bc21399eb8f13a0f7073495"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:82c2579b82b9973a110fab281860403b397c08c403de92de19568f32f7178598"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9af971fe85dcd5eaed8f585ddbc6bdbe8c217fb8fcf510ea6bca5bdfff56040e"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b641c7f16939b7d24b7bfc0be4102c56562a18281f84f635604e8a6989948914"}, + {file = "ruff-0.7.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d71672336e46b34e0c90a790afeac8a31954fd42872c1f6adaea1dff76fd44f9"}, + {file = "ruff-0.7.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:ab7d98c7eed355166f367597e513a6c82408df4181a937628dbec79abb2a1fe4"}, + {file = "ruff-0.7.0-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:1eb54986f770f49edb14f71d33312d79e00e629a57387382200b1ef12d6a4ef9"}, + {file = "ruff-0.7.0-py3-none-musllinux_1_2_i686.whl", hash = "sha256:dc452ba6f2bb9cf8726a84aa877061a2462afe9ae0ea1d411c53d226661c601d"}, + {file = "ruff-0.7.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:4b406c2dce5be9bad59f2de26139a86017a517e6bcd2688da515481c05a2cb11"}, + {file = "ruff-0.7.0-py3-none-win32.whl", hash = "sha256:f6c968509f767776f524a8430426539587d5ec5c662f6addb6aa25bc2e8195ec"}, + {file = "ruff-0.7.0-py3-none-win_amd64.whl", hash = "sha256:ff4aabfbaaba880e85d394603b9e75d32b0693152e16fa659a3064a85df7fce2"}, + {file = "ruff-0.7.0-py3-none-win_arm64.whl", hash = "sha256:10842f69c245e78d6adec7e1db0a7d9ddc2fff0621d730e61657b64fa36f207e"}, + {file = "ruff-0.7.0.tar.gz", hash = "sha256:47a86360cf62d9cd53ebfb0b5eb0e882193fc191c6d717e8bef4462bc3b9ea2b"}, +] + [[package]] name = "secretstorage" version = "3.3.3" @@ -1346,4 +1373,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = ">=3.8.1" -content-hash = "834cf487f5cc2d39634a863369e993f34b866da8b3b01a7fa9db3abb8d615aab" +content-hash = "db34c95c5db5ae1f91539580528953489276cd9d1b8e71dea6aee2aa1c2c5383" diff --git a/pyproject.toml b/pyproject.toml index 3b622798..3dfcec41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,6 +51,7 @@ twine = '^5.1.1' semver = '^3.0.0' setuptools-scm = '^8.0.0' debugpy = '^1.6.6' +ruff = '>=0.7.0' [tool.black] line-length = 88 diff --git a/src/syrupy/extensions/base.py b/src/syrupy/extensions/base.py index 945cf20b..282f641a 100644 --- a/src/syrupy/extensions/base.py +++ b/src/syrupy/extensions/base.py @@ -255,6 +255,10 @@ def _get_file_basename( return test_location.basename +def _count_leading_whitespace(s: str) -> int: + return len(s) - len(s.lstrip()) + + class SnapshotReporter(ABC): _context_line_count = 1 @@ -383,16 +387,13 @@ def __limit_context(self, lines: List[str]) -> Iterator[str]: num_lines = len(lines) if num_lines: if num_lines > self._context_line_max: - count_leading_whitespace: Callable[[str], int] = lambda s: len(s) - len( - s.lstrip() - ) if self._context_line_count: num_space = ( - count_leading_whitespace(lines[self._context_line_count - 1]) - + count_leading_whitespace(lines[-self._context_line_count]) + _count_leading_whitespace(lines[self._context_line_count - 1]) + + _count_leading_whitespace(lines[-self._context_line_count]) ) // 2 else: - num_space = count_leading_whitespace(lines[num_lines // 2]) + num_space = _count_leading_whitespace(lines[num_lines // 2]) yield " " * num_space + self._marker_context_max if self._context_line_count and num_lines > 1: yield from lines[-self._context_line_count :] # noqa: E203 diff --git a/tasks/benchmark.py b/tasks/benchmark.py index 06f574af..7360716e 100644 --- a/tasks/benchmark.py +++ b/tasks/benchmark.py @@ -7,4 +7,4 @@ def benchmark(ctx, report=False): Run and generate benchmarks for current code """ - ctx.run(f"pytest benchmarks --benchmark-json=benchmarks.json") + ctx.run("pytest benchmarks --benchmark-json=benchmarks.json") diff --git a/tasks/build.py b/tasks/build.py index e66f7698..2ff5dcaf 100644 --- a/tasks/build.py +++ b/tasks/build.py @@ -12,10 +12,10 @@ def install(ctx, upgrade=False): Install dependencies and update lock file. """ if upgrade: - ctx_run(ctx, f"poetry update") + ctx_run(ctx, "poetry update") else: - ctx_run(ctx, f"poetry lock") - ctx_run(ctx, f"poetry install") + ctx_run(ctx, "poetry lock") + ctx_run(ctx, "poetry install") @task diff --git a/tasks/lint.py b/tasks/lint.py index a3eb762f..ecb848f1 100644 --- a/tasks/lint.py +++ b/tasks/lint.py @@ -10,6 +10,7 @@ "black": lambda fix: f"python -m black {'' if fix else '--check'} .", "flake8": lambda _: "python -m flake8 src tests benchmarks *.py", "mypy": lambda _: "python -m mypy --strict src benchmarks", + "ruff": lambda _: "python -m ruff check", } diff --git a/tasks/test.py b/tasks/test.py index 70f44b39..b83b986b 100644 --- a/tasks/test.py +++ b/tasks/test.py @@ -1,5 +1,3 @@ -import os - from invoke import task from .utils import ctx_run