From 036979abdfeb6945ab78334aff5f5411aa7256b1 Mon Sep 17 00:00:00 2001 From: Zander Hill Date: Thu, 6 Feb 2025 23:57:46 -0800 Subject: [PATCH 1/3] Replace isort and black with ruff --- .pre-commit-config.yaml | 25 +++++++------------------ pyproject.toml | 1 + uv.lock | 31 ++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 82bf848..78d9590 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,25 +8,14 @@ repos: additional_dependencies: - black==23.3.0 # Matches hook -- repo: https://github.com/psf/black - rev: 23.3.0 - hooks: - - id: black - language_version: python3 - exclude: runbooks/binder/_template-deno.ipynb - -- repo: https://github.com/pycqa/isort - rev: 5.13.2 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: "d7f0995" # 2025-02-07 hooks: - - id: isort - name: isort (python) - args: ["--profile", "black", "--filter-files"] - -- repo: https://github.com/PyCQA/autoflake - rev: v2.2.1 - hooks: - - id: autoflake - args: ['--in-place', '--remove-all-unused-imports', '--remove-unused-variables', '--ignore-init-module-imports'] + - id: ruff + args: [--fix] + exclude: runbooks/binder/_template-deno.ipynb + - id: ruff-format + exclude: runbooks/binder/_template-deno.ipynb - repo: local hooks: diff --git a/pyproject.toml b/pyproject.toml index eef5b37..db42490 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ build-backend = "poetry.core.masonry.api" [dependency-groups] dev = [ "pytest>=8.3.4", + "ruff>=0.9.5", ] [tool.poetry.scripts] diff --git a/uv.lock b/uv.lock index 1ef1f6a..2248ac2 100644 --- a/uv.lock +++ b/uv.lock @@ -1771,6 +1771,31 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/f8/30/7ac943f69855c2db77407ae363484b915d861702dbba1aa82d68d57f42be/rpds_py-0.22.3-cp313-cp313t-win_amd64.whl", hash = "sha256:f5cf2a0c2bdadf3791b5c205d55a37a54025c6e18a71c71f82bb536cf9a454bf", size = 233794 }, ] +[[package]] +name = "ruff" +version = "0.9.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/02/74/6c359f6b9ed85b88df6ef31febce18faeb852f6c9855651dfb1184a46845/ruff-0.9.5.tar.gz", hash = "sha256:11aecd7a633932875ab3cb05a484c99970b9d52606ce9ea912b690b02653d56c", size = 3634177 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/17/4b/82b7c9ac874e72b82b19fd7eab57d122e2df44d2478d90825854f9232d02/ruff-0.9.5-py3-none-linux_armv6l.whl", hash = "sha256:d466d2abc05f39018d53f681fa1c0ffe9570e6d73cde1b65d23bb557c846f442", size = 11681264 }, + { url = "https://files.pythonhosted.org/packages/27/5c/f5ae0a9564e04108c132e1139d60491c0abc621397fe79a50b3dc0bd704b/ruff-0.9.5-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:38840dbcef63948657fa7605ca363194d2fe8c26ce8f9ae12eee7f098c85ac8a", size = 11657554 }, + { url = "https://files.pythonhosted.org/packages/2a/83/c6926fa3ccb97cdb3c438bb56a490b395770c750bf59f9bc1fe57ae88264/ruff-0.9.5-py3-none-macosx_11_0_arm64.whl", hash = "sha256:d56ba06da53536b575fbd2b56517f6f95774ff7be0f62c80b9e67430391eeb36", size = 11088959 }, + { url = "https://files.pythonhosted.org/packages/af/a7/42d1832b752fe969ffdbfcb1b4cb477cb271bed5835110fb0a16ef31ab81/ruff-0.9.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4f7cb2a01da08244c50b20ccfaeb5972e4228c3c3a1989d3ece2bc4b1f996001", size = 11902041 }, + { url = "https://files.pythonhosted.org/packages/53/cf/1fffa09fb518d646f560ccfba59f91b23c731e461d6a4dedd21a393a1ff1/ruff-0.9.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:96d5c76358419bc63a671caac70c18732d4fd0341646ecd01641ddda5c39ca0b", size = 11421069 }, + { url = "https://files.pythonhosted.org/packages/09/27/bb8f1b7304e2a9431f631ae7eadc35550fe0cf620a2a6a0fc4aa3d736f94/ruff-0.9.5-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:deb8304636ed394211f3a6d46c0e7d9535b016f53adaa8340139859b2359a070", size = 12625095 }, + { url = "https://files.pythonhosted.org/packages/d7/ce/ab00bc9d3df35a5f1b64f5117458160a009f93ae5caf65894ebb63a1842d/ruff-0.9.5-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:df455000bf59e62b3e8c7ba5ed88a4a2bc64896f900f311dc23ff2dc38156440", size = 13257797 }, + { url = "https://files.pythonhosted.org/packages/88/81/c639a082ae6d8392bc52256058ec60f493c6a4d06d5505bccface3767e61/ruff-0.9.5-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:de92170dfa50c32a2b8206a647949590e752aca8100a0f6b8cefa02ae29dce80", size = 12763793 }, + { url = "https://files.pythonhosted.org/packages/b3/d0/0a3d8f56d1e49af466dc770eeec5c125977ba9479af92e484b5b0251ce9c/ruff-0.9.5-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:3d28532d73b1f3f627ba88e1456f50748b37f3a345d2be76e4c653bec6c3e393", size = 14386234 }, + { url = "https://files.pythonhosted.org/packages/04/70/e59c192a3ad476355e7f45fb3a87326f5219cc7c472e6b040c6c6595c8f0/ruff-0.9.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2c746d7d1df64f31d90503ece5cc34d7007c06751a7a3bbeee10e5f2463d52d2", size = 12437505 }, + { url = "https://files.pythonhosted.org/packages/55/4e/3abba60a259d79c391713e7a6ccabf7e2c96e5e0a19100bc4204f1a43a51/ruff-0.9.5-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:11417521d6f2d121fda376f0d2169fb529976c544d653d1d6044f4c5562516ee", size = 11884799 }, + { url = "https://files.pythonhosted.org/packages/a3/db/b0183a01a9f25b4efcae919c18fb41d32f985676c917008620ad692b9d5f/ruff-0.9.5-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:5b9d71c3879eb32de700f2f6fac3d46566f644a91d3130119a6378f9312a38e1", size = 11527411 }, + { url = "https://files.pythonhosted.org/packages/0a/e4/3ebfcebca3dff1559a74c6becff76e0b64689cea02b7aab15b8b32ea245d/ruff-0.9.5-py3-none-musllinux_1_2_i686.whl", hash = "sha256:2e36c61145e70febcb78483903c43444c6b9d40f6d2f800b5552fec6e4a7bb9a", size = 12078868 }, + { url = "https://files.pythonhosted.org/packages/ec/b2/5ab808833e06c0a1b0d046a51c06ec5687b73c78b116e8d77687dc0cd515/ruff-0.9.5-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:2f71d09aeba026c922aa7aa19a08d7bd27c867aedb2f74285a2639644c1c12f5", size = 12524374 }, + { url = "https://files.pythonhosted.org/packages/e0/51/1432afcc3b7aa6586c480142caae5323d59750925c3559688f2a9867343f/ruff-0.9.5-py3-none-win32.whl", hash = "sha256:134f958d52aa6fdec3b294b8ebe2320a950d10c041473c4316d2e7d7c2544723", size = 9853682 }, + { url = "https://files.pythonhosted.org/packages/b7/ad/c7a900591bd152bb47fc4882a27654ea55c7973e6d5d6396298ad3fd6638/ruff-0.9.5-py3-none-win_amd64.whl", hash = "sha256:78cc6067f6d80b6745b67498fb84e87d32c6fc34992b52bffefbdae3442967d6", size = 10865744 }, + { url = "https://files.pythonhosted.org/packages/75/d9/fde7610abd53c0c76b6af72fc679cb377b27c617ba704e25da834e0a0608/ruff-0.9.5-py3-none-win_arm64.whl", hash = "sha256:18a29f1a005bddb229e580795627d297dfa99f16b30c7039e73278cf6b5f9fa9", size = 10064595 }, +] + [[package]] name = "runbook" version = "0" @@ -1799,6 +1824,7 @@ dependencies = [ [package.dev-dependencies] dev = [ { name = "pytest" }, + { name = "ruff" }, ] [package.metadata] @@ -1824,7 +1850,10 @@ requires-dist = [ ] [package.metadata.requires-dev] -dev = [{ name = "pytest", specifier = ">=8.3.4" }] +dev = [ + { name = "pytest", specifier = ">=8.3.4" }, + { name = "ruff", specifier = ">=0.9.5" }, +] [[package]] name = "send2trash" From 59600aad45bc23f85921db118ca995b90d06cf29 Mon Sep 17 00:00:00 2001 From: Zander Hill Date: Fri, 7 Feb 2025 00:02:58 -0800 Subject: [PATCH 2/3] Ruff auto formatting --- .pre-commit-config.yaml | 9 --------- pyproject.toml | 11 +++++++++++ runbook/cli/__init__.py | 1 + runbook/cli/commands/check.py | 1 + runbook/cli/commands/convert.py | 1 + runbook/cli/commands/create.py | 2 +- runbook/cli/commands/diff.py | 1 + runbook/cli/commands/edit.py | 1 + runbook/cli/commands/init.py | 2 +- runbook/cli/commands/list.py | 1 + runbook/cli/commands/plan.py | 4 ++-- runbook/cli/commands/run.py | 1 + runbook/cli/commands/show.py | 4 ++-- runbook/cli/completions.py | 4 ++-- runbook/data/_template-python.ipynb | 2 +- runbook/template.py | 8 +++++--- 16 files changed, 32 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 78d9590..3ec4237 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,4 @@ repos: -- repo: https://github.com/mwouts/jupytext - rev: v1.14.7 # CURRENT_TAG/COMMIT_HASH - hooks: - - id: jupytext - args: [--pipe, black] - exclude: runbook/data/_template-deno.ipynb|.*\.md|tests/.* - additional_dependencies: - - black==23.3.0 # Matches hook - - repo: https://github.com/astral-sh/ruff-pre-commit rev: "d7f0995" # 2025-02-07 hooks: diff --git a/pyproject.toml b/pyproject.toml index db42490..890bd3d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,3 +61,14 @@ source = "init" # https://github.com/python-poetry/poetry/issues/927 # [tool.poetry.plugins."papermill.translators"] # "typescript" = "translators:runbook.translators.TypescriptTranslator" + +[tool.ruff] +line-length = 88 # Same default as Black (adjust if needed) + +[tool.ruff.format] +quote-style = "double" # Choose "single" or "double" +indent-style = "space" +line-ending = "lf" + +[tool.ruff.lint] +select = ["I"] diff --git a/runbook/cli/__init__.py b/runbook/cli/__init__.py index 27d413b..6488983 100644 --- a/runbook/cli/__init__.py +++ b/runbook/cli/__init__.py @@ -1,6 +1,7 @@ import os import click + from runbook.cli.commands import ( check, convert, diff --git a/runbook/cli/commands/check.py b/runbook/cli/commands/check.py index b01a5fb..a7863d1 100644 --- a/runbook/cli/commands/check.py +++ b/runbook/cli/commands/check.py @@ -7,6 +7,7 @@ from os import path import click + from runbook.cli.completions import EditableNotebook from runbook.cli.validators import validate_runbook_file_path diff --git a/runbook/cli/commands/convert.py b/runbook/cli/commands/convert.py index a6f24c0..84cee6f 100644 --- a/runbook/cli/commands/convert.py +++ b/runbook/cli/commands/convert.py @@ -1,6 +1,7 @@ from os import path import click + from runbook.cli.completions import EditableNotebook from runbook.cli.validators import validate_runbook_file_path diff --git a/runbook/cli/commands/create.py b/runbook/cli/commands/create.py index e24676e..6d1fde9 100644 --- a/runbook/cli/commands/create.py +++ b/runbook/cli/commands/create.py @@ -1,6 +1,7 @@ from os import path import click + from runbook.cli.lib import nbconvert_launch_instance from runbook.cli.validators import validate_create_language, validate_template @@ -16,7 +17,6 @@ callback=validate_template, help="Path to the template file to use", ) - # TODO: switch to language and template defaulting to Deno # based on operational experience at work @click.option( diff --git a/runbook/cli/commands/diff.py b/runbook/cli/commands/diff.py index aaa2aa3..1b4b48b 100644 --- a/runbook/cli/commands/diff.py +++ b/runbook/cli/commands/diff.py @@ -1,6 +1,7 @@ from os import path import click + from runbook.cli.completions import EditableNotebook from runbook.cli.validators import validate_runbook_file_path diff --git a/runbook/cli/commands/edit.py b/runbook/cli/commands/edit.py index 264f54f..f82a6a0 100644 --- a/runbook/cli/commands/edit.py +++ b/runbook/cli/commands/edit.py @@ -1,6 +1,7 @@ from os import path import click + from runbook.cli.completions import EditableNotebook from runbook.cli.validators import validate_runbook_file_path diff --git a/runbook/cli/commands/init.py b/runbook/cli/commands/init.py index 42f9415..c29d15c 100644 --- a/runbook/cli/commands/init.py +++ b/runbook/cli/commands/init.py @@ -2,9 +2,9 @@ from pathlib import Path import click -from runbook.template import TEMPLATES from runbook import __version__ as VERSION +from runbook.template import TEMPLATES RUNBOOK_CONFIG = {"version": 1, "library_version": VERSION, "directory": None} diff --git a/runbook/cli/commands/list.py b/runbook/cli/commands/list.py index 2185fc1..7e1652b 100644 --- a/runbook/cli/commands/list.py +++ b/runbook/cli/commands/list.py @@ -1,4 +1,5 @@ import click + from runbook.cli.commands.show import get_notebook_header diff --git a/runbook/cli/commands/plan.py b/runbook/cli/commands/plan.py index 438df42..1458ce8 100644 --- a/runbook/cli/commands/plan.py +++ b/runbook/cli/commands/plan.py @@ -8,12 +8,12 @@ import click import nbformat +import papermill as pm + from runbook.cli.lib import nbconvert_launch_instance from runbook.cli.validators import validate_plan_params, validate_runbook_file_path from runbook.constants import RUNBOOK_METADATA -import papermill as pm - def get_notebook_language(notebook_path: str) -> str: """ diff --git a/runbook/cli/commands/run.py b/runbook/cli/commands/run.py index 6fc60a5..24d9f28 100644 --- a/runbook/cli/commands/run.py +++ b/runbook/cli/commands/run.py @@ -1,4 +1,5 @@ import click + from runbook.cli.validators import validate_planned_runbook_file_path diff --git a/runbook/cli/commands/show.py b/runbook/cli/commands/show.py index d4b777f..90b1ba1 100644 --- a/runbook/cli/commands/show.py +++ b/runbook/cli/commands/show.py @@ -1,13 +1,13 @@ import click import nbformat +import papermill as pm from rich.console import Console from rich.table import Table + from runbook.cli.commands.plan import get_notebook_language from runbook.cli.validators import validate_runbook_file_path from runbook.constants import RUNBOOK_METADATA -import papermill as pm - def get_notebook_header(notebook_path): """Get the title (H1) and description (H2) from the notebook's markdown cells. diff --git a/runbook/cli/completions.py b/runbook/cli/completions.py index 0bbe7d6..611bfef 100644 --- a/runbook/cli/completions.py +++ b/runbook/cli/completions.py @@ -7,7 +7,7 @@ def runnable_shell_complete(self, ctx, param, incomplete): pattern = f"**/{incomplete}*.ipynb" if incomplete == "": - pattern = f"**/*.ipynb" + pattern = "**/*.ipynb" return [ CompletionItem(name) for name in glob(pattern, recursive=True) if "runs" in name @@ -21,7 +21,7 @@ def runnable_shell_complete(self, ctx, param, incomplete): def editable_shell_complete(self, ctx, param, incomplete): pattern = f"**/{incomplete}*.ipynb" if incomplete == "": - pattern = f"**/*.ipynb" + pattern = "**/*.ipynb" return [CompletionItem(name) for name in glob(pattern, recursive=True)] diff --git a/runbook/data/_template-python.ipynb b/runbook/data/_template-python.ipynb index 50715fd..0548904 100644 --- a/runbook/data/_template-python.ipynb +++ b/runbook/data/_template-python.ipynb @@ -60,7 +60,7 @@ "outputs": [], "source": [ "# SHELL HELPERS\n", - "from runbook.shell import shell_builder, gather, confirm, style, quote\n", + "from runbook.shell import confirm, gather, shell_builder, style\n", "\n", "sh = shell_builder(\n", " dry_run, tags_default={\"environment\": \"testing\"}, confirm_default=True\n", diff --git a/runbook/template.py b/runbook/template.py index 307e6c9..55c4b63 100644 --- a/runbook/template.py +++ b/runbook/template.py @@ -17,9 +17,11 @@ def read_embedded_file(package: str, filename: str) -> str: """ if sys.version_info >= (3, 7): # Use importlib.resources (Python 3.7+) - with importlib_resources.files(package).joinpath(filename).open( - "r", encoding="utf-8" - ) as f: + with ( + importlib_resources.files(package) + .joinpath(filename) + .open("r", encoding="utf-8") as f + ): return f.read() else: # Use pkgutil.get_data() for older versions From d4b48f9b7eb473e66e80a62286bf7edfafafa0c2 Mon Sep 17 00:00:00 2001 From: Zander Hill Date: Fri, 7 Feb 2025 00:05:27 -0800 Subject: [PATCH 3/3] Move GH Actions to ruff --- .github/workflows/main.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9a445be..dfb54ed 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,13 +18,9 @@ jobs: - name: initialize folders # Runbook command comes from .hermit/bin/runbook run: runbook init --skip-confirmation=true - - name: Black formatting - run: pre-commit run black - - name: jupytext hook - run: pre-commit run jupytext - - name: isort hook - run: pre-commit run isort - - name: autoflake hook - run: pre-commit run autoflake + - name: Ruff formatting + run: pre-commit run ruff-format + - name: Ruff linting + run: pre-commit run ruff - run: just test - run: echo "🍏 This job's status is ${{ job.status }}."