Commit bb53ddb
committed
fix(types): make ComputedModelOutputThunk.parsed generic over the format type
`.parsed` previously returned `pydantic.BaseModel | None`, so callers still
needed `cast(MyModel, result.parsed)` for static narrowing — the gap
@ajbozarth flagged on PR generative-computing#1282.
Thread the format type through the thunk's existing type parameter `S`:
`_format` is now `type[S] | None` and `.parsed` returns `S | None`. Reusing
`S` (rather than a second TypeVar) composes with the companion `format=`
overloads on generative-computing#1274, which bind `S` to the supplied model so
`m.act(action, format=MyModel)` yields `ComputedModelOutputThunk[MyModel]`
and `.parsed` is typed `MyModel | None`.
The `.parsed` body narrows `_format` to a pydantic type to call
`model_validate_json`, then re-asserts the result as `S` — `S` is unbounded
(it is `str` for plain instructions) so neither cast can be elided.
Add `test/typing/check_parsed.py` asserting `.parsed` tracks the type
parameter for both a model-parameterized and a `str` thunk.
Assisted-by: Claude Code1 parent 4857a32 commit bb53ddb
2 files changed
Lines changed: 44 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
402 | 403 | | |
403 | 404 | | |
404 | 405 | | |
405 | | - | |
| 406 | + | |
406 | 407 | | |
407 | 408 | | |
408 | 409 | | |
| |||
893 | 894 | | |
894 | 895 | | |
895 | 896 | | |
896 | | - | |
| 897 | + | |
897 | 898 | | |
898 | 899 | | |
899 | | - | |
900 | | - | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
901 | 905 | | |
902 | 906 | | |
903 | 907 | | |
904 | | - | |
| 908 | + | |
905 | 909 | | |
906 | 910 | | |
907 | 911 | | |
| |||
911 | 915 | | |
912 | 916 | | |
913 | 917 | | |
914 | | - | |
915 | | - | |
| 918 | + | |
| 919 | + | |
916 | 920 | | |
917 | 921 | | |
918 | 922 | | |
919 | 923 | | |
920 | 924 | | |
921 | 925 | | |
922 | 926 | | |
923 | | - | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
924 | 932 | | |
925 | 933 | | |
926 | 934 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
0 commit comments