Closed baodrate closed 3 years ago
Yes, there is such a problem. Native fromisoformat
doesn't cover all the cases but it's the fastest way to parse most ISO 8601 datetime strings: https://github.com/closeio/ciso8601/issues/66#issuecomment-404848813. But you're right it's the mashumaro's scope to handle generic ISO-8601 strings, so I decided to add a way to choose the parser using field's metadata:
class DataClass(DataClassDictMixin):
x: datetime = field(metadata={"deserialize": "ciso8601"})
See more examples here: https://github.com/Fatal1ty/mashumaro/tree/datetime-parsers#using-field-metadata Moreover, I think it's an obvious way to make mashumaro more extensible and I'm going to use it for other things like https://github.com/Fatal1ty/mashumaro/issues/11. Also it would be convenient to configure a default behaviour for all fields in one place like Meta class in Django models.
We can change deserialization method with version 1.18: https://github.com/Fatal1ty/mashumaro#using-field-metadata
To fully cover the original problem, here is an example with dateutil
:
from datetime import datetime
from dataclasses import dataclass, field
from mashumaro import DataClassDictMixin
import dateutil.parser
@dataclass
class A(DataClassDictMixin):
x: datetime = field(metadata={"deserialize": dateutil.parser.isoparse})
print(A.from_dict({"x": "2019-01-02T07:00:00Z"}))
Perfect. I'll test it out soon, thank you!
As of Python 3.11, datetime.fromisoformat
can handle all the important bits of the ISO 8601 spec.
I'll be backporting its code in https://github.com/movermeyer/backports.datetime_fromisoformat/issues/21, so it'll be available for earlier versions of Python 3.
FWIW, ciso8601
is still faster than any other parser.
Currently the generated
from_dict
code callsdatetime.fromisoformat()
:https://github.com/Fatal1ty/mashumaro/blob/53cd94c34006aff7f8b4eaa29d7b5d7037ee4bfd/mashumaro/serializer/base/metaprogramming.py#L468-L471
fromisoformat
is only designed to invert the strings generated bydatetime.isoformat()
:According to mashumaro's documentation:
I believe it should be within mashumaro's scope to handle generic ISO-8601 strings