-
Notifications
You must be signed in to change notification settings - Fork 0
feat: auto-generate Pydantic models from JSON schema & add CI + linting #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23195a9
feat: automatic pydantic model generation from json schema
cka-y be7f731
added lint + improve ci
cka-y 2c0bfae
fix: zip without strict
cka-y 6d5af75
update models
cka-y f96ef39
fix: moved conf file
cka-y 176b896
fix: moved conf file
cka-y 4c25ea7
fix: added extra allowed
cka-y 775320a
fix: base row raw value
cka-y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| detect-changes: | ||
| name: Detect changed paths | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: read | ||
| outputs: | ||
| src: ${{ steps.filter.outputs.src }} | ||
| models: ${{ steps.filter.outputs.models }} | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| - uses: dorny/paths-filter@v4 | ||
| id: filter | ||
| with: | ||
| filters: | | ||
| src: | ||
| - 'src/**' | ||
| - 'tests/**' | ||
| - 'pyproject.toml' | ||
| models: | ||
| - 'src/gtfs_diff/schema.conf' | ||
| - 'scripts/generate_models.sh' | ||
| - 'src/gtfs_diff/models.py' | ||
| - '.github/workflows/ci.yml' | ||
|
|
||
| test: | ||
| needs: | ||
| - detect-changes | ||
| - lint | ||
| name: Run tests on Python ${{ matrix.python-version }} | ||
| if: needs.detect-changes.outputs.src == 'true' | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| python-version: ["3.10", "3.12", "3.14"] | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| - uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| - run: pip install -e '.[dev]' | ||
| - run: pytest --tb=short | ||
|
|
||
| lint: | ||
| needs: detect-changes | ||
| name: Lint and format check | ||
| if: needs.detect-changes.outputs.src == 'true' | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| - uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: "3.12" | ||
| - run: pip install -e '.[dev]' | ||
| - run: ./scripts/lint.sh | ||
|
|
||
| models-freshness: | ||
| needs: detect-changes | ||
| name: Check models.py is up to date | ||
| if: needs.detect-changes.outputs.models == 'true' | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
| - uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: "3.12" | ||
| - run: pip install 'datamodel-code-generator[ruff]>=0.59' | ||
| - run: ./scripts/generate_models.sh | ||
| - name: Check models.py is up to date | ||
| run: | | ||
| if ! git diff --exit-code src/gtfs_diff/models.py; then | ||
| echo "::error::src/gtfs_diff/models.py is out of date with the schema. Run ./scripts/generate_models.sh to regenerate." | ||
| exit 1 | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/usr/bin/env bash | ||
| # Generate Pydantic v2 models from the GTFS Diff JSON Schema. | ||
| # | ||
| # Usage: | ||
| # ./scripts/generate_models.sh # use version from src/gtfs_diff/schema.conf | ||
| # ./scripts/generate_models.sh v2-rc1 # fetch a specific version from GitHub | ||
| # ./scripts/generate_models.sh /path/to/local.json # use a local file | ||
| set -euo pipefail | ||
|
|
||
| REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" | ||
| OUTPUT="$REPO_ROOT/src/gtfs_diff/models.py" | ||
| SCHEMA_REPO="MobilityData/gtfs-diff" | ||
| SCHEMA_BRANCH="main" | ||
|
|
||
| # --- Resolve input: argument > schema.conf ----------------------------------- | ||
| SCHEMA_CONF="$REPO_ROOT/src/gtfs_diff/schema.conf" | ||
| if [ $# -ge 1 ]; then | ||
| INPUT="$1" | ||
| elif [ -f "$SCHEMA_CONF" ]; then | ||
| # shellcheck source=../src/gtfs_diff/schema.conf | ||
| source "$SCHEMA_CONF" | ||
|
cka-y marked this conversation as resolved.
|
||
| INPUT="${SCHEMA_VERSION:?SCHEMA_VERSION not set in schema.conf}" | ||
| echo "Using version from schema.conf: $INPUT" | ||
| else | ||
| echo "Usage: $0 [<version | /path/to/schema.json>]" >&2 | ||
| echo " e.g. $0 v2-rc1" >&2 | ||
| echo " or set SCHEMA_VERSION in src/gtfs_diff/schema.conf" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # --- Resolve schema source --------------------------------------------------- | ||
| if [ -f "$INPUT" ]; then | ||
| SCHEMA_FILE="$INPUT" | ||
| echo "Using local schema: $SCHEMA_FILE" | ||
| else | ||
| VERSION="$INPUT" | ||
| SCHEMA_URL="https://raw.githubusercontent.com/$SCHEMA_REPO/$SCHEMA_BRANCH/spec/v2/json_schema/${VERSION}.json" | ||
| SCHEMA_FILE="$(mktemp "${TMPDIR:-/tmp}/gtfs_diff_schema_XXXXXX.json")" | ||
| trap 'rm -f "$SCHEMA_FILE"' EXIT | ||
|
|
||
| echo "Fetching schema from $SCHEMA_URL ..." | ||
| curl -fsSL "$SCHEMA_URL" -o "$SCHEMA_FILE" | ||
| fi | ||
|
|
||
| # --- Ensure datamodel-code-generator is available ---------------------------- | ||
| if ! command -v datamodel-codegen &>/dev/null; then | ||
| echo "Installing datamodel-code-generator ..." | ||
| pip install 'datamodel-code-generator[ruff]>=0.59' | ||
| fi | ||
|
|
||
| # --- Generate ---------------------------------------------------------------- | ||
| echo "Generating models → $OUTPUT" | ||
| datamodel-codegen \ | ||
| --formatters ruff-format ruff-check \ | ||
| --input "$SCHEMA_FILE" \ | ||
| --input-file-type jsonschema \ | ||
| --output-model-type pydantic_v2.BaseModel \ | ||
| --target-python-version 3.10 \ | ||
| --use-standard-collections \ | ||
| --use-union-operator \ | ||
| --snake-case-field \ | ||
| --collapse-root-models \ | ||
| --enum-field-as-literal all \ | ||
| --field-constraints \ | ||
| --use-schema-description \ | ||
| --extra-fields allow \ | ||
| --class-name GtfsDiff \ | ||
| --output "$OUTPUT" | ||
|
cka-y marked this conversation as resolved.
|
||
|
|
||
| # --- Post-process: replace header, append __all__ ---------------------------- | ||
| # Replace the codegen header with a clear auto-generated notice. | ||
| { | ||
| echo "# AUTO-GENERATED — DO NOT EDIT" | ||
| echo "# This file is generated from the GTFS Diff JSON Schema." | ||
| echo "# To regenerate: ./scripts/generate_models.sh" | ||
| echo "# Schema source: https://github.com/$SCHEMA_REPO" | ||
| echo "" | ||
| # Strip the original codegen comment block (everything before the first blank line). | ||
| sed -n '/^$/,$p' "$OUTPUT" | ||
| } > "$OUTPUT.tmp" && mv "$OUTPUT.tmp" "$OUTPUT" | ||
|
|
||
| # Collect class names and append __all__. | ||
| CLASSES=$(grep -oE '^class ([A-Za-z_][A-Za-z0-9_]*)' "$OUTPUT" | awk '{print $2}') | ||
| { | ||
| echo "" | ||
| echo "" | ||
| echo "__all__ = [" | ||
| for cls in $CLASSES; do | ||
| echo " \"$cls\"," | ||
| done | ||
| echo "]" | ||
| } >> "$OUTPUT" | ||
|
|
||
| COUNT=$(echo "$CLASSES" | wc -w | tr -d ' ') | ||
| echo "Done — $COUNT model(s) generated." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env bash | ||
| # Auto-fix lint violations and reformat code. | ||
| # Usage: ./scripts/lint-fix.sh | ||
| set -euo pipefail | ||
|
|
||
| echo "Fixing lint violations ..." | ||
| ruff check --fix src/ tests/ | ||
|
|
||
| echo "Formatting ..." | ||
| ruff format src/ tests/ | ||
|
|
||
| echo "Done." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env bash | ||
| # Check lint and formatting (exits non-zero on violations). | ||
| # Usage: ./scripts/lint.sh | ||
| set -euo pipefail | ||
|
|
||
| echo "Checking lint rules ..." | ||
| ruff check src/ tests/ | ||
|
|
||
| echo "Checking formatting ..." | ||
| ruff format --check src/ tests/ | ||
|
|
||
| echo "All clean." |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All changes are only lint related |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is used to automatically generate the Pydantic models