Giter VIP home page Giter VIP logo

Comments (4)

Fatal1ty avatar Fatal1ty commented on May 23, 2024 1

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:

def ensure_collection_type_args_supported(
collection_type: Type,
type_args: Sequence[Type],
) -> bool:
if type_args and is_dataclass(type_args[0]):
raise UnserializableDataError(
f"{type_name(collection_type, short=True)} "
f"with dataclasses as keys is not supported by mashumaro"
)
return True

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.

Fatal1ty avatar Fatal1ty commented on May 23, 2024 1

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.

user293811 avatar user293811 commented on May 23, 2024

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.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.