[issue46757] dataclasses should define an empty __post_init__

2022-02-22 Thread Neil Girdhar
Neil Girdhar added the comment: @eric Good thinking. Would it make sense to add to the documentation as well that the __post_init__ methods aren't collected, and you should call super's __post_init__ if there is one using something like if hasattr(super(), "__post_init__"):

[issue46757] dataclasses should define an empty __post_init__

2022-02-22 Thread Eric V. Smith
Eric V. Smith added the comment: New changeset 288af845a32fd2a92e3b49738faf8f2de6a7bf7c by Eric V. Smith in branch 'main': bpo-46757: Add a test to verify dataclass's __post_init__ isn't being automatically added. (GH-31523)

[issue46757] dataclasses should define an empty __post_init__

2022-02-22 Thread Eric V. Smith
Eric V. Smith added the comment: I'm adding a test that mimic's Raymond's example of the proposed addition being a breaking change. This way, if we ever decide to actually add this feature, we'll break this test. If we do decide to continue and make the change anyway, at least we'll do so

[issue46757] dataclasses should define an empty __post_init__

2022-02-22 Thread Eric V. Smith
Change by Eric V. Smith : -- pull_requests: +29650 pull_request: https://github.com/python/cpython/pull/31523 ___ Python tracker ___

[issue46757] dataclasses should define an empty __post_init__

2022-02-22 Thread Eric V. Smith
Eric V. Smith added the comment: I'm going to close this issue. As Raymond says, it's a breaking change, and the workaround is easy enough. -- resolution: -> rejected stage: patch review -> resolved status: open -> closed ___ Python tracker

[issue46757] dataclasses should define an empty __post_init__

2022-02-21 Thread Neil Girdhar
Neil Girdhar added the comment: @Raymond yeah I've been thinking about this some more, and there's no way to have a "top level" method with the dataclass decorator. I think I will make a case to have a class version of dataclasses that works with inheritance. Class versions of dataclasses

[issue46757] dataclasses should define an empty __post_init__

2022-02-21 Thread Raymond Hettinger
Raymond Hettinger added the comment: Note that adding an empty __post_init__ method would be a breaking change. The following prints out 'B' then 'C'. But if class A adds an empty __post_init__, then 'B' never gets printed. The arrangement relies on class A being a passthrough to class

[issue46757] dataclasses should define an empty __post_init__

2022-02-20 Thread Raymond Hettinger
Raymond Hettinger added the comment: -1 * I concur with Eric that this is mostly not needed. Probably 99.95% of dataclass use case don't need this. When it is needed, it is trivial to implement and probably should be explicit rather that implicit. * Vedran is also correct in noting that

[issue46757] dataclasses should define an empty __post_init__

2022-02-20 Thread Eric V. Smith
Change by Eric V. Smith : -- assignee: -> eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue46757] dataclasses should define an empty __post_init__

2022-02-20 Thread Neil Girdhar
Neil Girdhar added the comment: > How would an arbitrary derived class know how to call this? It can't. There > has to be knowledge of the base class's requirements already. Surely knowing > "__post_init__ must be called with some_arg" isn't too different from "I know > __post_init__

[issue46757] dataclasses should define an empty __post_init__

2022-02-19 Thread Eric V. Smith
Eric V. Smith added the comment: The fact that it's never been needed in the years that dataclasses and attrs have existed tell me it's kind of a niche requirement. This does not seem like the ugliest code I've ever seen: if hasattr(super(), "__post_init__"):

[issue46757] dataclasses should define an empty __post_init__

2022-02-19 Thread Neil Girdhar
Neil Girdhar added the comment: > I'm not crazy about adding a method to every dataclass just for the 0.1% of > the times it's needed. I'm not sure I totally understand the cost here. You can have a single shared global function that you set on each dataclass. So the only cost would be an

[issue46757] dataclasses should define an empty __post_init__

2022-02-19 Thread Eric V. Smith
Eric V. Smith added the comment: I'm not crazy about adding a method to every dataclass just for the 0.1% of the times it's needed. I think using hasattr or catching the exception is a better way to go. -- ___ Python tracker

[issue46757] dataclasses should define an empty __post_init__

2022-02-19 Thread Neil Girdhar
Neil Girdhar added the comment: On Sat, Feb 19, 2022 at 2:51 AM Vedran Čačić wrote: > > Vedran Čačić added the comment: > > That "except AttributeError" approach is a powerful bug magnet, since it > can very easily mask real attribute errors stemming from misspelled > attribute names in the

[issue46757] dataclasses should define an empty __post_init__

2022-02-18 Thread Vedran Čačić
Vedran Čačić added the comment: That "except AttributeError" approach is a powerful bug magnet, since it can very easily mask real attribute errors stemming from misspelled attribute names in the __post_init__ call itself. What you should _really_ do is def __post_init__(self):

[issue46757] dataclasses should define an empty __post_init__

2022-02-18 Thread Nikita Sobolev
Change by Nikita Sobolev : -- type: -> behavior versions: +Python 3.11 ___ Python tracker ___ ___ Python-bugs-list mailing list

[issue46757] dataclasses should define an empty __post_init__

2022-02-18 Thread Nikita Sobolev
Change by Nikita Sobolev : -- keywords: +patch pull_requests: +29565 stage: -> patch review pull_request: https://github.com/python/cpython/pull/31430 ___ Python tracker ___

[issue46757] dataclasses should define an empty __post_init__

2022-02-18 Thread Nikita Sobolev
Nikita Sobolev added the comment: I like this idea. `attrs` right now behave the same way (no default `__attrs_post_init__`: ``` >>> import attrs >>> @attrs.define ... class Some: ... x: int ... >>> @attrs.define ... class Other(Some): ...def __attrs_post_init__(self): ...

[issue46757] dataclasses should define an empty __post_init__

2022-02-15 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue46757] dataclasses should define an empty __post_init__

2022-02-15 Thread Neil Girdhar
New submission from Neil Girdhar : When defining a dataclass, it's possible to define a post-init (__post_init__) method to, for example, verify contracts. Sometimes, when you inherit from another dataclass, that dataclass has its own post-init method. If you want that method to also do its