Skip to content

Commit 403bcf5

Browse files
chore(nox): chore update noxfile with format session that uses ruff (#16647)
Partial fix for #16014 #### Provides the following update to multiple noxfiles: * Adds a version number for ruff * Adds OR edits a `format` nox session to use ruff for formatting * Formats the edited noxfiles. #### What it does NOT do: This is not a universal panacea... it is purposefully kept lean and focuses on a single task. It does not delete black versions, it does not delete isort versions, etc. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent f1683b0 commit 403bcf5

14 files changed

Lines changed: 338 additions & 89 deletions

File tree

packages/bigquery-magics/noxfile.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import pathlib
2323
import re
2424
import shutil
25-
from typing import Dict, List
2625
import warnings
26+
from typing import Dict, List
2727

2828
import nox
2929

3030
FLAKE8_VERSION = "flake8==6.1.0"
3131
BLACK_VERSION = "black[jupyter]==23.7.0"
32+
RUFF_VERSION = "ruff==0.14.14"
3233
ISORT_VERSION = "isort==5.11.0"
3334
LINT_PATHS = ["docs", "bigquery_magics", "tests", "noxfile.py", "setup.py"]
3435

@@ -170,19 +171,29 @@ def blacken(session):
170171
@nox.session(python=DEFAULT_PYTHON_VERSION)
171172
def format(session):
172173
"""
173-
Run isort to sort imports. Then run black
174-
to format code to uniform standard.
174+
Run ruff to sort imports and format code.
175175
"""
176-
session.install(BLACK_VERSION, ISORT_VERSION)
177-
# Use the --fss option to sort imports using strict alphabetical order.
178-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
176+
# 1. Install ruff (skipped automatically if you run with --no-venv)
177+
session.install(RUFF_VERSION)
178+
179+
# 2. Run Ruff to fix imports
179180
session.run(
180-
"isort",
181-
"--fss",
181+
"ruff",
182+
"check",
183+
"--select",
184+
"I",
185+
"--fix",
186+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
187+
"--line-length=88",
182188
*LINT_PATHS,
183189
)
190+
191+
# 3. Run Ruff to format code
184192
session.run(
185-
"black",
193+
"ruff",
194+
"format",
195+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
196+
"--line-length=88",
186197
*LINT_PATHS,
187198
)
188199

packages/db-dtypes/noxfile.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import pathlib
2323
import re
2424
import shutil
25-
from typing import Dict, List
2625
import warnings
26+
from typing import Dict, List
2727

2828
import nox
2929

3030
FLAKE8_VERSION = "flake8==6.1.0"
3131
BLACK_VERSION = "black[jupyter]==23.7.0"
32+
RUFF_VERSION = "ruff==0.14.14"
3233
ISORT_VERSION = "isort==5.11.0"
3334
LINT_PATHS = ["docs", "db_dtypes", "tests", "noxfile.py", "setup.py"]
3435

@@ -122,20 +123,29 @@ def blacken(session):
122123
@nox.session(python=DEFAULT_PYTHON_VERSION)
123124
def format(session):
124125
"""
125-
Run isort to sort imports. Then run black
126-
to format code to uniform standard.
126+
Run ruff to sort imports and format code.
127127
"""
128-
session.install(BLACK_VERSION, ISORT_VERSION)
129-
# Use the --fss option to sort imports using strict alphabetical order.
130-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
131-
session.run("python", "-m", "pip", "freeze")
128+
# 1. Install ruff (skipped automatically if you run with --no-venv)
129+
session.install(RUFF_VERSION)
130+
131+
# 2. Run Ruff to fix imports
132132
session.run(
133-
"isort",
134-
"--fss",
133+
"ruff",
134+
"check",
135+
"--select",
136+
"I",
137+
"--fix",
138+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
139+
"--line-length=88",
135140
*LINT_PATHS,
136141
)
142+
143+
# 3. Run Ruff to format code
137144
session.run(
138-
"black",
145+
"ruff",
146+
"format",
147+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
148+
"--line-length=88",
139149
*LINT_PATHS,
140150
)
141151

packages/google-api-core/noxfile.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# PIP_INDEX_URL=https://pypi.org/simple nox
1919

2020
from __future__ import absolute_import
21+
2122
import os
2223
import pathlib
2324
import re
@@ -27,8 +28,8 @@
2728
# https://github.com/google/importlab/issues/25
2829
import nox
2930

30-
3131
BLACK_VERSION = "black==23.7.0"
32+
RUFF_VERSION = "ruff==0.14.14"
3233
BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
3334
# Black and flake8 clash on the syntax for ignoring flake8's F401 in this file.
3435
BLACK_EXCLUDES = ["--exclude", "^/google/api_core/operations_v1/__init__.py"]
@@ -72,6 +73,36 @@ def blacken(session):
7273
session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS)
7374

7475

76+
@nox.session(python=DEFAULT_PYTHON_VERSION)
77+
def format(session):
78+
"""
79+
Run ruff to sort imports and format code.
80+
"""
81+
# 1. Install ruff (skipped automatically if you run with --no-venv)
82+
session.install(RUFF_VERSION)
83+
84+
# 2. Run Ruff to fix imports
85+
session.run(
86+
"ruff",
87+
"check",
88+
"--select",
89+
"I",
90+
"--fix",
91+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
92+
"--line-length=88",
93+
*BLACK_PATHS,
94+
)
95+
96+
# 3. Run Ruff to format code
97+
session.run(
98+
"ruff",
99+
"format",
100+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
101+
"--line-length=88",
102+
*BLACK_PATHS,
103+
)
104+
105+
75106
def install_prerelease_dependencies(session, constraints_path):
76107
with open(constraints_path, encoding="utf-8") as constraints_file:
77108
constraints_text = constraints_file.read()

packages/google-auth-httplib2/noxfile.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
import pathlib
2121
import re
2222
import shutil
23-
from typing import Dict, List
2423
import warnings
24+
from typing import Dict, List
2525

2626
import nox
2727

2828
FLAKE8_VERSION = "flake8==6.1.0"
2929
BLACK_VERSION = "black[jupyter]==23.7.0"
30+
RUFF_VERSION = "ruff==0.14.14"
3031
ISORT_VERSION = "isort==5.11.0"
3132
LINT_PATHS = ["docs", "google_auth_httplib2.py", "tests", "noxfile.py", "setup.py"]
3233

@@ -118,19 +119,29 @@ def blacken(session):
118119
@nox.session(python=DEFAULT_PYTHON_VERSION)
119120
def format(session):
120121
"""
121-
Run isort to sort imports. Then run black
122-
to format code to uniform standard.
122+
Run ruff to sort imports and format code.
123123
"""
124-
session.install(BLACK_VERSION, ISORT_VERSION)
125-
# Use the --fss option to sort imports using strict alphabetical order.
126-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
124+
# 1. Install ruff (skipped automatically if you run with --no-venv)
125+
session.install(RUFF_VERSION)
126+
127+
# 2. Run Ruff to fix imports
127128
session.run(
128-
"isort",
129-
"--fss",
129+
"ruff",
130+
"check",
131+
"--select",
132+
"I",
133+
"--fix",
134+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
135+
"--line-length=88",
130136
*LINT_PATHS,
131137
)
138+
139+
# 3. Run Ruff to format code
132140
session.run(
133-
"black",
141+
"ruff",
142+
"format",
143+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
144+
"--line-length=88",
134145
*LINT_PATHS,
135146
)
136147

packages/google-auth-oauthlib/noxfile.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import pathlib
2323
import re
2424
import shutil
25-
from typing import Dict, List
2625
import warnings
26+
from typing import Dict, List
2727

2828
import nox
2929

3030
FLAKE8_VERSION = "flake8==6.1.0"
3131
BLACK_VERSION = "black[jupyter]==23.7.0"
32+
RUFF_VERSION = "ruff==0.14.14"
3233
ISORT_VERSION = "isort==5.11.0"
3334
LINT_PATHS = ["docs", "google_auth_oauthlib", "tests", "noxfile.py", "setup.py"]
3435

@@ -103,19 +104,29 @@ def blacken(session):
103104
@nox.session(python=DEFAULT_PYTHON_VERSION)
104105
def format(session):
105106
"""
106-
Run isort to sort imports. Then run black
107-
to format code to uniform standard.
107+
Run ruff to sort imports and format code.
108108
"""
109-
session.install(BLACK_VERSION, ISORT_VERSION)
110-
# Use the --fss option to sort imports using strict alphabetical order.
111-
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
109+
# 1. Install ruff (skipped automatically if you run with --no-venv)
110+
session.install(RUFF_VERSION)
111+
112+
# 2. Run Ruff to fix imports
112113
session.run(
113-
"isort",
114-
"--fss",
114+
"ruff",
115+
"check",
116+
"--select",
117+
"I",
118+
"--fix",
119+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
120+
"--line-length=88",
115121
*LINT_PATHS,
116122
)
123+
124+
# 3. Run Ruff to format code
117125
session.run(
118-
"black",
126+
"ruff",
127+
"format",
128+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
129+
"--line-length=88",
119130
*LINT_PATHS,
120131
)
121132

packages/google-auth/noxfile.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
CLICK_VERSION = "click"
2424
BLACK_VERSION = "black==23.7.0"
25+
RUFF_VERSION = "ruff==0.14.14"
2526
BLACK_PATHS = [
2627
"google",
2728
"tests",
@@ -51,6 +52,7 @@
5152
nox.options.sessions = [
5253
"lint",
5354
"blacken",
55+
"format",
5456
"mypy",
5557
# cover must be last to avoid error `No data to report`
5658
"docs",
@@ -97,6 +99,36 @@ def blacken(session):
9799
session.run("black", *BLACK_PATHS)
98100

99101

102+
@nox.session(python=DEFAULT_PYTHON_VERSION)
103+
def format(session):
104+
"""
105+
Run ruff to sort imports and format code.
106+
"""
107+
# 1. Install ruff (skipped automatically if you run with --no-venv)
108+
session.install(RUFF_VERSION)
109+
110+
# 2. Run Ruff to fix imports
111+
session.run(
112+
"ruff",
113+
"check",
114+
"--select",
115+
"I",
116+
"--fix",
117+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
118+
"--line-length=88",
119+
*BLACK_PATHS,
120+
)
121+
122+
# 3. Run Ruff to format code
123+
session.run(
124+
"ruff",
125+
"format",
126+
f"--target-version=py{ALL_PYTHON[0].replace('.', '')}",
127+
"--line-length=88",
128+
*BLACK_PATHS,
129+
)
130+
131+
100132
@nox.session(python=DEFAULT_PYTHON_VERSION)
101133
def mypy(session):
102134
"""Verify type hints are mypy compatible."""

packages/google-cloud-core/noxfile.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
# limitations under the License.
1414

1515
from __future__ import absolute_import
16+
1617
import os
1718
import re
1819
import shutil
1920

2021
import nox
2122

22-
2323
BLACK_VERSION = "black==23.7.0"
24+
RUFF_VERSION = "ruff==0.14.14"
2425
BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
2526

2627
DEFAULT_PYTHON_VERSION = "3.14"
@@ -75,6 +76,36 @@ def blacken(session):
7576
session.run("black", *BLACK_PATHS)
7677

7778

79+
@nox.session(python=DEFAULT_PYTHON_VERSION)
80+
def format(session):
81+
"""
82+
Run ruff to sort imports and format code.
83+
"""
84+
# 1. Install ruff (skipped automatically if you run with --no-venv)
85+
session.install(RUFF_VERSION)
86+
87+
# 2. Run Ruff to fix imports
88+
session.run(
89+
"ruff",
90+
"check",
91+
"--select",
92+
"I",
93+
"--fix",
94+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
95+
"--line-length=88",
96+
*BLACK_PATHS,
97+
)
98+
99+
# 3. Run Ruff to format code
100+
session.run(
101+
"ruff",
102+
"format",
103+
f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}",
104+
"--line-length=88",
105+
*BLACK_PATHS,
106+
)
107+
108+
78109
def default(session):
79110
"""Default unit test session.
80111
This is intended to be run **without** an interpreter set, so
@@ -342,7 +373,7 @@ def core_deps_from_source(session, protobuf_implementation):
342373
f"{CURRENT_DIRECTORY}/../google-api-core",
343374
f"{CURRENT_DIRECTORY}/../google-auth",
344375
f"{CURRENT_DIRECTORY}/../grpc-google-iam-v1",
345-
f"{CURRENT_DIRECTORY}/../proto-plus"
376+
f"{CURRENT_DIRECTORY}/../proto-plus",
346377
]
347378

348379
for dep in core_dependencies_from_source:

0 commit comments

Comments
 (0)