Skip to content

Commit 0e46d6b

Browse files
chore(python): add nox session to sort python imports (#721)
Source-Link: googleapis/synthtool@1b71c10 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d395b65 commit 0e46d6b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

packages/google-cloud-spanner/.github/.OwlBot.lock.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
# limitations under the License.
1414
docker:
1515
image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest
16-
digest: sha256:266a3407f0bb34374f49b6556ee20ee819374587246dcc19405b502ec70113b6
16+
digest: sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416
17+
# created: 2022-04-20T23:42:53.970438194Z

packages/google-cloud-spanner/noxfile.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import nox
2626

2727
BLACK_VERSION = "black==22.3.0"
28-
BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
28+
ISORT_VERSION = "isort==5.10.1"
29+
LINT_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
2930

3031
DEFAULT_PYTHON_VERSION = "3.8"
3132

@@ -85,7 +86,7 @@ def lint(session):
8586
session.run(
8687
"black",
8788
"--check",
88-
*BLACK_PATHS,
89+
*LINT_PATHS,
8990
)
9091
session.run("flake8", "google", "tests")
9192

@@ -96,7 +97,27 @@ def blacken(session):
9697
session.install(BLACK_VERSION)
9798
session.run(
9899
"black",
99-
*BLACK_PATHS,
100+
*LINT_PATHS,
101+
)
102+
103+
104+
@nox.session(python=DEFAULT_PYTHON_VERSION)
105+
def format(session):
106+
"""
107+
Run isort to sort imports. Then run black
108+
to format code to uniform standard.
109+
"""
110+
session.install(BLACK_VERSION, ISORT_VERSION)
111+
# Use the --fss option to sort imports using strict alphabetical order.
112+
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
113+
session.run(
114+
"isort",
115+
"--fss",
116+
*LINT_PATHS,
117+
)
118+
session.run(
119+
"black",
120+
*LINT_PATHS,
100121
)
101122

102123

packages/google-cloud-spanner/samples/samples/noxfile.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# WARNING - WARNING - WARNING - WARNING - WARNING
3131

3232
BLACK_VERSION = "black==22.3.0"
33+
ISORT_VERSION = "isort==5.10.1"
3334

3435
# Copy `noxfile_config.py` to your directory and modify it instead.
3536

@@ -168,12 +169,32 @@ def lint(session: nox.sessions.Session) -> None:
168169

169170
@nox.session
170171
def blacken(session: nox.sessions.Session) -> None:
172+
"""Run black. Format code to uniform standard."""
171173
session.install(BLACK_VERSION)
172174
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
173175

174176
session.run("black", *python_files)
175177

176178

179+
#
180+
# format = isort + black
181+
#
182+
183+
@nox.session
184+
def format(session: nox.sessions.Session) -> None:
185+
"""
186+
Run isort to sort imports. Then run black
187+
to format code to uniform standard.
188+
"""
189+
session.install(BLACK_VERSION, ISORT_VERSION)
190+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
191+
192+
# Use the --fss option to sort imports using strict alphabetical order.
193+
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
194+
session.run("isort", "--fss", *python_files)
195+
session.run("black", *python_files)
196+
197+
177198
#
178199
# Sample Tests
179200
#

0 commit comments

Comments
 (0)