|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import nox |
| 20 | + |
| 21 | +# Use a stable Python version for running the style utilities |
| 22 | +LINTING_VERSION = "3.10" |
| 23 | + |
| 24 | +# Error out if the runner is missing the target python interpreter |
| 25 | +nox.options.error_on_missing_interpreters = True |
| 26 | + |
| 27 | + |
| 28 | +def _determine_local_import_names(start_dir: str) -> list[str]: |
| 29 | + """Determines local import names to assist Flake8 with import order checks.""" |
| 30 | + try: |
| 31 | + file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)] |
| 32 | + return [ |
| 33 | + basename |
| 34 | + for basename, extension in file_ext_pairs |
| 35 | + if extension == ".py" |
| 36 | + or (os.path.isdir(os.path.join(start_dir, basename)) and basename != "__pycache__") |
| 37 | + ] |
| 38 | + except Exception: |
| 39 | + return [] |
| 40 | + |
| 41 | +# Linting with flake8. |
| 42 | +# |
| 43 | +# We ignore the following rules: |
| 44 | +# ANN101: missing type annotation for `self` in method |
| 45 | +# ANN102: missing type annotation for `cls` in method |
| 46 | +# E203: whitespace before ‘:’ |
| 47 | +# E266: too many leading ‘#’ for block comment |
| 48 | +# E501: line too long |
| 49 | +# I202: Additional newline in a section of imports |
| 50 | +# |
| 51 | +# We also need to specify the rules which are ignored by default: |
| 52 | +# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] |
| 53 | +# |
| 54 | +# For more information see: https://pypi.org/project/flake8-annotations |
| 55 | + |
| 56 | +# Standardize style configuration parameters |
| 57 | +FLAKE8_COMMON_ARGS = [ |
| 58 | + "--show-source", |
| 59 | + "--builtin=gettext", |
| 60 | + "--max-complexity=20", |
| 61 | + "--import-order-style=google", |
| 62 | + "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", |
| 63 | + "--ignore=ANN101,ANN102,E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", |
| 64 | + "--max-line-length=88", |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +@nox.session(python=LINTING_VERSION) |
| 69 | +def lint(session: nox.sessions.Session) -> None: |
| 70 | + """Runs flake8 linting checks. Honors incremental PR file arguments.""" |
| 71 | + session.install("flake8", "flake8-import-order") |
| 72 | + |
| 73 | + local_names = _determine_local_import_names(".") |
| 74 | + args = FLAKE8_COMMON_ARGS + [ |
| 75 | + "--application-import-names", |
| 76 | + ",".join(local_names), |
| 77 | + ] |
| 78 | + |
| 79 | + if session.posargs: |
| 80 | + args.extend(session.posargs) |
| 81 | + else: |
| 82 | + args.append(".") |
| 83 | + |
| 84 | + session.run("flake8", *args) |
| 85 | + |
| 86 | + |
| 87 | +@nox.session(python=LINTING_VERSION) |
| 88 | +def blacken(session: nox.sessions.Session) -> None: |
| 89 | + """Runs black code formatting checks. Honors incremental PR file arguments.""" |
| 90 | + session.install("black") |
| 91 | + |
| 92 | + # If explicit target files are passed via posargs, target ONLY those files. |
| 93 | + if session.posargs: |
| 94 | + targets = session.posargs |
| 95 | + else: |
| 96 | + # Fallback to scanning immediate root Python files if run purely locally without args |
| 97 | + targets = [path for path in os.listdir(".") if path.endswith(".py")] |
| 98 | + |
| 99 | + if targets: |
| 100 | + session.run("black", *targets) |
| 101 | + else: |
| 102 | + session.log("No specific Python targets identified for formatting validations.") |
0 commit comments