Comments (4)
Hi @user293811
mashumaro.exceptions.UnserializableDataError: dict with dataclasses as keys is not supported by mashumaro
Let me explain why you see this exception. All the supported serialization formats (json, msgpack, yaml, toml) are kind of dictionary based and allow only simple immutable types to be keys. By default dataclasses are serialized into dicts which can't be dictionary keys because they are mutable. NamedTuples are serialized into lists or dicts depending on the engine you choose (default is lists), so they can't be dictionary keys too.
Taking all above into account, if you want to use dataclasses or named tuples as keys in your structure, you should provide a custom serialization (and deserialization for the reverse) method that will convert them into simple immutable object like string. As you have noticed, SerializableType is one of the available approaches to providing a custom serialization method. However, in the current implementation of mappings serialization there is still a code checking that the key is not a dataclass:
mashumaro/mashumaro/core/meta/types/common.py
Lines 138 to 147 in 92d539c
This is why you were forced to remove the dataclass decorator. To workaround this problem we can extend this condition by checking SerializableType subclassing. Serialization will fail with TypeError: unhashable type: 'xxx'
if dataclass is intentionally converted to unhashable type, but I think we can leave it.
The proposed fix looks trivial. I can do it but if you're willing to contribute yourself, PR is welcome.
Edit: Oh, I just noticed that using forward references breaks this fix on python 3.8. It would be better if I looked at it myself.
from mashumaro.
I modified the process of checking to check if the key type is Hashable. This alteration will also trigger an UnserializableField
exception for other types that are not hashable.
If you remove frozen=True
parameter from NodeIdentifier
the following exception will now be raised:
mashumaro.exceptions.UnserializableField: Field "network" of type dict[NodeIdentifier, NodeConfig] in NetworkConfig is not serializable: NodeIdentifier is unhashable and can not be used as a key
from mashumaro.
Ok, I kind of gotten it to work with SerializableType if I drop the dataclass decorator on the NodeIdentifier, but printing it out isn't pretty. It would be really great dataclass serialization as keys was supported.
from mashumaro.
I tried subclassing NamedTuple and got:
mashumaro.exceptions.UnserializableDataError: ForwardRef('str') as a field type is not supported by mashumaro
This looks like another problem related to ForwardRef support. I managed to support forward references to TypedDicts here but this is something new. Could you provide a reproducible example with details in a separate issue? I'd like to cover as many cases as possible.
from mashumaro.
Related Issues (20)
- Add support for ForwardRef HOT 6
- Drop Python 3.7 support
- Allow basic types used as serialization methods to be reflected in the JSON schema
- Switch to ruff from flake8
- Comprehension inlining on Python 3.12.0rc1 breaks some tests HOT 1
- Add support for Python 3.12
- Add support for PEP 696 HOT 1
- Add an option to omit values equal to defaults on serialization
- Add an option to avoid collection data types copying
- Add support for LiteralString
- Generic dataclasses with Hashable typevar bound do not work HOT 3
- Query string support HOT 5
- Benchmark is not working in master branch HOT 2
- omit_default=True evaluates default_factory value at declaration time which can cause a circular reference HOT 3
- Multiple aliases for field HOT 6
- "`dict[str, T]` as a field type is not supported" HOT 2
- InitVar with no default value HOT 6
- The types defined inside the function result in syntactically invalid generated code
- Allow for more complex logic for subclass Discrimnator field matching HOT 3
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.