Type-check docs examples with pyright in `tests/test_examples.py`

Merged
DO
2mo ago
+606-229

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_examples lints is also type-checked with pyright (standard mode plus reportUnnecessaryTypeIgnoreComment), next to lint_ruff. Each case fails with its own errors, located at the Markdown/docstring file: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. -k runs only check what they select.
  • Opt-out: typecheck="skip - <reason>"; lint="skip" examples are skipped too.
  • It runs wherever pyright is installed (the default lint group) unless TYPECHECK_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 (and ModelSelectionContext, ...) 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 isinstance or ModelResponse.text: ~16
  • Typed constructors for settings/specs (BedrockModelSettings, ThinkingLevel, RetryConfig, ...): ~11
  • Case(evaluators=[...]): 8 (API widening, below)
  • Graph generics for the object defaults, plus 3 joins.md mistakes: ~10
  • Stale # type: ignore removed (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 (@tool without ctx, 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 where pydantic_ai_harness.exa isn't in the pinned harness, 2 where anthropic.__all__ omits the Bedrock Mantle/Vertex clients, 1 for the dirty-equals snapshot in testing.md, and 5 for Model.system (below). sqlparse, nest-asyncio and azure-identity joined the dev group instead of being opted out, and the ModelProfile example reads flags with .get()

API changes (backward compatible widenings)

  • Case(evaluators=...) takes a tuple or a list; Dataset.add_case(evaluators=...) any Sequence
  • DeferredToolRequests.build_results(approvals=...) takes a Mapping
  • MistralProvider's api_key overload gains the base_url the implementation already accepts
  • AzureRealtimeModel(credential=...) accepts azure.identity credentials under type checking (the token protocol's token is now read-only)

Open API questions

  • UploadedFile(provider_name=model.system) is the documented pattern, but Model.system is str, not UploadedFileProviderName (5 opt-outs in input.md, kept as is).
  • AGUIAdapter.from_request(request, agent=agent) binds the class's type parameters to their object defaults, so a typed-deps agent needs AGUIAdapter[Deps, str].from_request(...) (done in ag-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