Message 309628 - Python tracker (original) (raw)

Modify dataclasses to make it easier to specify special methods.

For example: currently, if you want to override repr, you need to specify @dataclass(repr=False), and also provide your own repr. Also, it's current an error to not specify repr=False and to also provide your own repr. @dataclass should be able to determine that if you provide repr, you don't want it to provide it's own repr.

The methods that @dataclass will provide for you are: init repr eq ne lt le gt ge hash_ and if using frozen=True, also: setattr delattr

Per the discussion that started at https://mail.python.org/pipermail/python-dev/2017-December/151487.html, the change will be to override these methods only if:

  1. the method would have been generated based on @dataclass params, and
  2. the method is not present in the class's dict.

The first item is to allow the user to continue to suppress method generation, and the second item is so that base-class methods are not considered in the decision.

I'm still thinking through how eq and ne should be handled (as well as the ordering comparisons, too). I'll raise an issue on python-dev and point it here.