[New-bugs-announce] [issue44365] Bad dataclass post-init example

Micael Jarniac report at bugs.python.org
Wed Jun 9 11:33:08 EDT 2021


New submission from Micael Jarniac <micael at jarniac.com>:

https://docs.python.org/3/library/dataclasses.html#post-init-processing

https://github.com/python/cpython/blob/3.9/Doc/library/dataclasses.rst#post-init-processing

In the example, a base class "Rectangle" is defined, and then a "Square" class inherits from it.

On reading the example, it seems like the Square class is meant to be used like:

>>> square = Square(5)

Since the Square class seems to be supposed to be a "shortcut" to creating a Rectangle with equal sides.

However, the Rectangle class has two required init arguments, and when Square inherits from it, those arguments are still required, so using Square like in the above example, with a single argument, results in an error:

>>> square = Square(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 2 required positional arguments: 'width' and 'side'

To "properly" use the Square class, it'd need to be instantiated like so:

>>> square = Square(0, 0, 5)
>>> square
Square(height=5, width=5, side=5)

Which, in my opinion, is completely counter-intuitive, and basically invalidates this example.

----------
assignee: docs at python
components: Documentation
messages: 395427
nosy: MicaelJarniac, docs at python
priority: normal
severity: normal
status: open
title: Bad dataclass post-init example
type: behavior
versions: Python 3.8

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


More information about the New-bugs-announce mailing list