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)
- 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.