jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML

Home Page:https://jcristharif.com/msgspec/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Datetime without timezone are decoded as str with msgspec.msgpack. in 0.18.6

sihrc opened this issue · comments

Description

import datetime

import msgspec
import pytz

print(type(msgspec.msgpack.decode(msgspec.msgpack.encode(datetime.datetime.now()))))
# <class 'str'>

print(
    type(
        msgspec.msgpack.decode(
            msgspec.msgpack.encode(datetime.datetime.now(tz=pytz.UTC))
        )
    )
)
# <class 'datetime.datetime'>

python==3.11.0rc1
msgspec==0.18.6

Maybe related to #534 ?

Datetimes without timezones encode as strings, since the msgpack timestamp extension only represents datetimes with timezones.

When decoding, the decoder has no way to know that the value should be interpreted as a datetime rather than a string unless you provide it a type annotation representing the expected schema.

In [1]: import datetime

In [2]: import msgspec

In [3]: dt = datetime.datetime.now()

In [4]: encoded = msgspec.msgpack.encode(dt)

In [5]: msgspec.msgpack.decode(encoded)  # no type annotation
Out[5]: '2024-03-14T13:28:46.058671'

In [6]: msgspec.msgpack.decode(encoded, type=datetime.datetime)  # with type annotation
Out[6]: datetime.datetime(2024, 3, 14, 13, 28, 46, 58671)

See the docs for more information.