Type-check docs examples with pyright in `tests/test_examples.py`
Merged
DO
2mo ago+606+606+012345678901234567890123456789-229-229-012345678901234567890123456789
Docs code examples were run and ruff-linted by tests/test_examples.py, but never type-checked, so examples drifted from the API's types (e.g. RunContext[None] on agents whose deps default to object since v2). This adds a pyright check and fixes every example it flags.
How the check works
- Every example
test_docs_exampleslints is also type-checked with pyright (standardmode plusreportUnnecessaryTypeIgnoreComment), next tolint_ruff. Each case fails with its own errors, located at the Markdown/docstringfile:line:col. - A session fixture writes the selected examples out (with their
requires=examples as siblings) and runs pyright once over all of them (~15 s), once per Python version on the fences (py="3.11"), concurrently. Under xdist, the first worker runs it and the others read its result.-kruns only check what they select. - Opt-out:
typecheck="skip - <reason>";lint="skip"examples are skipped too. - It runs wherever pyright is installed (the default
lintgroup) unlessTYPECHECK_EXAMPLES=false. CI sets that everywhere except the docs-only job (3.13) and the 3.14 all-extras cells, so it runs once per shard there rather than across the matrix or the latest-versions canary.
Fixes, by commit
- Bare
RunContext/AbstractCapability(andModelSelectionContext, ...) instead of[None]: 12 examples, plus the same pattern in prose-adjacent snippets, README and skills - Narrowing
Optionals (last_model_request_parameters,AgentRun.result,provider_details, ...): ~35 - Narrowing unions with
isinstanceorModelResponse.text: ~16 - Typed constructors for settings/specs (
BedrockModelSettings,ThinkingLevel,RetryConfig, ...): ~11 Case(evaluators=[...]): 8 (API widening, below)- Graph generics for the
objectdefaults, plus 3joins.mdmistakes: ~10 - Stale
# type: ignoreremoved (output.md,multi-agent-applications.md,graph.md); the "Type checking considerations" note now scopes the union advice to mypy, which still needs it - Plain mistakes, several of which would fail at runtime in unrun examples (
@toolwithoutctx,gemini_safety_settings, wrong MCP elicitation handler signature,SpanTree.all(), ...): ~20 - 16 opt-outs:
type_mistakes.py, 2 fragments, 3 for packages not installed in the test environment (langchain-community), 2 wherepydantic_ai_harness.exaisn't in the pinned harness, 2 whereanthropic.__all__omits the Bedrock Mantle/Vertex clients, 1 for the dirty-equals snapshot intesting.md, and 5 forModel.system(below).sqlparse,nest-asyncioandazure-identityjoined thedevgroup instead of being opted out, and theModelProfileexample reads flags with.get()
API changes (backward compatible widenings)
Case(evaluators=...)takes a tuple or a list;Dataset.add_case(evaluators=...)anySequenceDeferredToolRequests.build_results(approvals=...)takes aMappingMistralProvider'sapi_keyoverload gains thebase_urlthe implementation already acceptsAzureRealtimeModel(credential=...)acceptsazure.identitycredentials under type checking (the token protocol'stokenis now read-only)
Open API questions
UploadedFile(provider_name=model.system)is the documented pattern, butModel.systemisstr, notUploadedFileProviderName(5 opt-outs ininput.md, kept as is).AGUIAdapter.from_request(request, agent=agent)binds the class's type parameters to theirobjectdefaults, so a typed-deps agent needsAGUIAdapter[Deps, str].from_request(...)(done inag-ui.md).
Checklist
- Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code