|
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,15 @@ 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)" |
392 | | - ) |
| 390 | + _o2: DictTypeExpr | OpenUnionTypeExpr | UnionTypeExpr = other |
| 391 | + raise ValueError(f"What does it mean to have {_o2} here?") |
393 | 392 | if permit_unknown_members: |
394 | 393 | union = OpenUnionTypeExpr(UnionTypeExpr(any_of)) |
395 | 394 | else: |
@@ -471,7 +470,7 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]: |
471 | 470 | return (TypeName("bool"), [], [], set()) |
472 | 471 | elif type.type == "null" or type.type == "undefined": |
473 | 472 | typeddict_encoder.append("None") |
474 | | - return (TypeName("None"), [], [], set()) |
| 473 | + return (NoneTypeExpr(), [], [], set()) |
475 | 474 | elif type.type == "Date": |
476 | 475 | typeddict_encoder.append("TODO: dstewart") |
477 | 476 | return (TypeName("datetime.datetime"), [], [], set()) |
@@ -511,8 +510,11 @@ def extract_props(tpe: RiverType) -> list[dict[str, RiverType]]: |
511 | 510 | ) |
512 | 511 | case LiteralTypeExpr(const): |
513 | 512 | typeddict_encoder.append(repr(const)) |
| 513 | + case TypeName(value): |
| 514 | + typeddict_encoder.append(f"encode_{value}(x)") |
514 | 515 | case other: |
515 | | - typeddict_encoder.append(f"encode_{render_literal_type(other)}(x)") |
| 516 | + _o1: NoneTypeExpr | OpenUnionTypeExpr | UnionTypeExpr = other |
| 517 | + raise ValueError(f"What does it mean to have {_o1} here?") |
516 | 518 | return (DictTypeExpr(type_name), module_info, type_chunks, encoder_names) |
517 | 519 | assert type.type == "object", type.type |
518 | 520 |
|
@@ -823,7 +825,7 @@ def __init__(self, client: river.Client[Any]): |
823 | 825 | module_names, |
824 | 826 | permit_unknown_members=True, |
825 | 827 | ) |
826 | | - if error_type == "None": |
| 828 | + if isinstance(error_type, NoneTypeExpr): |
827 | 829 | error_type = TypeName("RiverError") |
828 | 830 | else: |
829 | 831 | serdes.append( |
@@ -916,7 +918,7 @@ def __init__(self, client: river.Client[Any]): |
916 | 918 | f"Unable to derive the input encoder from: {input_type}" |
917 | 919 | ) |
918 | 920 |
|
919 | | - if output_type == "None": |
| 921 | + if isinstance(output_type, NoneTypeExpr): |
920 | 922 | parse_output_method = "lambda x: None" |
921 | 923 |
|
922 | 924 | if procedure.type == "rpc": |
|
0 commit comments