Skip to content

Commit

Permalink
fix: formatting, linting, and schema code generation #1935
Browse files Browse the repository at this point in the history
ISSUE: #1935

CHANGELOG:
- [ ] Addresses error handling and parallelized formatting for toml
  files.
- [ ] Configures the ruff formatter to use requested format.
- [ ] Fixes snake_case for several Python variables.
- [ ] Configures the schemas.py code generator to generate snake_case
  field names to avoid lint reports and wraps the actual name in Field()
  metadata.
- [ ] Clean up some lint.
- [ ] Removes go-vulncheck from the pre-commit hook while retaining it
  during pre-push (easy to skip this using `-f --no-verify`).
  • Loading branch information
yesudeep committed Feb 13, 2025
1 parent 9d78019 commit 03bd1b9
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 182 deletions.
2 changes: 1 addition & 1 deletion py/bin/fmt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if [[ $? -ne 0 ]]; then
fi

# Format all Python code.
uvx ruff format "${TOP_DIR}/py"
uv run --directory "${TOP_DIR}/py" ruff format .
if [[ $? -ne 0 ]]; then
exit 1
fi
Expand Down
82 changes: 38 additions & 44 deletions py/bin/format_toml_files
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,48 @@

set -euo pipefail

GIT_ROOT=$(git rev-parse --show-toplevel)
TOP_DIR=$(git rev-parse --show-toplevel)

if command -v rust-parallel >/dev/null 2>&1; then
if command -v taplo >/dev/null 2>&1; then
if [ ! -f "${TOP_DIR}/py/taplo.toml" ]; then
echo "error: config file not found at ${TOP_DIR}/py/taplo.toml"
exit 1
fi

FORMATTER_COMMAND="taplo format --config ${TOP_DIR}/py/taplo.toml"
if command -v rust-parallel >/dev/null 2>&1; then
FORMATTER_COMMAND="rust-parallel -j4 ${FORMATTER_COMMAND}"
else
echo "warning: it is recommended to install https://crates.io/crates/rust-parallel for faster formatting"
fi

pushd "${TOP_DIR}"
if command -v fd >/dev/null 2>&1; then
echo "Using fd"
fd -e toml \
--exclude 'py/**/*.egg-info/**' \
--exclude 'py/**/.dist/**' \
--exclude 'py/**/.next/**' \
--exclude 'py/**/.output/**' \
--exclude 'py/**/.pytest_cache/**' \
--exclude 'py/**/.venv/**' \
--exclude 'py/**/__pycache__/**' \
--exclude 'py/**/build/**' \
--exclude 'py/**/develop-eggs/**' \
--exclude 'py/**/dist/**' \
--exclude 'py/**/eggs/**' \
--exclude 'py/**/node_modules/**' \
--exclude 'py/**/sdist/**' \
--exclude 'py/**/site/**' \
--exclude 'py/**/target/**' \
--exclude 'py/**/venv/**' \
--exclude 'py/**/wheels/**' |
rust-parallel -j4 \
taplo format --config "${GIT_ROOT}/py/taplo.toml"
--exclude '**/*.egg-info/**' \
--exclude '**/.dist/**' \
--exclude '**/.next/**' \
--exclude '**/.output/**' \
--exclude '**/.pytest_cache/**' \
--exclude '**/.venv/**' \
--exclude '**/__pycache__/**' \
--exclude '**/bazel-*/**' \
--exclude '**/build/**' \
--exclude '**/develop-eggs/**' \
--exclude '**/dist/**' \
--exclude '**/eggs/**' \
--exclude '**/node_modules/**' \
--exclude '**/sdist/**' \
--exclude '**/site/**' \
--exclude '**/target/**' \
--exclude '**/venv/**' \
--exclude '**/wheels/**' |
${FORMATTER_COMMAND}
else
echo "Using find"
find "${GIT_ROOT}" -name "*.toml" \
! -path 'py/**/*.egg-info/**' \
! -path 'py/**/.dist/**' \
! -path 'py/**/.next/**' \
! -path 'py/**/.output/**' \
! -path 'py/**/.pytest_cache/**' \
! -path 'py/**/.venv/**' \
! -path 'py/**/__pycache__/**' \
! -path 'py/**/build/**' \
! -path 'py/**/develop-eggs/**' \
! -path 'py/**/dist/**' \
! -path 'py/**/eggs/**' \
! -path 'py/**/node_modules/**' \
! -path 'py/**/sdist/**' \
! -path 'py/**/site/**' \
! -path 'py/**/target/**' \
! -path 'py/**/venv/**' \
! -path 'py/**/wheels/**' \
-print0 |
rust-parallel -j4 \
taplo format --config "${GIT_ROOT}/py/taplo.toml"
echo "Please install https://github.com/sharkdp/fd to find files to format."
fi
popd
else
echo "Please install GNU parallel to use this script"
echo "Please install https://github.com/tamasfe/taplo to format TOML files."
fi
20 changes: 10 additions & 10 deletions py/bin/generate_schema_types
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
set -euo pipefail

TOP_DIR=$(git rev-parse --show-toplevel)
SCHEMA_FILE="$TOP_DIR/py/packages/genkit/src/genkit/core/schemas.py"
SCHEMA_FILE="${TOP_DIR}/py/packages/genkit/src/genkit/core/schemas.py"

# Generate types using configuration from pyproject.toml
uv run --directory "$TOP_DIR/py" datamodel-codegen
uv run --directory "${TOP_DIR}/py" datamodel-codegen

# This isn't causing runtime errors at the moment so letting it be.
#sed -i '' '/^class Model(RootModel\[Any\]):$/,/^ root: Any$/d' "$SCHEMA_FILE"
#sed -i '' '/^class Model(RootModel\[Any\]):$/,/^ root: Any$/d' "${SCHEMA_FILE}"

# Sanitize the generated schema.
python3 "${TOP_DIR}/py/bin/sanitize_schemas.py" "$SCHEMA_FILE"
python3 "${TOP_DIR}/py/bin/sanitize_schemas.py" "${SCHEMA_FILE}"

# Add a generated by `generate_schema_types` comment.
sed -i '' '1i\
# DO NOT EDIT: Generated by `generate_schema_types` from `genkit-schemas.json`.
' "$SCHEMA_FILE"
' "${SCHEMA_FILE}"

# Add license header.
addlicense \
-c "Google LLC" \
-s=only \
"$SCHEMA_FILE"
"${SCHEMA_FILE}"

# Checks and formatting.
uv run --directory "$TOP_DIR/py" \
ruff check --fix "$SCHEMA_FILE"
uv run --directory "$TOP_DIR/py" \
ruff format "$SCHEMA_FILE"
uv run --directory "${TOP_DIR}/py" \
ruff format "${TOP_DIR}"
uv run --directory "${TOP_DIR}/py" \
ruff check --fix "${SCHEMA_FILE}"
5 changes: 3 additions & 2 deletions py/bin/sanitize_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def is_rootmodel_class(self, node: ast.ClassDef) -> bool:
return True
return False

def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
"""Visit class definitions and remove model_config if class inherits from RootModel."""
def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: # noqa: N802
"""Visit class definitions and remove model_config if class
inherits from RootModel."""
if self.is_rootmodel_class(node):
# Filter out model_config assignments
new_body = []
Expand Down
9 changes: 4 additions & 5 deletions py/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ function genkit::install_google_cloud_sdk() {
# This depends on Python 3.11 and installs it for the user on some systems.
if command -v gcloud &>/dev/null; then
gcloud config set disable_usage_reporting true
gcloud components update
return 0
yes | gcloud components update
else
curl https://sdk.cloud.google.com | bash -s -- --disable-prompts
gcloud config set disable_usage_reporting true
fi

curl https://sdk.cloud.google.com | bash -s -- --disable-prompts
gcloud config set disable_usage_reporting true
}

# Install all the required tools that have been written in Go.
Expand Down
11 changes: 11 additions & 0 deletions py/captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@
{
"run": "go test go/..."
},
{
"run": "govulncheck -C go ./...",
"conditions": [
{
"run": "CaptainHook::FileChanged.Any",
"options": {
"files": ["go/**/*.go", "go/**/go.mod", "go/**/go.sum"]
}
}
]
},
{
"run": "py/.hooks/commit-message-format-pre-push"
}
Expand Down
5 changes: 3 additions & 2 deletions py/packages/genkit/src/genkit/core/action.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Copyright 2025 Google LLC
# SPDX-License-Identifier: Apache-2.

import inspect
import json
from collections.abc import Callable
from typing import Any

from genkit.core.tracing import tracer
from pydantic import BaseModel, ConfigDict, TypeAdapter
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter


class ActionResponse(BaseModel):
model_config = ConfigDict(extra='forbid')

response: Any
trace_id: str
trace_id: str = Field(alias='traceId')


class Action:
Expand Down
Loading

0 comments on commit 03bd1b9

Please sign in to comment.