From 85000e168ef05173cfe1ae64022127f5c804fcdf Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 13:40:53 -0800 Subject: [PATCH 1/6] Added black, isort, and flake8 to the PR process. Also added a script to manually and locally run them --- .flake8 | 4 + .github/workflows/python-checks.yml | 43 ++++++++ pyproject.toml | 25 +++++ python/README.md | 21 ++++ python/python-checks.sh | 159 ++++++++++++++++++++++++++++ 5 files changed, 252 insertions(+) create mode 100644 .flake8 create mode 100644 .github/workflows/python-checks.yml create mode 100644 pyproject.toml create mode 100755 python/python-checks.sh diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000..f14d6ef1e --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 88 +exclude = .git,__pycache__,docs,venv +ignore = E203, W503, E501 \ No newline at end of file diff --git a/.github/workflows/python-checks.yml b/.github/workflows/python-checks.yml new file mode 100644 index 000000000..4a877b760 --- /dev/null +++ b/.github/workflows/python-checks.yml @@ -0,0 +1,43 @@ +name: Python Code Quality Checks + +on: + pull_request: + branches: + - main + # Only run the workflow if files in these specific Python directories are changed + paths: + - 'python/agents/**.py' + - 'python/notebooks/**.py' + - 'python/notebooks/**.ipynb' + +jobs: + python-checks: + runs-on: ubuntu-latest + + steps: + - name: โฌ‡๏ธ Checkout Repository + uses: actions/checkout@v4 + + - name: ๐Ÿ Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: โš™๏ธ Install Tools (Matching Makefile) + run: pip install black flake8 isort nbqa + + # --- Black/iSort Check for .py files --- + - name: ๐Ÿ“ Run Black and iSort Checks + run: | + black --check --diff python/agents/ python/notebooks/ + isort --check-only --diff python/agents/ python/notebooks/ + + # --- Flake8 Check for all .py files --- + - name: ๐Ÿงน Run Flake8 Linting + run: flake8 ./python/ + + # --- Black/iSort Check for .ipynb files (using nbqa) --- + - name: ๐Ÿ““ Check Notebooks (nbqa) + run: | + nbqa black --check --diff python/notebooks/ + nbqa isort --check-only --diff python/notebooks/ \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..48feb7b59 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[tool.black] +line-length = 88 +target-version = ['py38', 'py39', 'py310'] +include = '\.pyi?$' +exclude = ''' +/( + \.git + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | _build + | buck-out + | build +)/ +''' + +[tool.isort] +profile = "black" +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +ensure_newline_before_comments = true +line_length = 88 \ No newline at end of file diff --git a/python/README.md b/python/README.md index 9c8dd44c9..48b97b032 100644 --- a/python/README.md +++ b/python/README.md @@ -81,6 +81,27 @@ other models for these samples. โ”‚ โ””โ”€โ”€ README.md # This file (Repository overview) ``` +## ๐Ÿ“ Code Quality Checks + +We use automated checks to ensure high quality and consistency across all code samples. + +### ๐Ÿ Python Contributor Pre-Check + +Before submitting a Pull Request with changes to Python files (in `python/agents` or `python/notebooks`), please run the following script locally to catch any formatting or linting errors: + +1. Ensure you have Python installed. +2. Run the check script from the repository root: + +```bash +./python-checks.sh --run # run all checks +./python-checks.sh --run all # run all checks +./python-checks.sh --run black # run black only +./python-checks.sh --run isort flake8 # run isort and flake8 +./python-checks.sh --help # for more info +``` + +This script will run `black`, `isort` and `flake8` to check for formatting and linting errors. + ## โ„น๏ธ Getting help If you have any questions or if you found any problems with this repository, diff --git a/python/python-checks.sh b/python/python-checks.sh new file mode 100755 index 000000000..d83742eec --- /dev/null +++ b/python/python-checks.sh @@ -0,0 +1,159 @@ +#!/bin/bash +# +# check-python.sh: Runs formatting and linting checks on Python files based on arguments. +# +set -e # <-- RE-ENABLED: Exit immediately if a command exits with a non-zero status. + +# --- Configuration --- +AGENTS_DIR="./agents" +NOTEBOOKS_DIR="./notebooks" +CHECKS_TO_RUN=() +RUN_COMMAND=false + +# --- Function Definitions (unchanged) --- + +show_help() { + echo " +Usage: ./check-python.sh [OPTION] [CHECK]... + +Runs Python formatting (Black, iSort) and linting (Flake8) checks +on the 'python/agents/' and 'python/notebooks/' directories. + +Options: + --help Displays this help message. + --run [CHECK]... Executes the specified checks. If no CHECK is provided, + it defaults to running all checks (black, isort, and flake8). + +Available Checks (CHECK): + all Runs all checks: black, isort, and flake8. + black Runs the Black auto-formatter check only. + isort Runs the iSort import sorter check only. + lint, flake8 Runs the Flake8 linter check only. (Aliases for linting) +" + exit 0 +} + +run_black() { + echo -e "\n--- Running Black Formatting Check (.py files) ---" + black --check --diff $AGENTS_DIR + + echo -e "\n--- Running Black Check on Notebooks (.ipynb) ---" + nbqa black --check --diff $NOTEBOOKS_DIR +} + +run_isort() { + echo -e "\n--- Running iSort Import Check (.py files) ---" + isort --check-only --diff $AGENTS_DIR + + echo -e "\n--- Running iSort Check on Notebooks (.ipynb) ---" + nbqa isort --check-only --diff $NOTEBOOKS_DIR +} + +run_flake8() { + echo -e "\n--- Running Flake8 Linting Check (.py files) ---" + flake8 $AGENTS_DIR + + echo -e "\n--- Running Flake8 Linting Check on Notebooks (.ipynb) ---" + nbqa flake8 $NOTEBOOKS_DIR +} + +# --- Argument Parsing Loop (unchanged) --- + +while [[ $# -gt 0 ]]; do + case "$1" in + --help) + show_help + ;; + --run) + RUN_COMMAND=true + # Shift the --run option off, leaving only the checks + shift + # Break the loop here to process the remaining arguments as checks + break + ;; + *) + # If we hit an argument we don't recognize, show error/help + echo "Error: Unknown option '$1'. Use './check-python.sh --help' for options." + exit 1 + ;; + esac + # Shift to the next argument + shift +done + +# --- Main Execution (The necessary logic that was missing) --- + +# Handle case where only './check-python.sh' is run without options +if [[ "$RUN_COMMAND" == false && "$#" -eq 0 ]]; then + echo "Error: Missing '--run' option. Use './check-python.sh --help' for options." + exit 1 +fi + +# After parsing, all remaining arguments are stored in positional parameters ($@) +CHECKS_TO_RUN=("$@") + +# If --run was used with no further arguments, default to 'all' +if [[ "$RUN_COMMAND" == true && ${#CHECKS_TO_RUN[@]} -eq 0 ]]; then + CHECKS_TO_RUN=("all") +fi + +echo "Starting local Python code quality checks..." + +# --- Tool Installation Check (FIXED LOGIC) --- +REQUIRED_TOOLS="black flake8 isort nbqa" +TOOLS_MISSING=0 + +for tool in $REQUIRED_TOOLS; do + if ! command -v $tool &> /dev/null; then + TOOLS_MISSING=1 + break + fi +done + +if [ $TOOLS_MISSING -eq 1 ]; then + echo "Installing required Python tools ($REQUIRED_TOOLS)..." + # Use python3 -m pip install for maximum environment safety + python3 -m pip install $REQUIRED_TOOLS +fi + +# --- Determine which checks to run --- +RUN_ALL=false +CLEAN_CHECKS=() +for check in "${CHECKS_TO_RUN[@]}"; do + case "$check" in + all) + RUN_ALL=true + break + ;; + black | isort | lint | flake8) + CLEAN_CHECKS+=("$check") + ;; + *) + echo "Error: Unknown check '$check'. Use './check-python.sh --help' for valid checks." + exit 1 + ;; + esac +done + +# --- Execute Checks (THE MISSING LOGIC) --- +if $RUN_ALL; then + run_black + run_isort + run_flake8 +else + for check in "${CLEAN_CHECKS[@]}"; do + case "$check" in + black) + run_black + ;; + isort) + run_isort + ;; + lint | flake8) + run_flake8 + ;; + esac + done +fi + +echo -e "\nโœ… All requested Python checks completed successfully!" \ No newline at end of file From 5e97e22b8aa5e9c4e2598920984a218601b9bece Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 13:52:12 -0800 Subject: [PATCH 2/6] Manually changed a python file to trigger the python hook --- python/agents/antom-payment/antom-payemnt-agent/agent.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/agents/antom-payment/antom-payemnt-agent/agent.py b/python/agents/antom-payment/antom-payemnt-agent/agent.py index 6ac6454d9..39b197973 100644 --- a/python/agents/antom-payment/antom-payemnt-agent/agent.py +++ b/python/agents/antom-payment/antom-payemnt-agent/agent.py @@ -1,7 +1,9 @@ -import os -from google.adk.agents import Agent from google.adk.tools import MCPToolset from google.adk.tools.mcp_tool import StdioConnectionParams + + +import os +from google.adk.agents import Agent from mcp import StdioServerParameters root_agent = Agent( From 5b901283ada864cb0374f13b61be02cd08c68b3c Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 13:53:52 -0800 Subject: [PATCH 3/6] Undid the changed in the python file. --- python/agents/antom-payment/antom-payemnt-agent/agent.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/agents/antom-payment/antom-payemnt-agent/agent.py b/python/agents/antom-payment/antom-payemnt-agent/agent.py index 39b197973..6ac6454d9 100644 --- a/python/agents/antom-payment/antom-payemnt-agent/agent.py +++ b/python/agents/antom-payment/antom-payemnt-agent/agent.py @@ -1,9 +1,7 @@ -from google.adk.tools import MCPToolset -from google.adk.tools.mcp_tool import StdioConnectionParams - - import os from google.adk.agents import Agent +from google.adk.tools import MCPToolset +from google.adk.tools.mcp_tool import StdioConnectionParams from mcp import StdioServerParameters root_agent = Agent( From cc28d08e5188fd403e4756b4be2066e1fb32fc34 Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 14:48:53 -0800 Subject: [PATCH 4/6] Undid previous changes --- .flake8 | 4 - python/README.md | 15 ---- python/python-checks.sh | 159 ---------------------------------------- 3 files changed, 178 deletions(-) delete mode 100644 .flake8 delete mode 100755 python/python-checks.sh diff --git a/.flake8 b/.flake8 deleted file mode 100644 index f14d6ef1e..000000000 --- a/.flake8 +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max-line-length = 88 -exclude = .git,__pycache__,docs,venv -ignore = E203, W503, E501 \ No newline at end of file diff --git a/python/README.md b/python/README.md index 48b97b032..10ef82c62 100644 --- a/python/README.md +++ b/python/README.md @@ -85,21 +85,6 @@ other models for these samples. We use automated checks to ensure high quality and consistency across all code samples. -### ๐Ÿ Python Contributor Pre-Check - -Before submitting a Pull Request with changes to Python files (in `python/agents` or `python/notebooks`), please run the following script locally to catch any formatting or linting errors: - -1. Ensure you have Python installed. -2. Run the check script from the repository root: - -```bash -./python-checks.sh --run # run all checks -./python-checks.sh --run all # run all checks -./python-checks.sh --run black # run black only -./python-checks.sh --run isort flake8 # run isort and flake8 -./python-checks.sh --help # for more info -``` - This script will run `black`, `isort` and `flake8` to check for formatting and linting errors. ## โ„น๏ธ Getting help diff --git a/python/python-checks.sh b/python/python-checks.sh deleted file mode 100755 index d83742eec..000000000 --- a/python/python-checks.sh +++ /dev/null @@ -1,159 +0,0 @@ -#!/bin/bash -# -# check-python.sh: Runs formatting and linting checks on Python files based on arguments. -# -set -e # <-- RE-ENABLED: Exit immediately if a command exits with a non-zero status. - -# --- Configuration --- -AGENTS_DIR="./agents" -NOTEBOOKS_DIR="./notebooks" -CHECKS_TO_RUN=() -RUN_COMMAND=false - -# --- Function Definitions (unchanged) --- - -show_help() { - echo " -Usage: ./check-python.sh [OPTION] [CHECK]... - -Runs Python formatting (Black, iSort) and linting (Flake8) checks -on the 'python/agents/' and 'python/notebooks/' directories. - -Options: - --help Displays this help message. - --run [CHECK]... Executes the specified checks. If no CHECK is provided, - it defaults to running all checks (black, isort, and flake8). - -Available Checks (CHECK): - all Runs all checks: black, isort, and flake8. - black Runs the Black auto-formatter check only. - isort Runs the iSort import sorter check only. - lint, flake8 Runs the Flake8 linter check only. (Aliases for linting) -" - exit 0 -} - -run_black() { - echo -e "\n--- Running Black Formatting Check (.py files) ---" - black --check --diff $AGENTS_DIR - - echo -e "\n--- Running Black Check on Notebooks (.ipynb) ---" - nbqa black --check --diff $NOTEBOOKS_DIR -} - -run_isort() { - echo -e "\n--- Running iSort Import Check (.py files) ---" - isort --check-only --diff $AGENTS_DIR - - echo -e "\n--- Running iSort Check on Notebooks (.ipynb) ---" - nbqa isort --check-only --diff $NOTEBOOKS_DIR -} - -run_flake8() { - echo -e "\n--- Running Flake8 Linting Check (.py files) ---" - flake8 $AGENTS_DIR - - echo -e "\n--- Running Flake8 Linting Check on Notebooks (.ipynb) ---" - nbqa flake8 $NOTEBOOKS_DIR -} - -# --- Argument Parsing Loop (unchanged) --- - -while [[ $# -gt 0 ]]; do - case "$1" in - --help) - show_help - ;; - --run) - RUN_COMMAND=true - # Shift the --run option off, leaving only the checks - shift - # Break the loop here to process the remaining arguments as checks - break - ;; - *) - # If we hit an argument we don't recognize, show error/help - echo "Error: Unknown option '$1'. Use './check-python.sh --help' for options." - exit 1 - ;; - esac - # Shift to the next argument - shift -done - -# --- Main Execution (The necessary logic that was missing) --- - -# Handle case where only './check-python.sh' is run without options -if [[ "$RUN_COMMAND" == false && "$#" -eq 0 ]]; then - echo "Error: Missing '--run' option. Use './check-python.sh --help' for options." - exit 1 -fi - -# After parsing, all remaining arguments are stored in positional parameters ($@) -CHECKS_TO_RUN=("$@") - -# If --run was used with no further arguments, default to 'all' -if [[ "$RUN_COMMAND" == true && ${#CHECKS_TO_RUN[@]} -eq 0 ]]; then - CHECKS_TO_RUN=("all") -fi - -echo "Starting local Python code quality checks..." - -# --- Tool Installation Check (FIXED LOGIC) --- -REQUIRED_TOOLS="black flake8 isort nbqa" -TOOLS_MISSING=0 - -for tool in $REQUIRED_TOOLS; do - if ! command -v $tool &> /dev/null; then - TOOLS_MISSING=1 - break - fi -done - -if [ $TOOLS_MISSING -eq 1 ]; then - echo "Installing required Python tools ($REQUIRED_TOOLS)..." - # Use python3 -m pip install for maximum environment safety - python3 -m pip install $REQUIRED_TOOLS -fi - -# --- Determine which checks to run --- -RUN_ALL=false -CLEAN_CHECKS=() -for check in "${CHECKS_TO_RUN[@]}"; do - case "$check" in - all) - RUN_ALL=true - break - ;; - black | isort | lint | flake8) - CLEAN_CHECKS+=("$check") - ;; - *) - echo "Error: Unknown check '$check'. Use './check-python.sh --help' for valid checks." - exit 1 - ;; - esac -done - -# --- Execute Checks (THE MISSING LOGIC) --- -if $RUN_ALL; then - run_black - run_isort - run_flake8 -else - for check in "${CLEAN_CHECKS[@]}"; do - case "$check" in - black) - run_black - ;; - isort) - run_isort - ;; - lint | flake8) - run_flake8 - ;; - esac - done -fi - -echo -e "\nโœ… All requested Python checks completed successfully!" \ No newline at end of file From a86b12c57ad557408a3240627296310ffff1daef Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 14:49:50 -0800 Subject: [PATCH 5/6] Undid previous changes --- pyproject.toml | 25 ------------------------- python/README.md | 5 ----- 2 files changed, 30 deletions(-) delete mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index 48feb7b59..000000000 --- a/pyproject.toml +++ /dev/null @@ -1,25 +0,0 @@ -[tool.black] -line-length = 88 -target-version = ['py38', 'py39', 'py310'] -include = '\.pyi?$' -exclude = ''' -/( - \.git - | \.hg - | \.mypy_cache - | \.tox - | \.venv - | _build - | buck-out - | build -)/ -''' - -[tool.isort] -profile = "black" -multi_line_output = 3 -include_trailing_comma = true -force_grid_wrap = 0 -use_parentheses = true -ensure_newline_before_comments = true -line_length = 88 \ No newline at end of file diff --git a/python/README.md b/python/README.md index 10ef82c62..c2ff5ea32 100644 --- a/python/README.md +++ b/python/README.md @@ -81,11 +81,6 @@ other models for these samples. โ”‚ โ””โ”€โ”€ README.md # This file (Repository overview) ``` -## ๐Ÿ“ Code Quality Checks - -We use automated checks to ensure high quality and consistency across all code samples. - -This script will run `black`, `isort` and `flake8` to check for formatting and linting errors. ## โ„น๏ธ Getting help From b4b754ba9df8200a493cbb6d8e591a03f5e7316b Mon Sep 17 00:00:00 2001 From: Shahin Saadati Date: Wed, 19 Nov 2025 14:50:16 -0800 Subject: [PATCH 6/6] Undid previous changes --- python/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/python/README.md b/python/README.md index c2ff5ea32..9c8dd44c9 100644 --- a/python/README.md +++ b/python/README.md @@ -81,7 +81,6 @@ other models for these samples. โ”‚ โ””โ”€โ”€ README.md # This file (Repository overview) ``` - ## โ„น๏ธ Getting help If you have any questions or if you found any problems with this repository,