Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 185fc03

Browse files
committed
chore: replace isort and black with ruff
1 parent a6fb1a7 commit 185fc03

File tree

9 files changed

+273
-191
lines changed

9 files changed

+273
-191
lines changed

gapic/templates/noxfile.py.j2

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import warnings
1212

1313
import nox
1414

15-
BLACK_VERSION = "black[jupyter]==23.7.0"
16-
ISORT_VERSION = "isort==5.11.0"
15+
RUFF_VERSION = "ruff==0.14.14"
1716

1817
{% if api.naming.module_namespace %}
1918
LINT_PATHS = ["docs", "{{ api.naming.module_namespace[0] }}", "tests", "noxfile.py", "setup.py"]
@@ -147,12 +146,14 @@ def lint(session):
147146
Returns a failure if the linters find linting errors or sufficiently
148147
serious code quality issues.
149148
"""
150-
session.install("flake8", BLACK_VERSION)
151-
session.run(
152-
"black",
153-
"--check",
154-
*LINT_PATHS,
155-
)
149+
session.install("flake8", "ruff")
150+
151+
# 1. Check imports
152+
session.run("ruff", "check", "--select", "I", "--line-length=88", *LINT_PATHS)
153+
154+
# 2. Check formatting
155+
session.run("ruff", "format", "--check", "--line-length=88", *LINT_PATHS)
156+
156157
157158
{% if api.naming.module_namespace %}
158159
session.run("flake8", "{{ api.naming.module_namespace[0] }}", "tests")
@@ -163,30 +164,37 @@ def lint(session):
163164

164165
@nox.session(python=DEFAULT_PYTHON_VERSION)
165166
def blacken(session):
166-
"""Run black. Format code to uniform standard."""
167-
session.install(BLACK_VERSION)
168-
session.run(
169-
"black",
170-
*LINT_PATHS,
171-
)
167+
"""(Deprecated) Legacy session. Please use 'nox -s format'."""
168+
session.log("WARNING: The 'blacken' session is deprecated and will be removed in the next release. Please use 'nox -s format' in the future.")
169+
170+
# Just run the ruff formatter (keeping legacy behavior of only formatting, not sorting imports)
171+
session.install("ruff")
172+
session.run("ruff", "format", "--line-length=88", *LINT_PATHS)
172173

173174

174175
@nox.session(python=DEFAULT_PYTHON_VERSION)
175176
def format(session):
176177
"""
177-
Run isort to sort imports. Then run black
178-
to format code to uniform standard.
178+
Run ruff to sort imports and format code.
179179
"""
180-
session.install(BLACK_VERSION, ISORT_VERSION)
181-
# Use the --fss option to sort imports using strict alphabetical order.
182-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
180+
# 1. Install ruff (skipped automatically if you run with --no-venv)
181+
session.install("ruff")
182+
183+
# 2. Run Ruff to fix imports
184+
# check --select I: Enables strict import sorting
185+
# --fix: Applies the changes automatically
183186
session.run(
184-
"isort",
185-
"--fss",
187+
"ruff", "check",
188+
"--select", "I",
189+
"--fix",
190+
"--line-length=88", # Standard Black line length
186191
*LINT_PATHS,
187192
)
193+
194+
# 3. Run Ruff to format code
188195
session.run(
189-
"black",
196+
"ruff", "format",
197+
"--line-length=88", # Standard Black line length
190198
*LINT_PATHS,
191199
)
192200

noxfile.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131

3232
showcase_version = os.environ.get("SHOWCASE_VERSION", "0.35.0")
3333
ADS_TEMPLATES = path.join(path.dirname(__file__), "gapic", "ads-templates")
34-
BLACK_VERSION = "black==25.1.0"
35-
BLACK_PATHS = ["docs", "gapic", "tests", "test_utils", "noxfile.py", "setup.py"]
36-
# exclude golden files and generated protobuf code
37-
BLACK_EXCLUDES = "|".join([".*golden.*", ".*pb2.py"])
34+
RUFF_VERSION = "ruff==0.14.14"
35+
LINT_PATHS = ["docs", "gapic", "tests", "test_utils", "noxfile.py", "setup.py"]
36+
# Ruff uses globs for excludes (different from Black's regex)
37+
# .*golden.* -> *golden*
38+
# .*pb2.py -> *pb2.py
39+
RUFF_EXCLUDES = "*golden*,*pb2.py,*pb2.pyi"
3840

3941
ALL_PYTHON = (
4042
"3.7",
@@ -755,14 +757,27 @@ def lint(session):
755757
Returns a failure if the linters find linting errors or sufficiently
756758
serious code quality issues.
757759
"""
758-
session.install("flake8", BLACK_VERSION)
760+
session.install("flake8", RUFF_VERSION)
761+
762+
# 1. Check imports
759763
session.run(
760-
"black",
764+
"ruff",
765+
"check",
766+
"--select", "I",
767+
*LINT_PATHS,
768+
"--extend-exclude", RUFF_EXCLUDES,
769+
)
770+
771+
# 2. Check formatting
772+
session.run(
773+
"ruff",
774+
"format",
761775
"--check",
762-
*BLACK_PATHS,
763-
"--extend-exclude",
764-
BLACK_EXCLUDES,
776+
*LINT_PATHS,
777+
"--extend-exclude", RUFF_EXCLUDES,
765778
)
779+
780+
# 3. Run Flake8
766781
session.run(
767782
"flake8",
768783
"gapic",
@@ -772,11 +787,21 @@ def lint(session):
772787

773788
@nox.session(python="3.10")
774789
def blacken(session):
775-
"""Run black. Format code to uniform standard."""
776-
session.install(BLACK_VERSION)
790+
"""Run ruff format.
791+
792+
DEPRECATED: This session now uses Ruff instead of Black.
793+
It formats code style only (indentation, quotes, etc).
794+
"""
795+
session.log("WARNING: The 'blacken' session is deprecated and will be removed in the next release. Please use 'nox -s format' in the future.")
796+
797+
798+
session.install(RUFF_VERSION)
799+
800+
# 1. Format Code (Replaces black)
801+
# We do NOT run 'ruff check --select I' here, preserving strict parity.
777802
session.run(
778-
"black",
779-
*BLACK_PATHS,
780-
"--extend-exclude",
781-
BLACK_EXCLUDES,
803+
"ruff",
804+
"format",
805+
*LINT_PATHS,
806+
"--extend-exclude", RUFF_EXCLUDES,
782807
)

tests/integration/goldens/asset/noxfile.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
import nox
2525

26-
BLACK_VERSION = "black[jupyter]==23.7.0"
27-
ISORT_VERSION = "isort==5.11.0"
26+
RUFF_VERSION = "ruff==0.14.14"
2827

2928
LINT_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
3029

@@ -150,42 +149,50 @@ def lint(session):
150149
Returns a failure if the linters find linting errors or sufficiently
151150
serious code quality issues.
152151
"""
153-
session.install("flake8", BLACK_VERSION)
154-
session.run(
155-
"black",
156-
"--check",
157-
*LINT_PATHS,
158-
)
152+
session.install("flake8", "ruff")
153+
154+
# 1. Check imports
155+
session.run("ruff", "check", "--select", "I", "--line-length=88", *LINT_PATHS)
156+
157+
# 2. Check formatting
158+
session.run("ruff", "format", "--check", "--line-length=88", *LINT_PATHS)
159159

160160
session.run("flake8", "google", "tests")
161161

162162

163163
@nox.session(python=DEFAULT_PYTHON_VERSION)
164164
def blacken(session):
165-
"""Run black. Format code to uniform standard."""
166-
session.install(BLACK_VERSION)
167-
session.run(
168-
"black",
169-
*LINT_PATHS,
170-
)
165+
"""(Deprecated) Legacy session. Please use 'nox -s format'."""
166+
session.log("WARNING: The 'blacken' session is deprecated and will be removed in the next release. Please use 'nox -s format' in the future.")
167+
168+
# Just run the ruff formatter (keeping legacy behavior of only formatting, not sorting imports)
169+
session.install("ruff")
170+
session.run("ruff", "format", "--line-length=88", *LINT_PATHS)
171171

172172

173173
@nox.session(python=DEFAULT_PYTHON_VERSION)
174174
def format(session):
175175
"""
176-
Run isort to sort imports. Then run black
177-
to format code to uniform standard.
176+
Run ruff to sort imports and format code.
178177
"""
179-
session.install(BLACK_VERSION, ISORT_VERSION)
180-
# Use the --fss option to sort imports using strict alphabetical order.
181-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
178+
# 1. Install ruff (skipped automatically if you run with --no-venv)
179+
session.install("ruff")
180+
181+
# 2. Run Ruff to fix imports
182+
# check --select I: Enables strict import sorting
183+
# --fix: Applies the changes automatically
182184
session.run(
183-
"isort",
184-
"--fss",
185+
"ruff", "check",
186+
"--select", "I",
187+
"--fix",
188+
"--line-length=88", # Standard Black line length
185189
*LINT_PATHS,
186190
)
191+
192+
# 3. Run Ruff to format code
187193
session.run(
188-
"black",
194+
"ruff", "format",
195+
"--line-length=88", # Standard Black line length
189196
*LINT_PATHS,
190197
)
191198

tests/integration/goldens/credentials/noxfile.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
import nox
2525

26-
BLACK_VERSION = "black[jupyter]==23.7.0"
27-
ISORT_VERSION = "isort==5.11.0"
26+
RUFF_VERSION = "ruff==0.14.14"
2827

2928
LINT_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
3029

@@ -150,42 +149,50 @@ def lint(session):
150149
Returns a failure if the linters find linting errors or sufficiently
151150
serious code quality issues.
152151
"""
153-
session.install("flake8", BLACK_VERSION)
154-
session.run(
155-
"black",
156-
"--check",
157-
*LINT_PATHS,
158-
)
152+
session.install("flake8", "ruff")
153+
154+
# 1. Check imports
155+
session.run("ruff", "check", "--select", "I", "--line-length=88", *LINT_PATHS)
156+
157+
# 2. Check formatting
158+
session.run("ruff", "format", "--check", "--line-length=88", *LINT_PATHS)
159159

160160
session.run("flake8", "google", "tests")
161161

162162

163163
@nox.session(python=DEFAULT_PYTHON_VERSION)
164164
def blacken(session):
165-
"""Run black. Format code to uniform standard."""
166-
session.install(BLACK_VERSION)
167-
session.run(
168-
"black",
169-
*LINT_PATHS,
170-
)
165+
"""(Deprecated) Legacy session. Please use 'nox -s format'."""
166+
session.log("WARNING: The 'blacken' session is deprecated and will be removed in the next release. Please use 'nox -s format' in the future.")
167+
168+
# Just run the ruff formatter (keeping legacy behavior of only formatting, not sorting imports)
169+
session.install("ruff")
170+
session.run("ruff", "format", "--line-length=88", *LINT_PATHS)
171171

172172

173173
@nox.session(python=DEFAULT_PYTHON_VERSION)
174174
def format(session):
175175
"""
176-
Run isort to sort imports. Then run black
177-
to format code to uniform standard.
176+
Run ruff to sort imports and format code.
178177
"""
179-
session.install(BLACK_VERSION, ISORT_VERSION)
180-
# Use the --fss option to sort imports using strict alphabetical order.
181-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
178+
# 1. Install ruff (skipped automatically if you run with --no-venv)
179+
session.install("ruff")
180+
181+
# 2. Run Ruff to fix imports
182+
# check --select I: Enables strict import sorting
183+
# --fix: Applies the changes automatically
182184
session.run(
183-
"isort",
184-
"--fss",
185+
"ruff", "check",
186+
"--select", "I",
187+
"--fix",
188+
"--line-length=88", # Standard Black line length
185189
*LINT_PATHS,
186190
)
191+
192+
# 3. Run Ruff to format code
187193
session.run(
188-
"black",
194+
"ruff", "format",
195+
"--line-length=88", # Standard Black line length
189196
*LINT_PATHS,
190197
)
191198

0 commit comments

Comments
 (0)