This pull request adds support for more types and also utilizes the code generation capabilities for them.
The following types where added:
- uuid.UUID
- PosixPath, WindowsPath, PurePath, PurePosixPath, PureWindowsPath from pathlib
- IPv4Address, IPv6Address, IPv4Network, IPv6Network, IPv4Interface, IPv6Interface from ipaddress
- date & datetime from datetime
I needed these types because I wanted to use pyserde in combination with asyncpg.
asyncpg returns dict like objects which can be converted to dataclasses by pyserde's from_dict
function.
Since asyncpg also supports the above mentioned types, the dicts already contain instances of them.
That is the reason why I also added a new argument reuse_instances
for from_dict
& from_tuple
.
When this is set to True
these functions check if the field already contains an instances of the target type and reuse it when possible.
This is faster for Path & IPAddress then calling the constructor again.
For UUID it is also the only way to handle existing instances because uuid.UUID()
does not accept them as an argument.
It is also possible to change the default value of reuse_instances
via the serialize
& deserialize
decorators.
To not cause slowdowns when serializing or deserializing to json/msgpack/toml/yaml reuse_instances
is always set to False
there, because we will never see existing instances there.
That is the part where this pull request drifted into refactoring.
Since I had to add the reuse_instances
argument at all these places, I also used the opportunity to remove some unused arguments (named
& strict
) and renamed asdict
to to_dict
and astuple
to to_tuple
.
These are breaking changes, but I felt it was worth the gained similarity between serialization and deserialization code.
In my option the public exposed functions are now also named more uniformly.
For msgpack external types I also changed the behaviour slightly. It is not required anymore that the dataclasses have a special meaning _type
attribute.
Instead to_msgpack
& from_msgpack
search the ext_dict
for the correct type or type code and also throw exceptions if they can not find them.
I am sorry that this became so intertwined, I understand if you don't want to merge the breaking changes.
I tried to make the commits cherry-pickable so maybe you only want to use some commits.
During development, I also noticed that Unions do not work properly for these types.
I will make a separate pull request to fix that.
Finally, I have a question: What is the purpose of setting SE_NAME
and why is it used in is_serializable
?
Could it be removed? is_serializable
could use TO_ITER
and TO_DICT
for the check like is_deserializable
.