[New-bugs-announce] [issue46938] dataclass __post_init__ recursion

Bar Harel report at bugs.python.org
Sun Mar 6 06:34:34 EST 2022


New submission from Bar Harel <bzvi7919 at gmail.com>:

Not sure if a continuance of https://bugs.python.org/issue44365 or not, but the suggestion to call super().__init__() in __post__init__ will cause infinite recursion if the superclass also contains __post__init__:

>>> @d
... class A:
...  test: int
...  def __post_init__(self):
...    pass

>>> @d
... class B(A):
...  test2: int
...  def __post_init__(self):
...    super().__init__(test=1)

>>> B(test2=1, test=3) <-- infinite recursion.

This is caused by line 564 (https://github.com/python/cpython/blob/4716f70c8543d12d18c64677af650d479b99edac/Lib/dataclasses.py#L564) checking for post init on current class, and calling it on self (child class).

Not sure if the bug is in the documentation/suggestion, or if it's a bug in the implementation, in which case we need to call the current class's __post_init__.

----------
assignee: docs at python
components: Documentation, Library (Lib)
messages: 414613
nosy: bar.harel, docs at python
priority: normal
severity: normal
status: open
title: dataclass __post_init__ recursion
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

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


More information about the New-bugs-announce mailing list