[Python-Dev] Concerns about method overriding and subclassing with dataclasses

Ethan Furman ethan at stoneleaf.us
Fri Dec 29 14:37:38 EST 2017


On 12/29/2017 02:23 AM, Ethan Smith wrote:

> The first is that needing both a keyword and method is duplicative and unnecessary. Eric agreed it was a hassle, but
> felt it was justified considering someone may accidentally override a dataclass method. I disagree with this point of
> view as dataclasses are billed as providing automatic methods. Overriding via method definition is very natural and
> idiomatic. I don't really see how someone could accidentally override a dataclass method if methods were not generated
> by the dataclass decorator that are already defined in the class at definition time.

Accidental or not, the decorator should not be replacing methods defined by the class.

> The second concern, which I came across more recently, is if I have a base class, and dataclasses inherit from this base
> class, inherited __repr__ & co are silently overridden by dataclass. This is both unexpected, and also means I need to
> pass a repr=False to each subclass' decorator to get correct behavior, which somewhat defeats the utility of
> subclassing. Im not as sure a whole lot can be done about this though.

It is possible to determine whether an existing __repr__ is from 'object' or not, and only provide one if that is the 
case.  I think that should be the default, with 'repr = True' for those cases where a new, auto-generated, __repr__ is 
desired.

--
~Ethan~


More information about the Python-Dev mailing list