Accept `Annotated`, `Literal` and union `output_type`s as PEP 747 `TypeForm`s, and bump pyright to 1.1.414
- Closes #8601
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 (
lintgroup floor +uv.lock, viauv lock --upgrade-package pyright); - adds
TypeForm[T_co]toOutputTypeOrFunction, so everyoutput_typeposition infers the type it spells; - also accepts a
TypeFormforAgent(deps_type=...), sodeps_type=Literal['human', 'machine'](as indocs/tools-advanced.md) infersAgentDepsTprecisely.from_spec/from_filekeeptype[T], because they passdeps_typeon 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= | before | after |
|---|---|---|
Confidence / Annotated[float, Field(ge=0, le=1)] | error | float |
[Annotated[Foo, ...], Bar], ToolOutput(Confidence), NativeOutput([Confidence, Foo]) | error | Foo | Bar, float, float | Foo |
Annotated[Foo, ...] | Bar, Confidence | None | error | Foo | Bar, float | None |
run_sync(..., output_type=Confidence) | error | float |
Foo | Bar | Agent[.., Foo] | Agent[.., Bar] (1.1.414 without this PR) | Foo | Bar |
Literal['a', 'b'] | Literal | Literal['a', 'b'] |
5, Foo(a=1), [Foo, 0] | error | error |
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_typepins intests/typed_agent.pyandtests/typed_deps.py, plus pins intests/typed_output.py(which reports unnecessary ignores) that5, 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, unionToolOutput,output_type=None, theLiterallist intest_typesafe.py), added one for apartial.__qualname__typeshed change intavily.py, and made_utils.is_model_likereturn a realbool. docs/output.md: the "Type checking considerations" note now says the union/Annotated/Literalworkaround 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