[Python-Dev] Concerns about method overriding and subclassing with dataclasses (original) (raw)

Ethan Smith ethan at ethanhs.me
Fri Dec 29 14:55:38 EST 2017


On Fri, Dec 29, 2017 at 11:37 AM, Ethan Furman <ethan at stoneleaf.us> wrote:

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.

The only other thing you'd want to handle is to cover inheriting from another dataclass. e.g., if I have dataclass with attribute a, and subclass it to add attribute b, I'd want both in the repr.

~>Ethan Smith

-- Ethan


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ethan% 40ethanhs.me -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20171229/606f6f68/attachment.html>



More information about the Python-Dev mailing list