Skip to content
This repository was archived by the owner on Jun 21, 2025. It is now read-only.

Commit 2af6167

Browse files
docs: add docs about Union types and casting (#383)
* docs: add docs about Union types and casting #379 * docs: add additional example
1 parent c9beb1a commit 2af6167

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

docs/serialization.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ Serialization
22
=============
33

44
Data in Redis
5-
-------------
5+
#############
66
pydantic-aioredis uses Redis Hashes to store data. The ```_primary_key_field``` of each Model is used as the key of the hash.
77

88
Because Redis only supports string values as the fields of a hash, data types have to be serialized.
99

1010
Simple data types
11-
-----------------
11+
#################
1212
Simple python datatypes that can be represented as a string and natively converted by pydantic are converted to strings and stored. Examples
1313
are ints, floats, strs, bools, and Nonetypes.
1414

15+
Unintentional Type Casting with Union Types
16+
*******************************************
17+
18+
When using Union types, pydantic will cast the value to the first type in the Union. This can cause unintended type casting. For example, if you have a field
19+
of type Union[float, int], and you set the value to 1, pydantic will cast the value to a float 1.0. In the other direction (i.e. `x: Union[int, float]`) will result in
20+
the value being casted as an int and rounded. If you used a value of x = 1.99, it would get cast as an int and rounded to 1.
21+
22+
This is an issue with Pydantic. More info can be found in
23+
`this issue <https://github.com/andrewthetechie/pydantic-aioredis/issues/379>`_ reported by `david-wahlstedt <https://github.com/david-wahlstedt>`_ and `this issue in Pydantic <https://github.com/pydantic/pydantic/issues/4519>`_.
24+
25+
There are some `test cases <https://github.com/andrewthetechie/pydantic-aioredis/blob/main/test/test_union_typing.py>`_ in pydantic_aioredis that illustrate this problem.
26+
1527
Complex data types
16-
------------------
28+
##################
1729
Complex data types are dumped to json with json.dumps().
1830

1931
Custom serialization is possible using `json_default <https://docs.python.org/3/library/json.html#:~:text=not%20None.-,If%20specified%2C%20default%20should%20be%20a%20function%20that%20gets%20called%20for%20objects%20that%20can%E2%80%99t%20otherwise%20be%20serialized.%20It%20should%20return%20a%20JSON%20encodable%20version%20of%20the%20object%20or%20raise%20a%20TypeError.%20If%20not%20specified%2C%20TypeError%20is%20raised.,-If%20sort_keys%20is>`_ and `json_object_hook <https://docs.python.org/3/library/json.html#:~:text=object_hook%20is%20an%20optional%20function%20that%20will%20be%20called%20with%20the%20result%20of%20any%20object%20literal%20decoded%20(a%20dict).%20The%20return%20value%20of%20object_hook%20will%20be%20used%20instead%20of%20the%20dict.%20This%20feature%20can%20be%20used%20to%20implement%20custom%20decoders%20(e.g.%20JSON%2DRPC%20class%20hinting).>`_.

0 commit comments

Comments
 (0)