Repository navigation
docs(website): start the Python guide from the FastAPI template repository - #1340
Conversation
|
🚀 Website Preview Deployed! Preview your changes at: https://pr-1340-common-grants.agile6.workers.dev Built from commit a8cf95f |
b6eef9b to
f717929
Compare
### Summary - Fixes #1310 - Time to review: 15 minutes 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: ```bash 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: ```bash 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: ```bash 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`: ```bash make gen-openapi npx @common-grants/cli@0.5.0 check spec openapi.yaml --protocol-version 0.5.0 ``` 7. Build the site: ```bash 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.
…itory Replace the cg init setup with Use this template and git clone, add the template's make ci check and curl requests, and match the project tree to the template.
…e template The template has no schemas re-export, OpportunityService, or plugin.py. Point to opportunity.py, the OpportunityRepository seam and create_app(), note that routes already omit unset fields, and drop the Pyright workaround the template's Opportunity alias makes unnecessary.
The template now depends on SDK 0.9, so the upgrade applies only to projects from an earlier template. Move it after the getting-started flow, use the ^0.9.0 range, and name the template's real camelCase arguments.
Put cg check spec in synchronized package-manager tabs and drop make check-spec, which the template does not have.
67653fd to
04b8df8
Compare
karinamzalez
left a comment
There was a problem hiding this comment.
Approved! Will you merge this after common-grants/cg-api-template-fastapi#3 lands?
Quick question: The guide now pins @common-grants/cli@0.5.0 check spec in all three tabs (lines 124, 131, 138). The "If the check spec command was updated" checklist in lib/cli/DEVELOPMENT.md still lists only the FastAPI template Makefile, so the next CLI release could miss this page. Could you add this guide to that checklist?
| @@ -1,23 +1,23 @@ | |||
| --- | |||
| title: Building a Python API | |||
There was a problem hiding this comment.
Question! the title becomes “Using Python”, but the TypeScript guide is still titled “Building a TypeScript API”. Should we match the names?
There was a problem hiding this comment.
They'll match once #1341 merges. That PR retitles the TypeScript guide to "Using TypeScript".
|
@karinamzalez Yes, I'll merge this once common-grants/cg-api-template-fastapi#3 lands. I added this guide to the |
Summary
main.The Python guide now starts from the FastAPI template repository 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:
git clonereplacemkdirandcg init. Adds the template'smake cicheck and threecurlrequests. The project tree matches the template.schemas/opportunity.py, theOpportunityRepositoryinterface andcreate_app()replace file and class names the template doesn't have. The custom-fields plugin goes inopportunity.py, so the Pyright workaround is no longer needed.^0.9.0and names the template's real renames:SearchSorting(sort_by=..., sortOrder=...)andPaginatedBodyParams(page_size=...)in tests.make check-spec, which the template doesn't have.cg initchecklist inlib/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 cipassed (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 templatemain(c5e11ca, SDK 0.8.1) gives a passingmake cion SDK 0.9. The website build, lint, format, and spell checks pass.