ramonhagenaars / jsons

🐍 A Python lib for (de)serializing Python objects to/from JSON
https://jsons.readthedocs.io
MIT License
289 stars 41 forks source link

Deserialisation error on parameterised dictionary under Python 3.9 #133

Closed ghost closed 3 years ago

ghost commented 3 years ago
"""Minimum example"""
from dataclasses import dataclass
import jsons # version 1.4.1

@dataclass
class Data():
    data: dict[str, str]

data = Data({'hello': 'world'})

s = jsons.dump(data)
jsons.load(s, Data) # DeserializationError

jsons.exceptions.DeserializationError: Could not deserialize value "{'hello': 'world'}" into "dict". isinstance() argument 2 cannot be a parameterized generic

I can only find two references to this exception in PEP 585 and PEP 604.

This can be resolved by creating a separate class without parameters:

...

class Data_():
    data: dict

ds = jsons.load(s, Data_)
print(ds.data)

This workaround does not work for me since I have an object subclassing dict with hashed keys that require a custom deserialiser from list[int] -> range:

jsons.exceptions.DeserializationError: A detailed type is needed for cls of the form Dict[<type>, <type>] to deserialize a dict with hashed keys.

ramonhagenaars commented 3 years ago

This is a bug which will be resolved in version 1.4.2. Please check if that version works for you and let me know otherwise.