Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump returns methods from list #139

Open
jessekerkhoven opened this issue Jul 29, 2021 · 3 comments
Open

Dump returns methods from list #139

jessekerkhoven opened this issue Jul 29, 2021 · 3 comments

Comments

@jessekerkhoven
Copy link

jessekerkhoven commented Jul 29, 2021

When dumping a List of a dataclasses it returns method names instead of the data.

This issues is introduced in V1.5.0 (v1.4.2 works as expected). I think commit d2b36fd introduced the issue.

See below for minimal poc to replicate the issue:

@dataclass
class Child:
  name: str

@dataclass
class Person:
  children: List[Child]

test = {"children":[{"name": "Ramon"}]}

jsons.dump(Person(**test))

results in {'children': [{'clear': {}, 'copy': {}, 'fromkeys': {}, 'get': {}, 'items': {}, 'keys': {}, 'pop': {}, 'popitem': {}, 'setdefault': {}, 'update': {}, 'values': {}}]}

@ramonhagenaars
Copy link
Owner

Hi Jesse, thanks for reporting.

I've looked into your issue and I think that the problem lies with the way you instantiated Person here.

>>> test = {"children":[{"name": "Ramon"}]}
>>> Person(**test)
Person(children=[{'name': 'Ramon'}])

Notice that for this instance children holds a list of dicts rather than a list of Child instances as you presumably intended.

The following works as expected:

>>> jsons.dump(Person(children=[Child(name="Ramon")]))
{'children': [{'name': 'Ramon'}]}

@jessekerkhoven
Copy link
Author

jessekerkhoven commented Jul 29, 2021

Nice catch, I didn't noticed that. This is a issue indeed.

But nevertheless what explains the difference between version 1.4.2 and 1.5.0?

@ramonhagenaars
Copy link
Owner

As of 1.5.0, there is a dedicated list_serializer that takes the type(...) of an element in a list. It allows for better performance, because it can inspect the generic type of a list once and use it for each element.

To be honest, I would have expected jsons to raise an error here, since it should have tried to dump a dict as if it were a Child (which won't work). It does behave as such when dumping with strict=True though.

I think it's a bit hairy, I'll check into this a bit more and may still come with a patch eventually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants