From f7ea333ac49cc917af303a4c990405411e9daf6d Mon Sep 17 00:00:00 2001
From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com>
Date: Wed, 20 Apr 2022 20:53:00 -0400
Subject: [PATCH] chore(python): add nox session to sort python imports (#229)

Source-Link: https://github.com/googleapis/synthtool/commit/1b71c10e20de7ed3f97f692f99a0e3399b67049f
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>
---
 container/snippets/create_cluster_test.py |  2 --
 container/snippets/delete_cluster_test.py |  2 --
 container/snippets/noxfile.py             | 23 ++++++++++++++++++++++-
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/container/snippets/create_cluster_test.py b/container/snippets/create_cluster_test.py
index 728c07509de6..75a281c96015 100644
--- a/container/snippets/create_cluster_test.py
+++ b/container/snippets/create_cluster_test.py
@@ -17,9 +17,7 @@
 import uuid
 
 import backoff
-
 from google.cloud import container_v1 as gke
-
 import pytest
 
 import create_cluster as gke_create
diff --git a/container/snippets/delete_cluster_test.py b/container/snippets/delete_cluster_test.py
index fc7845d3465e..5e187d84de1a 100644
--- a/container/snippets/delete_cluster_test.py
+++ b/container/snippets/delete_cluster_test.py
@@ -17,10 +17,8 @@
 import uuid
 
 import backoff
-
 from google.api_core import exceptions as googleEx
 from google.cloud import container_v1 as gke
-
 import pytest
 
 import delete_cluster as gke_delete
diff --git a/container/snippets/noxfile.py b/container/snippets/noxfile.py
index 25f87a215d4c..3b3ffa5d2b0f 100644
--- a/container/snippets/noxfile.py
+++ b/container/snippets/noxfile.py
@@ -22,7 +22,6 @@
 
 import nox
 
-
 # WARNING - WARNING - WARNING - WARNING - WARNING
 # WARNING - WARNING - WARNING - WARNING - WARNING
 #           DO NOT EDIT THIS FILE EVER!
@@ -30,6 +29,7 @@
 # WARNING - WARNING - WARNING - WARNING - WARNING
 
 BLACK_VERSION = "black==22.3.0"
+ISORT_VERSION = "isort==5.10.1"
 
 # Copy `noxfile_config.py` to your directory and modify it instead.
 
@@ -168,12 +168,33 @@ def lint(session: nox.sessions.Session) -> None:
 
 @nox.session
 def blacken(session: nox.sessions.Session) -> None:
+    """Run black. Format code to uniform standard."""
     session.install(BLACK_VERSION)
     python_files = [path for path in os.listdir(".") if path.endswith(".py")]
 
     session.run("black", *python_files)
 
 
+#
+# format = isort + black
+#
+
+
+@nox.session
+def format(session: nox.sessions.Session) -> None:
+    """
+    Run isort to sort imports. Then run black
+    to format code to uniform standard.
+    """
+    session.install(BLACK_VERSION, ISORT_VERSION)
+    python_files = [path for path in os.listdir(".") if path.endswith(".py")]
+
+    # Use the --fss option to sort imports using strict alphabetical order.
+    # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections
+    session.run("isort", "--fss", *python_files)
+    session.run("black", *python_files)
+
+
 #
 # Sample Tests
 #