Skip to content

[BUG] from_json of class with attribute of list of types which inherit basic types results in wrong result #485

Open
@nirh-cye

Description

@nirh-cye

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

import json

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin


class Name(str):
    ...


@dataclass
class Person(DataClassJsonMixin):
    names: list[Name]


if __name__ == '__main__':
    johnny = Person(names=[Name('John Doe')])
    with open('storage.json', 'w') as f:
        json.dump(johnny.to_json(), f)

    ...

    with open('storage.json', 'r') as f:
        johnny_again = Person.from_json(json.load(f))

    print(isinstance(johnny_again.names[0], Name))  # prints: False

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

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions