Skip to content

Commit e6b9803

Browse files
chore(python): add nox session to sort python imports (#102)
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 b8d5db2 commit e6b9803

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

.github/.OwlBot.lock.yaml

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

noxfile.py

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

2828
BLACK_VERSION = "black==22.3.0"
29-
BLACK_PATHS = ["docs", "db_dtypes", "tests", "noxfile.py", "setup.py"]
29+
ISORT_VERSION = "isort==5.10.1"
30+
LINT_PATHS = ["docs", "db_dtypes", "tests", "noxfile.py", "setup.py"]
3031

3132
DEFAULT_PYTHON_VERSION = "3.8"
3233

@@ -86,7 +87,7 @@ def lint(session):
8687
session.run(
8788
"black",
8889
"--check",
89-
*BLACK_PATHS,
90+
*LINT_PATHS,
9091
)
9192
session.run("flake8", "db_dtypes", "tests")
9293

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

103124

samples/snippets/noxfile.py

Lines changed: 22 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,33 @@ 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+
184+
@nox.session
185+
def format(session: nox.sessions.Session) -> None:
186+
"""
187+
Run isort to sort imports. Then run black
188+
to format code to uniform standard.
189+
"""
190+
session.install(BLACK_VERSION, ISORT_VERSION)
191+
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
192+
193+
# Use the --fss option to sort imports using strict alphabetical order.
194+
# See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
195+
session.run("isort", "--fss", *python_files)
196+
session.run("black", *python_files)
197+
198+
177199
#
178200
# Sample Tests
179201
#

0 commit comments

Comments
 (0)