[issue46757] dataclasses should define an empty __post_init__

Nikita Sobolev report at bugs.python.org
Sat Feb 19 02:09:51 EST 2022


Nikita Sobolev <mail at sobolevn.me> added the comment:

I like this idea.

`attrs` right now behave the same way (no default `__attrs_post_init__`:

```
>>> import attrs
>>> @attrs.define
... class Some:
...   x: int
... 
>>> @attrs.define
... class Other(Some):
...    def __attrs_post_init__(self):
...       super().__attrs_post_init__()
...       self.x += 1
... 
>>> Other(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<attrs generated init __main__.Other>", line 3, in __init__
  File "<stdin>", line 4, in __attrs_post_init__
AttributeError: 'super' object has no attribute '__attrs_post_init__'
```

I am attaching a simple patch.

Alternative solution is to not merge this patch and recommend this instead:

```
def __post_init__(self):
  try:
    super().__post_init__()
  except AttributeError:
    pass
```

----------
nosy: +sobolevn

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


More information about the Python-bugs-list mailing list