This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients NeilGirdhar, eric.smith, sobolevn, veky
Date 2022-02-19.17:44:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1645292643.34.0.850638654611.issue46757@roundup.psfhosted.org>
In-reply-to
Content
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__"):
            super().__post_init__()

or the probably more efficient, but more lines of code:

        try:
            post_init = super().__post_init__
        except AttributeError:
            pass
        else:
            post_init()

As always with calling super functions, the whole hierarchy needs to cooperate. Say you had a dataclass:

@dataclass
class Base:
    def __post_init__(self, some_arg):
        pass

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__ doesn't exist". I don't think adding ways to make the "always call super" pattern easier is a good idea.

I'm still unconvinced, but I'll hold off on making a decision to see if there's more support. Maybe taking it to python-ideas would be worthwhile.
History
Date User Action Args
2022-02-19 17:44:03eric.smithsetrecipients: + eric.smith, NeilGirdhar, veky, sobolevn
2022-02-19 17:44:03eric.smithsetmessageid: <1645292643.34.0.850638654611.issue46757@roundup.psfhosted.org>
2022-02-19 17:44:03eric.smithlinkissue46757 messages
2022-02-19 17:44:03eric.smithcreate