You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a dataclass that holds an attribute of list (if names was type of name and not list of Name, issue would not reproduce) of a class that inherits the basic type: string.
When creating an instance of this class, converting it into json, writing said json to a file and later reading it and re-creating the object from it, the object structure is not identical to the previous object structure.
The attribute of list that contains objects which inherit the basic type creates the list objects as the basic type instead of their expected type.
johnny.to_json already returns a string. If you do f.write(johnny.to_json()), do you still have an issue?
Yes, the reason I am writing to a file in this example is that Person.from_json(johnny.to_json()) would indeed work as expected, the reason being that johnny.to_json would not change the names inner elements to string, it would leave them as they are and therefore would not convert them back in the from_json function.
When this is written to a file, the text: '{"names": ["John Doe"]}' is dumped into it. when attempting to convert that into Person via the Person.from_json function, the type of the names elements will indeed be incorrect.
In short, the writing into a file is not required and the following will also reproduce this bug:
Description
I have a
dataclass
that holds an attribute of list (if names was type of name and not list of Name, issue would not reproduce) of a class that inherits the basic type:string
.When creating an instance of this class, converting it into json, writing said json to a file and later reading it and re-creating the object from it, the object structure is not identical to the previous object structure.
The attribute of list that contains objects which inherit the basic type creates the list objects as the basic type instead of their expected type.
Code snippet that reproduces the issue
Describe the results you expected
As the original type was indeed Name (before being written into the file) I expected the object after reading the json to also be Name
Python version you are using
3.10
Environment description
Standard clean env. only used
pip install dataclasses-json
The text was updated successfully, but these errors were encountered: