This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Dataclasses can raise RecursionError in __repr__
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, remi.lapeyre, rhettinger, xtreak
Priority: normal Keywords: patch

Created on 2018-06-23 12:58 by eric.smith, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9916 merged thatiparthy, 2018-10-16 19:08
PR 9970 merged miss-islington, 2018-10-19 16:55
Messages (5)
msg320305 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-23 12:58
>>> @dataclass
... class C:
...   f: "C"
...
>>> c = C(None)
>>> c.f = c
>>> c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2, in __repr__
  File "<string>", line 2, in __repr__
  File "<string>", line 2, in __repr__
  [Previous line repeated 328 more times]
RecursionError: maximum recursion depth exceeded
>>>

It would be better to produce "C(f=...)".
msg320322 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2018-06-23 19:13
This seems like a difficult problem to tackle in all cases, if two dataclasses reference each other the cycle could be complex to identify and introduce complexity.

The way repr is defined is part of PEP 557 and actually force this behavior.

Should the `repr` parameter default to False or not include the repr of each field?
msg320327 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-23 19:29
We have a tool designed for this problem.  See: https://docs.python.org/3/library/reprlib.html#reprlib.recursive_repr

If you want to avoid a dependency, it is easy to inline the logic.  For an example, see the Python 2.7 version of collections.OrderedDict.__repr__.
msg320331 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-23 20:15
Thanks, Raymond. I'm working on a patch.
msg328055 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-10-19 17:28
New changeset b9182aa7dad8991fc8768ae494b45b5f7c316aca by Eric V. Smith (Miss Islington (bot)) in branch '3.7':
bpo-33947:  dataclasses no longer can raise RecursionError in repr (GF9916) (#9970)
https://github.com/python/cpython/commit/b9182aa7dad8991fc8768ae494b45b5f7c316aca
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78128
2018-10-19 17:29:46eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-10-19 17:28:35eric.smithsetmessages: + msg328055
2018-10-19 16:55:05miss-islingtonsetpull_requests: + pull_request9315
2018-10-16 19:08:05thatiparthysetkeywords: + patch
stage: patch review
pull_requests: + pull_request9273
2018-10-01 10:58:19eric.smithsetassignee: eric.smith
2018-09-23 14:47:24xtreaksetnosy: + xtreak
2018-06-23 20:15:00eric.smithsetmessages: + msg320331
versions: + Python 3.8
2018-06-23 19:29:13rhettingersetpriority: low -> normal
nosy: + rhettinger
messages: + msg320327

2018-06-23 19:13:22remi.lapeyresetnosy: + remi.lapeyre
messages: + msg320322
2018-06-23 12:58:46eric.smithcreate