[issue46757] dataclasses should define an empty __post_init__

Neil Girdhar report at bugs.python.org
Sat Feb 19 03:07:03 EST 2022


Neil Girdhar <mistersheik at gmail.com> added the comment:

On Sat, Feb 19, 2022 at 2:51 AM Vedran Čačić <report at bugs.python.org> wrote:

>
> Vedran Čačić <vedgar at gmail.com> 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):
>         with contextlib.suppress(AttributeError):
>             post_init = super().__post_init__
>         post_init()
>
> But of course, nobody will ever write that.
>
> Great point!

Raymond in his "super considered super" video (
> https://youtu.be/xKgELVmrqfs?t=2068) says the right thing to do is to
> make your own root which knows exactly what classes it manages, and drops
> the supercalls from them (after possibly verifying that all kwargs have
> actually been used and so on).
>
> But in case of dataclasses, usually any class can serve as such a root,
> and the main reason people use dataclasses is to avoid boilerplate code. So
> I think it would be a nice compromise.
>

I'm not sure I understand what you're saying here.  Anyone can inherit from
a class C with the special root and some other class D, and then your
introduced root won't catch D's super calls.

>
> ----------
> nosy: +veky
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <https://bugs.python.org/issue46757>
> _______________________________________
>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46757>
_______________________________________


More information about the Python-bugs-list mailing list