Coder Social home page Coder Social logo

Comments (7)

biqqles avatar biqqles commented on August 30, 2024

Hi, thanks for this report. I wonder if it wouldn't it be enough to simply change line 85 to something like

return type.__new__(DataClassInit if options['init'] and user_init else mcs, name, bases, dict_)

This is optimal because it avoids the definition of a slow Python __call__.

Apologies, I am away for exams and will likely not be able to test and/or merge for a while.

from dataclassy.

kgreav avatar kgreav commented on August 30, 2024

No worries and no rush, I'll test it out - already set up everything in my repo so I'm not beholden to the PR being merged.

from dataclassy.

kgreav avatar kgreav commented on August 30, 2024

OK, after testing your solution is equivalent to mine in function but optimal, however I realised there is one breaking change (applicable to both versions). Personally I don't think it is a bad breaking change as the usage was quite misleading.

In the existing, the following works:

@dataclass(init=False)
class Obj(object):
    a: str
    b: str

    def __init__(self, c, d):
        self.a = c
        self.b = d

o = Obj("x", "y", "w", "z")
assert o.a = "w"
assert o.b = "z"

whereas in the new version this will fail, and would need to be changed to:

Obj("w", "z")

This is obviously a naive example, and someone could have more contrived examples that make sense, however the worst case would be they'd have to add their classes attributes to their __init__ function which I think is desirable anyway, as from what I've seen you can't currently get a meaningful constructor signature out of this example unless you document it, as it's an amalgamation of __new__ and __init__.

from dataclassy.

biqqles avatar biqqles commented on August 30, 2024

I think it's fair to call the behaviour you discovered a bug - init is false and yet the arguments its function would have had are still being consumed. This was an oversight on my part - there is absolutely no reason to use a custom __call__ to modify arguments if there isn't both __init__ and __new__ on the class. If someone is using this obscure behaviour, for some reason, it is a breaking change - but we're still on 0.x releases so what can they expect!

from dataclassy.

biqqles avatar biqqles commented on August 30, 2024

I took a few moments to push the fix for this. Any problems, just let me know.

from dataclassy.

biqqles avatar biqqles commented on August 30, 2024

Fix released in v0.6.2. Also, apologies, I didn't realise you had incorporated my solution into your PR before I unceremoniously closed it.

from dataclassy.

kgreav avatar kgreav commented on August 30, 2024

Not a problem at all, the quick turnaround more than makes up for it - I didn't get around to fixing/writing tests anyway. Thanks a bunch!

from dataclassy.

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.