From 147e917f556a9487dc2ec4ea4bc8dfcdbe7b6c63 Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Wed, 25 Oct 2023 14:02:31 -0400 Subject: [PATCH] Add test to compute on TypedDict model --- tests/serializers/test_typed_dict.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/serializers/test_typed_dict.py b/tests/serializers/test_typed_dict.py index c89cdc240..a13fac751 100644 --- a/tests/serializers/test_typed_dict.py +++ b/tests/serializers/test_typed_dict.py @@ -405,3 +405,23 @@ def ser_columns(v: dict, serializer: core_schema.SerializerFunctionWrapHandler, value = {'one': 1, 'two': 2, 'three': 3} assert s.to_python(value) == {'one': 1, 'two': 2, 'three': 3, 'columns': ['ONE', 'TWO', 'THREE']} assert s.to_json(value) == b'{"one":1,"two":2,"three":3,"columns":["ONE","TWO","THREE"]}' + + +def test_computed_fields_with_typed_dict_model(): + class Model(TypedDict): + x: int + + def ser_y(v: Any) -> str: + return f'{v["x"]}.00' + + s = SchemaSerializer( + core_schema.typed_dict_schema( + {'x': core_schema.typed_dict_field(core_schema.int_schema())}, + computed_fields=[ + core_schema.computed_field( + 'y', core_schema.str_schema(serialization=core_schema.plain_serializer_function_ser_schema(ser_y)) + ) + ], + ) + ) + assert s.to_python(Model(x=1000)) == {'x': 1000, 'y': '1000.00'}