Giter Site home page Giter Site logo

Comments (5)

matt035343 avatar matt035343 commented on June 22, 2024

Seems to be a duplicate of #281

from dataclasses-json.

NiroHaim avatar NiroHaim commented on June 22, 2024

Oh! seems I missed it
anyways, I wrote a small function that gets all initvars from a dataclass, only did basic testing of course but maybe this could help:

    @classmethod
    def _get_initvar_names(cls) -> list[str]:
        annotations = cls.__annotations__.copy()

        for base in cls.__bases__:
            if hasattr(base, "__annotations__"):
                annotations.update(base.__annotations__)
        return [name for name, typ in annotations.items() if isinstance(typ, dataclasses.InitVar)]

On another note, I saw that the issue remains open from 2021, are there currently any intentions to resolve it?

from dataclasses-json.

matt035343 avatar matt035343 commented on June 22, 2024

Hi @NiroHaim, this repo has been without much activity for a long time, so issues have been piling up. A few new maintainers have been added (including myself), so we will try to reduce the backlog as much as possible. This includes looking into the issue described here. I cannot guarantee when, but we will definitely have a look when time allows.

from dataclasses-json.

matt035343 avatar matt035343 commented on June 22, 2024

When I am looking at the documentation for InitVar, it looks like it is supposed to be passed to the constructor, but it should not be accessible from the outside afterwards, e.g. through fields(). Therefore, I would not expect it being returned from to_dict() or to_json() either. Therefore, A.from_dict(A(a_init=1).to_dict()) would never work if we were to respect this assumption.

@NiroHaim What are you expectations to handling this discrepancy?

Looping in contributors of the original issue @AbdulRahmanAlHamali and @ALiangLiang. Feel free to contribute to the discussion.

from dataclasses-json.

NiroHaim avatar NiroHaim commented on June 22, 2024

Hey @matt035343 , Thanks for the response
I believe that the correct behaviour would indeed not be to include a_init as a key in the to_dict() result, that would just be odd in the general case. (Also changing the output of to_dict would probably be a pain for some projects to upgrade which you would probably want to avoid)
However, I do think that as the InitVars are valid init attributes that are expected to be received, I expect them not to be filtered out when a user chooses to add them to a dict and use from_dict on said dict.
If you want to limit the usage of from_dict to only receive input from the to_dict function that Is fine, but if the purpose is for them to be seperate functions that also work in tandem, I think you should filter out all InitVars through a method similar to what I suggested above and just make sure that the values are there for the init call inside the from_dict function.

**Do note that adding the option for passing initvars to from_dict would allow fun features such as wrapping from_dict to change the private _a to a_init keyword and making the a prop a counter of how many times this json has been converted to A type by adding 1 to a_init each time.
This is of course a rather dull case, however the option of passing InitVars would be very beneficial by allowing the user more freedom when using and wrapping the class.

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.