Skip to content

Commit

Permalink
[bug] Missed the "init" encoders (#101)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
blast-hardcheese authored Oct 25, 2024
1 parent a8418ec commit 988c540
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions replit_river/codegen/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,33 @@ def __init__(self, client: river.Client[{handshake_type}]):
)
""".rstrip()

# Init renderer
if typed_dict_inputs and init_type:
if is_literal(procedure.input):
render_init_method = "lambda x: x"
elif isinstance(
procedure.input, RiverConcreteType
) and procedure.input.type in ["array"]:
assert init_type.startswith(
"List["
) # in case we change to list[...]
_init_type_name = init_type[len("List[") : -len("]")]
render_init_method = (
f"lambda xs: [encode_{_init_type_name}(x) for x in xs]"
)
else:
render_init_method = f"encode_{init_type}"
else:
render_init_method = f"""\
lambda x: TypeAdapter({input_type})
.validate_python
""".rstrip()
if isinstance(
procedure.init, RiverConcreteType
) and procedure.init.type not in ["object", "array"]:
render_init_method = "lambda x: x"

# Input renderer
if typed_dict_inputs:
if is_literal(procedure.input):
render_input_method = "lambda x: x"
Expand Down Expand Up @@ -648,7 +675,7 @@ async def {name}(
'{name}',
init,
inputStream,
TypeAdapter({init_type}).validate_python,
{render_init_method},
{render_input_method},
{parse_output_method},
{parse_error_method},
Expand Down Expand Up @@ -698,7 +725,7 @@ async def {name}(
'{name}',
init,
inputStream,
TypeAdapter({init_type}).validate_python,
{render_init_method},
{render_input_method},
{parse_output_method},
{parse_error_method},
Expand Down

0 comments on commit 988c540

Please sign in to comment.