|
15 | 15 |
|
16 | 16 | import nox |
17 | 17 |
|
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 = [ |
21 | 21 | "docs", |
22 | 22 | "django_spanner", |
23 | 23 | "tests", |
@@ -52,23 +52,35 @@ def lint(session): |
52 | 52 | Returns a failure if the linters find linting errors or sufficiently |
53 | 53 | serious code quality issues. |
54 | 54 | """ |
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 | + |
57 | 67 | session.run("flake8", "django_spanner", "tests") |
58 | 68 |
|
59 | 69 |
|
60 | 70 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
61 | 71 | 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 | + ) |
72 | 84 |
|
73 | 85 |
|
74 | 86 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
@@ -343,13 +355,31 @@ def prerelease_deps(session): |
343 | 355 | @nox.session |
344 | 356 | def format(session: nox.sessions.Session) -> None: |
345 | 357 | """ |
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. |
348 | 359 | """ |
349 | | - session.install(BLACK_VERSION, ISORT_VERSION) |
| 360 | + # 1. Install ruff (skipped automatically if you run with --no-venv) |
| 361 | + session.install(RUFF_VERSION) |
350 | 362 | python_files = [path for path in os.listdir(".") if path.endswith(".py")] |
351 | 363 |
|
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