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

String deserialized as datetime despite target type annotation #169

Closed tbarkai closed 2 years ago

tbarkai commented 2 years ago

This code:

@dataclass
class Sample:
    prop: str

def unrelated_date_deserializer(obj: str, cls: type = datetime, **kwargs) -> datetime:
    return datetime.strptime(obj, '%Y')

jsons.set_deserializer(unrelated_date_deserializer, datetime)

json = {'prop': '1025'}
print(jsons.load(json, Sample, strict=True))

Outputs:

Sample(prop=datetime.datetime(1025, 1, 1, 0, 0))

Shouldn't the str type annotation be used to decide whether to employ the deserializer or not?

ramonhagenaars commented 2 years ago

Hi @tbarkai. You are right: this is flawed, a string is to be expected. I will work on a fix.