Skip to content

Commit c2138d8

Browse files
authored
Use .get() for discriminator access in generated union encoders (#176)
Why === Follow-up to #175. The discriminator field in a discriminated union may be `NotRequired` in the TypedDict. Direct key access (`x["shapeType"]`) triggers pyright's `reportTypedDictNotRequiredAccess` error. This broke the pid2 codegen CI when the scribe schema added discriminated union variants where the discriminator field is optional. What changed ============ Use `x.get("key")` instead of `x["key"]` for discriminator checks in the generated ternary chain. This is safe because a missing key returns `None`, which won't match any discriminator value and falls through to the next branch. Test plan ========= - All 64 tests pass - Updated snapshot for `test_unknown_enum` - Tested end-to-end against the pid2 schema from ai-infra — codegen, mypy, and pyright all pass ~ written by Zerg 👾
1 parent 35ea72f commit c2138d8

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/replit_river/codegen/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def flatten_union(tpe: RiverType) -> list[RiverType]:
342342
)
343343
typeddict_encoder.append(
344344
f"""
345-
if x[{repr(discriminator_name)}]
345+
if x.get({repr(discriminator_name)})
346346
== {repr(discriminator_value)}
347347
else
348348
""",

tests/v1/codegen/snapshot/snapshots/test_unknown_enum/enumService/needsEnumObject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def encode_NeedsenumobjectInput(
7575
encode_NeedsenumobjectInputOneOf_in_first(
7676
cast("NeedsenumobjectInputOneOf_in_first", x)
7777
)
78-
if x["kind"] == "in_first"
78+
if x.get("kind") == "in_first"
7979
else encode_NeedsenumobjectInputOneOf_in_second(
8080
cast("NeedsenumobjectInputOneOf_in_second", x)
8181
)

0 commit comments

Comments
 (0)