Closed uyha closed 8 months ago
Hi @uyha
Can you please try reuse_instances=True
?
This is how objects are treated within generated code.
I changed https://github.com/yukinarit/pyserde/blob/a517254b18b2927348b94f59de095ce873abe101/serde/msgpack.py#L71 to
kwargs: Any = {"c": cls, "reuse_instances": True, "convert_sets": True}
with the following snippet, it still converts the datetime
object to a str
from serde.msgpack import to_msgpack
from datetime import datetime
from msgpack import packb
time = datetime.fromisoformat("2020-01-01 00:00:00+00")
print(to_msgpack(time, datetime=True))
print(packb(time, datetime=True))
which outputs
b'\xb92020-01-01T00:00:00+00:00'
b'\xd6\xff^\x0b\xe1\x00'
Ah,
reuse_instances
are not available in to_msgpack
reuse_instances
works only for dataclass field
@dataclass
class Foo:
v: datetime
f = Foo(datetime.fromisoformat("2020-01-01 00:00:00+00")) print(to_dict(f, reuse_instances=True)) print(to_dict(f, reuse_instances=False))
it prints
{'v': datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)} {'v': '2020-01-01T00:00:00+00:00'}
ok, I found a way to fix. let me post it tomorrow
@uyha v0.14.2 was published to PyPI. Please take a look if it resolves your issue.
yup, works
Currently, https://github.com/yukinarit/pyserde/blob/35901de31a8b135c777710d5388c8f5a0f11dc66/serde/se.py#L383-L384 converts
datetime
to an ISO string unconditionally. This makes themsgpack
serializer unable to convertdatetime
to its extension type in the spec. It would be nice to have a flag to disable this convertion and let the serializer handle it.