Skip to content

Commit 2a03fa8

Browse files
committed
replace black and isort with ruff
1 parent 03548b8 commit 2a03fa8

1 file changed

Lines changed: 52 additions & 22 deletions

File tree

packages/django-google-spanner/noxfile.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import nox
1717

18-
BLACK_VERSION = "black==22.3.0"
19-
ISORT_VERSION = "isort==5.11.0"
20-
BLACK_PATHS = [
18+
19+
RUFF_VERSION = "ruff==0.14.14"
20+
LINT_PATHS = [
2121
"docs",
2222
"django_spanner",
2323
"tests",
@@ -52,23 +52,35 @@ def lint(session):
5252
Returns a failure if the linters find linting errors or sufficiently
5353
serious code quality issues.
5454
"""
55-
session.install("flake8", BLACK_VERSION)
56-
session.run("black", "--check", *BLACK_PATHS)
55+
session.install("flake8", RUFF_VERSION)
56+
57+
# 2. Check formatting
58+
session.run(
59+
"ruff",
60+
"format",
61+
"--check",
62+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
63+
"--line-length=88",
64+
*LINT_PATHS,
65+
)
66+
5767
session.run("flake8", "django_spanner", "tests")
5868

5969

6070
@nox.session(python=DEFAULT_PYTHON_VERSION)
6171
def blacken(session):
62-
"""Run black.
63-
64-
Format code to uniform standard.
65-
66-
This currently uses Python 3.6 due to the automated Kokoro run of synthtool.
67-
That run uses an image that doesn't have 3.6 installed. Before updating this
68-
check the state of the `gcp_ubuntu_config` we use for that Kokoro run.
69-
"""
70-
session.install(BLACK_VERSION)
71-
session.run("black", *BLACK_PATHS)
72+
"""(Deprecated) Legacy session. Please use 'nox -s format'."""
73+
session.log(
74+
"WARNING: The 'blacken' session is deprecated and will be removed in a future release. Please use 'nox -s format' in the future."
75+
)
76+
session.install(RUFF_VERSION)
77+
session.run(
78+
"ruff",
79+
"format",
80+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
81+
"--line-length=88",
82+
*LINT_PATHS,
83+
)
7284

7385

7486
@nox.session(python=DEFAULT_PYTHON_VERSION)
@@ -343,13 +355,31 @@ def prerelease_deps(session):
343355
@nox.session
344356
def format(session: nox.sessions.Session) -> None:
345357
"""
346-
Run isort to sort imports. Then run black
347-
to format code to uniform standard.
358+
Run ruff to sort imports and format code.
348359
"""
349-
session.install(BLACK_VERSION, ISORT_VERSION)
360+
# 1. Install ruff (skipped automatically if you run with --no-venv)
361+
session.install(RUFF_VERSION)
350362
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
351363

352-
# Use the --fss option to sort imports using strict alphabetical order.
353-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
354-
session.run("isort", "--fss", *python_files)
355-
session.run("black", *python_files)
364+
# 2. Run Ruff to fix imports
365+
# check --select I: Enables strict import sorting
366+
# --fix: Applies the changes automatically
367+
session.run(
368+
"ruff",
369+
"check",
370+
"--select",
371+
"I",
372+
"--fix",
373+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
374+
"--line-length=88", # Standard Black line length
375+
*python_files,
376+
)
377+
378+
# 3. Run Ruff to format code
379+
session.run(
380+
"ruff",
381+
"format",
382+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
383+
"--line-length=88", # Standard Black line length
384+
*python_files,
385+
)

0 commit comments

Comments
 (0)