Skip to content

docs(website): update the Python guide for SDK 0.9 and v0.5 - #1348

Merged
karinamzalez merged 5 commits into
mainfrom
karina/1310-update-python-guide
Oct 27, 2026
Merged

karinamzalez merged 5 commits into
mainfrom
karina/1310-update-python-guide

Conversation

@karinamzalez

@karinamzalez karinamzalez commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Updates the "Building a Python API" guide for common-grants-sdk 0.9.0 and protocol v0.5, following the same structure as the TypeScript guide in #1345. Out of scope: moving off cg init to the template repository, which #1184 and #1340 cover.

Changes proposed

  • website/src/content/docs/guides/using-python.mdx:
    • Setup: prerequisites as a note, npx @common-grants/cli@0.4.1 init, and an accurate project tree.
    • Upgrade the SDK (new): poetry add common-grants-sdk@0.9.0, a table of SDK modules, and a note that award models and client.awards follow the opportunity patterns. It also explains the 0.9 change from camelCase to snake_case constructor arguments, which make check-types flags in the template.
    • Validate your spec (new): generate openapi.yaml and run cg check spec --protocol-version 0.5.0.
    • Implementing services: add response_model_exclude_unset=True, so unset optional fields are left out instead of sent as null.
    • Adding custom fields: replace the hand-written CustomField classes with a CustomFieldSet plugin built with define_plugin, matching the TypeScript guide.
  • website/.cspell.json: add bunx, conftest, and coveragerc.

Context for reviewers

Check most closely that every command works in a fresh project. The cg check step needs CLI 0.5.0 from #1346, which isn't published yet. Until it is, use that PR's build for step 6.

  1. Scaffold a project, accepting the defaults:

    mkdir common-grants-api && cd common-grants-api
    npx @common-grants/cli@0.4.1 init --template fast-api
    make install
  2. Upgrade the SDK and confirm the type errors the guide describes. Expect 37 errors:

    poetry add common-grants-sdk@0.9.0
    make check-types
  3. Rename the camelCase keyword arguments in services/opportunity.py, services/utils.py, and tests/conftest.py. Expect 0 errors and 9 passing tests:

    make check-types
    make test
  4. Add response_model_exclude_unset=True to the three route decorators in routes/opportunities.py. Then confirm the list response no longer contains null fields.

  5. Copy the plugin snippet to src/common_grants/schemas/plugin.py and the usage snippet into any module. Run it, then run poetry run ruff check and poetry run pyright on both. Assigning the result to a list[OpportunityBase] should fail pyright, and Paginated[Opportunity] should pass, as the guide now says.

  6. Generate and check the spec. Expect Spec is compliant with base spec, with 3 warnings:

    make gen-openapi
    npx @common-grants/cli@0.5.0 check spec openapi.yaml --protocol-version 0.5.0
  7. Build the site:

    pnpm --filter website run build

Note for reviewers: make checks fails in any scaffolded project, because template.json never shipped check_domain.sh. This guide uses make check-types instead. #1347 fixes the template.

Additional information

Output captured from a fresh scaffold with Python 3.11 and Poetry 2.5:

# After poetry add common-grants-sdk@0.9.0
$ make check-types
37 errors, 0 warnings, 0 informations

# After renaming the keyword arguments
$ make check-types
0 errors, 0 warnings, 0 informations
$ make test
9 passed, 1 warning

# Plugin snippet
$ poetry run python -m common_grants.use_plugin
12345
$ poetry run pyright src/common_grants/schemas/plugin.py src/common_grants/use_plugin.py
0 errors, 0 warnings, 0 informations
$ poetry run ruff check src/common_grants/schemas/plugin.py
All checks passed!

# Spec check (CLI build from #1346)
$ cg check spec openapi.yaml --protocol-version 0.5.0
0 errors, 3 warnings
Routes missing
  GET /common-grants/awards (warning)
  POST /common-grants/awards/search (warning)
  GET /common-grants/awards/{awdId} (warning)
Spec is compliant with base spec, with 3 warnings

pnpm --filter website run build passes, and prettier and cspell pass on the changed files.

- Upgrade the scaffolded project to common-grants-sdk 0.9.0 and explain
  the snake_case constructor rename that make check-types flags.
- Validate the generated spec against protocol v0.5 with cg check.
- Omit unset optional fields from responses with
  response_model_exclude_unset.
- Replace the hand-rolled custom field classes with a CustomFieldSet
  plugin, matching the TypeScript guide.
- Add bunx, conftest, and coveragerc to the website cspell words.

Refs #1310
@github-actions github-actions Bot added website Issues related to the website typescript Issue or PR related to TypeScript tooling labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Website Preview Deployed!

Preview your changes at: https://pr-1348-common-grants.agile6.workers.dev

Built from commit 45cc82d

- Say that pyright treats the custom-fields type parameter as invariant
  and show the typed Paginated and Filtered responses to use instead.
- Add a module docstring to the plugin snippet so the template's ruff
  config passes.
- Use python3 --version, add the poetry env use tip, list
  package-lock.json among the scaffold leftovers, and add conftest.py
  to the project tree.
- Scope make check-spec to cg 0.5.0 or later and add the Ctrl+C note.
@SnowboardTechie
SnowboardTechie self-requested a review October 26, 2026 15:16

@SnowboardTechie SnowboardTechie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked through this, really nice work. I ran the guide end to end against SDK 0.9.0, the fast-api template and #1346's CLI, and every step held.

@karinamzalez
karinamzalez merged commit 0a2c92c into main Oct 27, 2026
5 checks passed
@karinamzalez
karinamzalez deleted the karina/1310-update-python-guide branch October 27, 2026 16:32
SnowboardTechie added a commit that referenced this pull request Oct 28, 2026
…itory (#1340)

### Summary

- Part of #1184
- Builds on #1348, now merged; rebased onto `main`.
- Depends on common-grants/cg-api-template-fastapi#3 (SDK 0.9 in the
template) and on the CLI 0.5.0 release (#1346) for the spec-check step.
- Time to review: 10 minutes

The Python guide now starts from the [FastAPI template
repository](https://github.com/common-grants/cg-api-template-fastapi)
instead of `cg init`, which ADR 0029 retires. A reader goes from "Use
this template" to a working first request.

### Changes proposed

On top of #1348:

- Getting started: "Use this template" and `git clone` replace `mkdir`
and `cg init`. Adds the template's `make ci` check and three `curl`
requests. The project tree matches the template.
- Corrections for the template: `schemas/opportunity.py`, the
`OpportunityRepository` interface and `create_app()` replace file and
class names the template doesn't have. The custom-fields plugin goes in
`opportunity.py`, so the Pyright workaround is no longer needed.
- "Upgrade the SDK" becomes "Upgrading from an earlier SDK", after the
getting-started flow, for projects on SDK 0.8. It uses `^0.9.0` and
names the template's real renames: `SearchSorting(sort_by=...,
sortOrder=...)` and `PaginatedBodyParams(page_size=...)` in tests.
- The spec check runs in synchronized pnpm, npm, and bun tabs. Removes
`make check-spec`, which the template doesn't have.
- Title: "Using Python".
- Removes this page from the `cg init` checklist in
`lib/cli/DEVELOPMENT.md`.

Kept from #1348: the page structure, the SDK module table and awards
note, the snake_case explanation, spec validation, data validation, and
the plugin example.

### Context for reviewers

Each change is its own commit, so the diff can be read commit by commit.

### Additional information

Every documented command ran on a fresh clone of template PR 3
(`3a5cc86`), including the custom-fields plugin. `make ci` passed (109
tests), and the spec check passed through pnpm, npm, and bun with the
CLI packed from #1346 (3 award-route warnings). Following the upgrade
section on template `main` (`c5e11ca`, SDK 0.8.1) gives a passing `make
ci` on SDK 0.9. The website build, lint, format, and spell checks pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

typescript Issue or PR related to TypeScript tooling website Issues related to the website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Website] Update the "using Python" guide

2 participants