Commit c7566b6
authored
codegen: emit guards for every anyOf variant to fix mypy union-attr on array-containing unions (#182)
Why
===
The encoder generator for non-discriminated anyOf unions emits a chain
of ternary expressions, with the last variant historically rendered as
the unguarded `else` branch. That works for simple unions like `object |
str | list` (mypy can negative-narrow `x` to `list` in the final
branch), but it breaks for deeper unions where the array variant is
last, e.g.
```
str | float | bool | list[scalar] | None
```
When mypy fails to fully narrow `x` to `list[...]` through the prior
`isinstance` checks (`isinstance(x, (int, float))` plus `bool`
subclassing `int` make this tricky), it complains that scalar items of
the union have no `__iter__` attribute:
```
error: Item "float" of "str | float | bool | None | list[...]"
has no attribute "__iter__" (not iterable) [union-attr]
error: Item "bool" of ... has no attribute "__iter__" [union-attr]
error: Item "object" of ... has no attribute "__iter__" [union-attr]
```
This is the exact failure that has been blocking ai-infra's
`codegen-latest-pid2-schema.yml` auto-update workflow since 2026-05-04,
when repl-it-web#78355 widened
`agentToolPostgreSQL.executeSqlCommand.params` from a flat scalar union
to `array<scalar | array<scalar>>`. Every run since has failed on the
regenerated `executeSqlCommand.py` at the `for y in x` iteration inside
`encode_ExecutesqlcommandInputParams`. The committed pid2 client in
ai-infra has been kept current by hand (see replit/ai-infra#12813), but
the bot has been red for ~2.5 weeks.
What changed
============
`src/replit_river/codegen/client.py`: in the non-discriminated-anyOf
branch of `encode_type`, emit an explicit `isinstance` / `is None` guard
for every entry in `encoder_parts` — including the last one — and append
a `cast(Any, x)` fallback. mypy no longer has to negative-narrow into
the iterating branch, so deep unions with an array variant lint cleanly.
`Any` and `cast` are already part of `FILE_HEADER` so no import
bookkeeping changes.
Concretely, for the failing executeSqlCommand schema, the encoder now
ends with:
```python
return (
x if isinstance(x, str)
else x if isinstance(x, (int, float))
else x if isinstance(x, bool)
else None if x is None
else [encode_..._AnyOf_4(y) for y in x]
if isinstance(x, list)
else cast(Any, x)
)
```
Test plan
=========
- Existing `tests/v1/codegen/snapshot/test_anyof_mixed.py` snapshot
updated to show the new `if isinstance(x, list) else cast(Any, x)` tail
on its `obj | str | list[str]` encoder (the change is additive — the
runtime behavior is unchanged).
- New snapshot test
`tests/v1/codegen/snapshot/test_anyof_array_in_union.py` added with a
schema that mirrors `executeSqlCommand.params` (`array<scalar |
array<scalar>>`) and locks in the fixed output. This is the regression
test for ai-infra's CI failure.
- `uv run pytest` is green (67 passed, including all v1 and v2 codegen
tests).
- `make lint` is clean apart from a pre-existing `pyright` `grpc` import
error in `tests/v1/test_communication.py` that also fails on `main`
(unrelated).
- End-to-end verification against ai-infra: pointed ai-infra's
`./pkgs/pid2_client/scripts/generate.sh` at this branch via
`RIVER_CODEGEN_PATH=/tmp/opencode/river-python` and reran the full lint
pipeline that the auto-update workflow runs in CI; `[mypy] completed in
15.19s` and the script exited `OK.` instead of the historical
`union-attr` failure.
Once this is released (e.g. `v0.17.20`) ai-infra can bump `replit-river`
in `pkgs/pid2_client/pyproject.toml` and the auto-update workflow will
start producing green PRs again.
~ written by Zerg 👾
([ascendant-goliath-6d2f](https://zerg.zergrush.dev/chat?id=ascendant-goliath-6d2f))1 parent 008e539 commit c7566b6
7 files changed
Lines changed: 246 additions & 8 deletions
File tree
- src/replit_river/codegen
- tests/v1/codegen
- snapshot
- snapshots
- test_anyof_array_in_union
- test_service
- test_anyof_mixed_types/test_service
- types
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
457 | 457 | | |
458 | 458 | | |
459 | 459 | | |
460 | | - | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
461 | 475 | | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
466 | 481 | | |
467 | | - | |
468 | | - | |
469 | | - | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
470 | 489 | | |
471 | 490 | | |
472 | 491 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 42 additions & 0 deletions
| 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 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
Lines changed: 82 additions & 0 deletions
| 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 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| |||
| 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 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 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 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
0 commit comments