Skip to content

Commit 988c540

Browse files
[bug] Missed the "init" encoders (#101)
Why === - TypedDict attempts are failing because we're passing TypedDicts into pydantic. Fix this. What changed ============ - Follow the input encoder pattern for now. Before generalizing I want to have `NewType`s to keep bindings straight Test plan ========= Manually linking the project and running codegen makes tests pass
1 parent a8418ec commit 988c540

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

replit_river/codegen/client.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,33 @@ def __init__(self, client: river.Client[{handshake_type}]):
548548
)
549549
""".rstrip()
550550

551+
# Init renderer
552+
if typed_dict_inputs and init_type:
553+
if is_literal(procedure.input):
554+
render_init_method = "lambda x: x"
555+
elif isinstance(
556+
procedure.input, RiverConcreteType
557+
) and procedure.input.type in ["array"]:
558+
assert init_type.startswith(
559+
"List["
560+
) # in case we change to list[...]
561+
_init_type_name = init_type[len("List[") : -len("]")]
562+
render_init_method = (
563+
f"lambda xs: [encode_{_init_type_name}(x) for x in xs]"
564+
)
565+
else:
566+
render_init_method = f"encode_{init_type}"
567+
else:
568+
render_init_method = f"""\
569+
lambda x: TypeAdapter({input_type})
570+
.validate_python
571+
""".rstrip()
572+
if isinstance(
573+
procedure.init, RiverConcreteType
574+
) and procedure.init.type not in ["object", "array"]:
575+
render_init_method = "lambda x: x"
576+
577+
# Input renderer
551578
if typed_dict_inputs:
552579
if is_literal(procedure.input):
553580
render_input_method = "lambda x: x"
@@ -648,7 +675,7 @@ async def {name}(
648675
'{name}',
649676
init,
650677
inputStream,
651-
TypeAdapter({init_type}).validate_python,
678+
{render_init_method},
652679
{render_input_method},
653680
{parse_output_method},
654681
{parse_error_method},
@@ -698,7 +725,7 @@ async def {name}(
698725
'{name}',
699726
init,
700727
inputStream,
701-
TypeAdapter({init_type}).validate_python,
728+
{render_init_method},
702729
{render_input_method},
703730
{parse_output_method},
704731
{parse_error_method},

0 commit comments

Comments
 (0)