Skip to content

Commit a3f6375

Browse files
chore(python): add nox session to sort python imports (#217)
* chore(python): add nox session to sort python imports Source-Link: googleapis/synthtool@1b71c10 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 * add requirements.txt * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent db71bb0 commit a3f6375

14 files changed

+122
-19
lines changed

generated_samples/interactive-tutorials/events/import_user_events_gcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def main(bucket_name):
2121
# Import user events into a catalog from GCS using Retail API
2222

2323
import time
24-
import google.auth
2524

25+
import google.auth
2626
from google.cloud.retail import (
2727
GcsSource,
2828
ImportErrorsConfig,

generated_samples/interactive-tutorials/events/import_user_events_inline.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
import time
2323

2424
import google.auth
25-
2625
from google.cloud.retail import (
2726
ImportUserEventsRequest,
2827
UserEvent,
2928
UserEventInlineSource,
3029
UserEventInputConfig,
3130
UserEventServiceClient,
3231
)
33-
3432
from google.protobuf.timestamp_pb2 import Timestamp
3533

3634
project_id = google.auth.default()[1]

generated_samples/interactive-tutorials/events/noxfile.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
import nox
2424

25-
2625
# WARNING - WARNING - WARNING - WARNING - WARNING
2726
# WARNING - WARNING - WARNING - WARNING - WARNING
2827
# DO NOT EDIT THIS FILE EVER!
2928
# WARNING - WARNING - WARNING - WARNING - WARNING
3029
# WARNING - WARNING - WARNING - WARNING - WARNING
3130

3231
BLACK_VERSION = "black==22.3.0"
32+
ISORT_VERSION = "isort==5.10.1"
3333

3434
# Copy `noxfile_config.py` to your directory and modify it instead.
3535

@@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
168168

169169
@nox.session
170170
def blacken(session: nox.sessions.Session) -> None:
171+
"""Run black. Format code to uniform standard."""
171172
session.install(BLACK_VERSION)
172173
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
173174

174175
session.run("black", *python_files)
175176

176177

178+
#
179+
# format = isort + black
180+
#
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
#

generated_samples/interactive-tutorials/events/purge_user_event.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# Import user events into a catalog from inline source using Retail API
1818
#
1919
import google.auth
20-
2120
from google.cloud.retail import PurgeUserEventsRequest, UserEventServiceClient
2221

2322
from setup_events.setup_cleanup import write_user_event

generated_samples/interactive-tutorials/events/rejoin_user_event.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# Import user events into a catalog from inline source using Retail API
1818
#
1919
import google.auth
20-
2120
from google.cloud.retail import RejoinUserEventsRequest, UserEventServiceClient
2221

2322
from setup_events.setup_cleanup import purge_user_event, write_user_event

generated_samples/interactive-tutorials/events/write_user_event.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import datetime
2020

2121
import google.auth
22-
2322
from google.cloud.retail import UserEvent, UserEventServiceClient, WriteUserEventRequest
24-
2523
from google.protobuf.timestamp_pb2 import Timestamp
2624

2725
from setup_events.setup_cleanup import purge_user_event

generated_samples/interactive-tutorials/noxfile.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414

1515
from __future__ import print_function
1616

17+
import glob
1718
import os
1819
from pathlib import Path
1920
import sys
2021
from typing import Callable, Dict, List, Optional
2122

2223
import nox
2324

24-
2525
# WARNING - WARNING - WARNING - WARNING - WARNING
2626
# WARNING - WARNING - WARNING - WARNING - WARNING
2727
# DO NOT EDIT THIS FILE EVER!
2828
# WARNING - WARNING - WARNING - WARNING - WARNING
2929
# WARNING - WARNING - WARNING - WARNING - WARNING
3030

3131
BLACK_VERSION = "black==22.3.0"
32+
ISORT_VERSION = "isort==5.10.1"
3233

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

@@ -64,7 +65,7 @@
6465
sys.path.append(".")
6566
from noxfile_config import TEST_CONFIG_OVERRIDE
6667
except ImportError as e:
67-
print(f"No user noxfile_config found: detail: {e}")
68+
print("No user noxfile_config found: detail: {}".format(e))
6869
TEST_CONFIG_OVERRIDE = {}
6970

7071
# Update the TEST_CONFIG with the user supplied values.
@@ -109,6 +110,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
109110

110111
def _determine_local_import_names(start_dir: str) -> List[str]:
111112
"""Determines all import names that should be considered "local".
113+
112114
This is used when running the linter to insure that import order is
113115
properly checked.
114116
"""
@@ -166,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
166168

167169
@nox.session
168170
def blacken(session: nox.sessions.Session) -> None:
171+
"""Run black. Format code to uniform standard."""
169172
session.install(BLACK_VERSION)
170173
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
171174

172175
session.run("black", *python_files)
173176

174177

178+
#
179+
# format = isort + black
180+
#
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+
175198
#
176199
# Sample Tests
177200
#
@@ -183,31 +206,49 @@ def blacken(session: nox.sessions.Session) -> None:
183206
def _session_tests(
184207
session: nox.sessions.Session, post_install: Callable = None
185208
) -> None:
209+
# check for presence of tests
210+
test_list = glob.glob("*_test.py") + glob.glob("test_*.py")
211+
test_list.extend(glob.glob("tests"))
212+
213+
if len(test_list) == 0:
214+
print("No tests found, skipping directory.")
215+
return
216+
186217
if TEST_CONFIG["pip_version_override"]:
187218
pip_version = TEST_CONFIG["pip_version_override"]
188219
session.install(f"pip=={pip_version}")
189220
"""Runs py.test for a particular project."""
221+
concurrent_args = []
190222
if os.path.exists("requirements.txt"):
191223
if os.path.exists("constraints.txt"):
192224
session.install("-r", "requirements.txt", "-c", "constraints.txt")
193225
else:
194226
session.install("-r", "requirements.txt")
227+
with open("requirements.txt") as rfile:
228+
packages = rfile.read()
195229

196230
if os.path.exists("requirements-test.txt"):
197231
if os.path.exists("constraints-test.txt"):
198232
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
199233
else:
200234
session.install("-r", "requirements-test.txt")
235+
with open("requirements-test.txt") as rtfile:
236+
packages += rtfile.read()
201237

202238
if INSTALL_LIBRARY_FROM_SOURCE:
203239
session.install("-e", _get_repo_root())
204240

205241
if post_install:
206242
post_install(session)
207243

244+
if "pytest-parallel" in packages:
245+
concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"])
246+
elif "pytest-xdist" in packages:
247+
concurrent_args.extend(["-n", "auto"])
248+
208249
session.run(
209250
"pytest",
210-
*(PYTEST_COMMON_ARGS + session.posargs),
251+
*(PYTEST_COMMON_ARGS + session.posargs + concurrent_args),
211252
# Pytest will return 5 when no tests are collected. This can happen
212253
# on travis where slow and flaky tests are excluded.
213254
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
@@ -222,7 +263,9 @@ def py(session: nox.sessions.Session) -> None:
222263
if session.python in TESTED_VERSIONS:
223264
_session_tests(session)
224265
else:
225-
session.skip(f"SKIPPED: {session.python} tests are disabled for this sample.")
266+
session.skip(
267+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
268+
)
226269

227270

228271
#

generated_samples/interactive-tutorials/product/create_product.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import string
2121

2222
import google.auth
23-
from google.cloud.retail import CreateProductRequest, Product, ProductServiceClient
24-
from google.cloud.retail import PriceInfo
23+
from google.cloud.retail import (
24+
CreateProductRequest,
25+
PriceInfo,
26+
Product,
27+
ProductServiceClient,
28+
)
2529
from google.cloud.retail_v2.types import product
2630

2731
from setup_product.setup_cleanup import delete_product

generated_samples/interactive-tutorials/product/crud_product.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
CreateProductRequest,
2525
DeleteProductRequest,
2626
GetProductRequest,
27+
PriceInfo,
2728
Product,
2829
ProductServiceClient,
2930
UpdateProductRequest,
3031
)
31-
from google.cloud.retail import PriceInfo
3232
from google.cloud.retail_v2.types import product
3333

3434
project_id = google.auth.default()[1]

generated_samples/interactive-tutorials/product/noxfile.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
import nox
2424

25-
2625
# WARNING - WARNING - WARNING - WARNING - WARNING
2726
# WARNING - WARNING - WARNING - WARNING - WARNING
2827
# DO NOT EDIT THIS FILE EVER!
2928
# WARNING - WARNING - WARNING - WARNING - WARNING
3029
# WARNING - WARNING - WARNING - WARNING - WARNING
3130

3231
BLACK_VERSION = "black==22.3.0"
32+
ISORT_VERSION = "isort==5.10.1"
3333

3434
# Copy `noxfile_config.py` to your directory and modify it instead.
3535

@@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
168168

169169
@nox.session
170170
def blacken(session: nox.sessions.Session) -> None:
171+
"""Run black. Format code to uniform standard."""
171172
session.install(BLACK_VERSION)
172173
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
173174

174175
session.run("black", *python_files)
175176

176177

178+
#
179+
# format = isort + black
180+
#
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
#

generated_samples/interactive-tutorials/product/set_inventory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
from google.cloud.retail import (
2323
FulfillmentInfo,
2424
PriceInfo,
25+
Product,
2526
ProductServiceClient,
2627
SetInventoryRequest,
2728
)
28-
from google.cloud.retail import Product
2929
from google.protobuf.field_mask_pb2 import FieldMask
3030

3131
from setup_product.setup_cleanup import create_product, delete_product, get_product

generated_samples/interactive-tutorials/requirements.txt

Whitespace-only changes.

generated_samples/interactive-tutorials/search/noxfile.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
import nox
2424

25-
2625
# WARNING - WARNING - WARNING - WARNING - WARNING
2726
# WARNING - WARNING - WARNING - WARNING - WARNING
2827
# DO NOT EDIT THIS FILE EVER!
2928
# WARNING - WARNING - WARNING - WARNING - WARNING
3029
# WARNING - WARNING - WARNING - WARNING - WARNING
3130

3231
BLACK_VERSION = "black==22.3.0"
32+
ISORT_VERSION = "isort==5.10.1"
3333

3434
# Copy `noxfile_config.py` to your directory and modify it instead.
3535

@@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
168168

169169
@nox.session
170170
def blacken(session: nox.sessions.Session) -> None:
171+
"""Run black. Format code to uniform standard."""
171172
session.install(BLACK_VERSION)
172173
python_files = [path for path in os.listdir(".") if path.endswith(".py")]
173174

174175
session.run("black", *python_files)
175176

176177

178+
#
179+
# format = isort + black
180+
#
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
#

generated_samples/interactive-tutorials/search/search_with_query_expansion_spec.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import google.auth
2121
from google.cloud.retail import SearchRequest, SearchServiceClient
2222

23-
2423
project_id = google.auth.default()[1]
2524

2625

0 commit comments

Comments
 (0)