Comments (6)
I'm closing this issue since use_enum
and use_datetime
were removed in version 3.0.
from mashumaro.
I added use_enum
, use_datetime
and use_bytes
flags because packages for json, msgpack and yaml could parse data right into enum, datetime or bytes instances instead of bare strings. It's just standard types for some decoder libraries.
Also I should notice that calling isinstance
function at runtime goes against performance. That's why it's not a good idea.
from mashumaro.
@ydylla The goal of mashumaro
's design is to maintain the best performance in most cases. But now it's possible to change the generated code with configuration such as field metadata options. I think the best way here is to add a new flag that would add isinstance
call. At the moment we could add this flag inside the metadata options of each field. If you could add a new option, there would be no problems for me to accept such a pull request :) Also I'm planning to add a global config inside a dataclass that could change default behaviour in one place. It would be nice to have a choice of where to change the parameters.
from mashumaro.
Also keep in mind that simple isinstance
could not be enough in some cases like List[Dict[str, List[float]]]
fields. For real validation we need something else.
from mashumaro.
It's just standard types for some decoder libraries
Thats also why I noticed it, I tried to use the form_dict function on Record objects returned by asyncpg, which supports types like UUID, datetime & IPv4Address.
Right now I can not use from_dict because it breaks on UUID and for the datetimes I would have to add use_datetime everywhere. And then remember to not use these code paths for dicts that come from other sources where the datetimes could be strings.
Adding an options flag (use_uuid, ...) for each type where the constructor can not accept an instance of itself feels not right. Thats why I wanted to add an isinstance check.
But you are right a global isinstance check (at the top of from_dict) does not work for nested container types.
I think a compromise would be possible where the isinstance check happens directly before the constructor of each special type:
elif origin_type is uuid.UUID:
return overridden or f"{value_name} if isinstance({value_name}, {type_name(origin_type)}) else uuid.UUID({value_name})"
This would allow the dict to contain uuids without adding a new extra option. For Path and IPv4Address the instance check is also faster than using the constructor on existing instances.
For datetimes it would be around 5% slower but in my opinion the gained flexibility and removal of use_datetime would be more important. But we probably can not remove use_dateime for compatibility reasons.
I will prepare a pull request so you can look at it with all proposed changes.
Here is the code I used for testing:
import timeit
from dataclasses import dataclass
from datetime import datetime
from ipaddress import IPv4Address
from pathlib import Path
import uuid
from uuid import UUID
from mashumaro import DataClassDictMixin
@dataclass
class A(DataClassDictMixin):
ip: IPv4Address
@dataclass
class B(DataClassDictMixin):
d: datetime
@dataclass
class C(DataClassDictMixin):
p: Path
@dataclass
class D(DataClassDictMixin):
u: UUID
if __name__ == '__main__':
# 5.283844800000001 no instance check
# 2.4646422999999995 with instance check
print(min(timeit.repeat("""A.from_dict({"ip": IPv4Address("127.0.0.1")})""", globals=globals())))
# 0.8063721000000004 no instance check
# 0.8445386000000004 with instance check, instead of use_datetime
print(min(timeit.repeat("""B.from_dict({"d": datetime.now()}, use_datetime=True)""", globals=globals())))
# 5.1607049 no instance check
# 2.6398259 with instance check
print(min(timeit.repeat("""C.from_dict({"p": Path("/a")})""", globals=globals())))
# 1.9809536999999997 with instance check
# 1.9536457 no instance check, fake use_uuid=True
# 4.0846588 with uuid.UUID(str(value))
print(min(timeit.repeat("""D.from_dict({"u": uuid.uuid4()})""", globals=globals())))
from mashumaro.
Hi @ydylla
JFYI, since I recently implemented the dialects feature, Iām planning to remove use_bytes
, use_enum
, use_datetime
parameters. These three parameters were added due to differences in third-party libraries and can now be replaced by dialects.
from mashumaro.
Related Issues (20)
- Using Union with int/float casts to whichever appears first HOT 3
- Not parsing Generics correctly HOT 2
- Unserializable field in 3.12 if defined as a Generic TypeVar with mixin bounds HOT 6
- Allow propagation of class based discriminator settings to subclasses HOT 3
- Reject extra keys on deserialization HOT 7
- Investigate support for recursive Union types HOT 2
- Supports `numpy.ndarray` type for `orjson` HOT 2
- Add support for PEP 695
- Union type of [int | float] not serialized correctly HOT 2
- Add an alternative way to assign a field alias with annotations
- Add support for PEP 696
- Annotated SerializationStrategy used as a field serialization strategy leads to RecursionError
- Suppress warning about Union type HOT 4
- Take description from docstring HOT 4
- Incorrect type annotation for the result of `to_json` in `DataClassORJSONMixin`
- Different results between standard library json and orjson HOT 3
- Python 3.12.4 Compatibility
- Some tests error out with typing_extensions>=4.12
- More issues on Python 3.12.4 HOT 5
- [BUG] to_msgpack doesn't behave properly with Discriminator(include_subtypes=True) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mashumaro.