Skip to content

Commit 1b71c10

Browse files
authored
chore(python): add nox session to sort python imports (#1402)
1 parent c00a2df commit 1b71c10

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

synthtool/gcp/templates/python_library/noxfile.py.j2

+24-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import warnings
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="{{ default_python_version }}"
3132

@@ -105,7 +106,7 @@ def lint(session):
105106
session.run(
106107
"black",
107108
"--check",
108-
*BLACK_PATHS,
109+
*LINT_PATHS,
109110
)
110111
session.run("flake8", "google", "tests")
111112

@@ -116,7 +117,27 @@ def blacken(session):
116117
session.install(BLACK_VERSION)
117118
session.run(
118119
"black",
119-
*BLACK_PATHS,
120+
*LINT_PATHS,
121+
)
122+
123+
124+
@nox.session(python=DEFAULT_PYTHON_VERSION)
125+
def format(session):
126+
"""
127+
Run isort to sort imports. Then run black
128+
to format code to uniform standard.
129+
"""
130+
session.install(BLACK_VERSION, ISORT_VERSION)
131+
# Use the --fss option to sort imports using strict alphabetical order.
132+
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
133+
session.run(
134+
"isort",
135+
"--fss",
136+
*LINT_PATHS,
137+
)
138+
session.run(
139+
"black",
140+
*LINT_PATHS,
120141
)
121142

122143

synthtool/gcp/templates/python_samples/noxfile.py.j2

+21
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import nox
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
#

synthtool/languages/python.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ def owlbot_main() -> None:
268268

269269
py_samples(skip_readmes=True)
270270

271-
# run blacken session for all directories which a noxfile
271+
# run format nox session for all directories which have a noxfile
272272
for noxfile in Path(".").glob("**/noxfile.py"):
273-
s.shell.run(["nox", "-s", "blacken"], cwd=noxfile.parent, hide_output=False)
273+
s.shell.run(["nox", "-s", "format"], cwd=noxfile.parent, hide_output=False)
274274

275275
configure_previous_major_version_branches()
276276

0 commit comments

Comments
 (0)