Giter Site home page Giter Site logo

[BUG] from_json of class with attribute of list of types which inherit basic types results in wrong result about dataclasses-json HOT 3 OPEN

NiroHaimo avatar NiroHaimo commented on September 26, 2024
[BUG] from_json of class with attribute of list of types which inherit basic types results in wrong result

from dataclasses-json.

Comments (3)

george-zubrienko avatar george-zubrienko commented on September 26, 2024

I am not sure what is happening here:

with open('storage.json', 'w') as f:
        json.dump(johnny.to_json(), f)

johnny.to_json already returns a string. If you do f.write(johnny.to_json()), do you still have an issue?

from dataclasses-json.

NiroHaimo avatar NiroHaimo commented on September 26, 2024

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:

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin


class Name(str):
    ...


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


if __name__ == '__main__':
    js = '{"names": ["John Doe"]}'
    print(isinstance(Person.from_json(js).names[0], Name))  # prints: False

from dataclasses-json.

george-zubrienko avatar george-zubrienko commented on September 26, 2024

I'll retest this on latest commit tomorrow to see if this is some similar issue with 3.10 as with self-types

from dataclasses-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.