Skip to content

Commit ab95ace

Browse files
committed
Use .get() for discriminator access in generated union encoders
The discriminator field may be NotRequired in the TypedDict, so direct key access (x["key"]) triggers pyright's reportTypedDictNotRequiredAccess. Using .get() is safe because a missing key returns None, which won't match any discriminator value and falls through to the next branch.
1 parent 35ea72f commit ab95ace

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)