Accept `Annotated`, `Literal` and union `output_type`s as PEP 747 `TypeForm`s, and bump pyright to 1.1.414

Merged
DO
2mo ago
+111-47

Why

Pyright rejected output_type=Annotated[float, Field(ge=0, le=1)], inline or through an alias, even though it works at run time and TypeSafeModel requires it for a score. Pyright gives the value Annotated[...] a special form type that no annotation short of object accepts. The only precise fix is PEP 747 TypeForm. Pyright checks it without an experimental flag since 1.1.412 (microsoft/pyright#11412), so this PR:

  • bumps the repo's pinned pyright from 1.1.411 to 1.1.414, the latest release (lint group floor + uv.lock, via uv lock --upgrade-package pyright);
  • adds TypeForm[T_co] to OutputTypeOrFunction, so every output_type position infers the type it spells;
  • also accepts a TypeForm for Agent(deps_type=...), so deps_type=Literal['human', 'machine'] (as in docs/tools-advanced.md) infers AgentDepsT precisely. from_spec/from_file keep type[T], because they pass deps_type on to template compilation.

The duplicated Agent.__init__ overload and the private _NoneOutput protocol stay. On 1.1.412+ they are redundant, but removing them made Foo | Bar, Literal[...] and [Foo, None] fail on pyright 1.1.411, so they remain for older versions.

New public surface

None. OutputTypeOrFunction (and so OutputSpec) and Agent.__init__'s deps_type now also accept TypeForms.

User-visible behavior (pyright 1.1.412+)

output_type=beforeafter
Confidence / Annotated[float, Field(ge=0, le=1)]errorfloat
[Annotated[Foo, ...], Bar], ToolOutput(Confidence), NativeOutput([Confidence, Foo])errorFoo | Bar, float, float | Foo
Annotated[Foo, ...] | Bar, Confidence | NoneerrorFoo | Bar, float | None
run_sync(..., output_type=Confidence)errorfloat
Foo | BarAgent[.., Foo] | Agent[.., Bar] (1.1.414 without this PR)Foo | Bar
Literal['a', 'b']LiteralLiteral['a', 'b']
5, Foo(a=1), [Foo, 0]errorerror

Pyright 1.1.412+ also accepts two type forms that fail at run time: output_type=None on its own and a string forward reference such as 'Foo'. Neither can be excluded from TypeForm.

Verification

  • assert_type pins in tests/typed_agent.py and tests/typed_deps.py, plus pins in tests/typed_output.py (which reports unnecessary ignores) that 5, a model instance and [Foo, 0] are still refused. mypy (make typecheck-mypy) still passes.
  • Pyright 1.1.414 fallout: dropped the ignores it no longer needs (reportUnusedClass, union ToolOutput, output_type=None, the Literal list in test_typesafe.py), added one for a partial.__qualname__ typeshed change in tavily.py, and made _utils.is_model_like return a real bool.
  • docs/output.md: the "Type checking considerations" note now says the union/Annotated/Literal workaround is only needed with mypy or Pyright before 1.1.412.

What changes for existing users

Nothing at run time. Pyright users on 1.1.412+ get precise output and deps types for type forms; older pyright behaves as before.

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