|
30 | 30 | ListTypeExpr, |
31 | 31 | LiteralTypeExpr, |
32 | 32 | ModuleName, |
| 33 | + NoneTypeExpr, |
33 | 34 | OpenUnionTypeExpr, |
34 | 35 | RenderedPath, |
35 | 36 | TypeExpression, |
@@ -170,7 +171,7 @@ def encode_type( |
170 | 171 | encoder_name: TypeName | None = None # defining this up here to placate mypy |
171 | 172 | chunks: List[FileContents] = [] |
172 | 173 | if isinstance(type, RiverNotType): |
173 | | - return (TypeName("None"), [], [], set()) |
| 174 | + return (NoneTypeExpr(), [], [], set()) |
174 | 175 | elif isinstance(type, RiverUnionType): |
175 | 176 | typeddict_encoder = list[str]() |
176 | 177 | encoder_names: set[TypeName] = set() |
@@ -379,17 +380,17 @@ def flatten_union(tpe: RiverType) -> list[RiverType]: |
379 | 380 | typeddict_encoder.append( |
380 | 381 | f"encode_{render_literal_type(inner_type_name)}(x)" |
381 | 382 | ) |
382 | | - case DictTypeExpr(_): |
383 | | - raise ValueError( |
384 | | - "What does it mean to try and encode a dict in" |
385 | | - " this position?" |
386 | | - ) |
387 | 383 | case LiteralTypeExpr(const): |
388 | 384 | typeddict_encoder.append(repr(const)) |
| 385 | + case TypeName(value): |
| 386 | + typeddict_encoder.append(f"encode_{value}(x)") |
| 387 | + case NoneTypeExpr(): |
| 388 | + typeddict_encoder.append("None") |
389 | 389 | case other: |
390 | | - typeddict_encoder.append( |
391 | | - f"encode_{render_literal_type(other)}(x)" |
| 390 | + _o2: DictTypeExpr | OpenUnionTypeExpr | UnionTypeExpr = ( |
| 391 | + other |
392 | 392 | ) |
| 393 | + raise ValueError(f"What does it mean to have {_o2} here?") |
393 | 394 | if permit_unknown_members: |
394 | 395 | union = OpenUnionTypeExpr(UnionTypeExpr(any_of)) |
395 | 396 | else: |
@@ -471,7 +472,7 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]: |
471 | 472 | return (TypeName("bool"), [], [], set()) |
472 | 473 | elif type.type == "null" or type.type == "undefined": |
473 | 474 | typeddict_encoder.append("None") |
474 | | - return (TypeName("None"), [], [], set()) |
| 475 | + return (NoneTypeExpr(), [], [], set()) |
475 | 476 | elif type.type == "Date": |
476 | 477 | typeddict_encoder.append("TODO: dstewart") |
477 | 478 | return (TypeName("datetime.datetime"), [], [], set()) |
@@ -511,8 +512,11 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]: |
511 | 512 | ) |
512 | 513 | case LiteralTypeExpr(const): |
513 | 514 | typeddict_encoder.append(repr(const)) |
| 515 | + case TypeName(value): |
| 516 | + typeddict_encoder.append(f"encode_{value}(x)") |
514 | 517 | case other: |
515 | | - typeddict_encoder.append(f"encode_{render_literal_type(other)}(x)") |
| 518 | + _o1: NoneTypeExpr | OpenUnionTypeExpr | UnionTypeExpr = other |
| 519 | + raise ValueError(f"What does it mean to have {_o1} here?") |
516 | 520 | return (DictTypeExpr(type_name), module_info, type_chunks, encoder_names) |
517 | 521 | assert type.type == "object", type.type |
518 | 522 |
|
@@ -823,7 +827,7 @@ def __init__(self, client: river.Client[Any]): |
823 | 827 | module_names, |
824 | 828 | permit_unknown_members=True, |
825 | 829 | ) |
826 | | - if error_type == "None": |
| 830 | + if isinstance(error_type, NoneTypeExpr): |
827 | 831 | error_type = TypeName("RiverError") |
828 | 832 | else: |
829 | 833 | serdes.append( |
@@ -916,7 +920,7 @@ def __init__(self, client: river.Client[Any]): |
916 | 920 | f"Unable to derive the input encoder from: {input_type}" |
917 | 921 | ) |
918 | 922 |
|
919 | | - if output_type == "None": |
| 923 | + if isinstance(output_type, NoneTypeExpr): |
920 | 924 | parse_output_method = "lambda x: None" |
921 | 925 |
|
922 | 926 | if procedure.type == "rpc": |
|
0 commit comments