Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataclasses can raise RecursionError in __repr__ #78128

Closed
ericvsmith opened this issue Jun 23, 2018 · 5 comments
Closed

Dataclasses can raise RecursionError in __repr__ #78128

ericvsmith opened this issue Jun 23, 2018 · 5 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ericvsmith
Copy link
Member

BPO 33947
Nosy @rhettinger, @ericvsmith, @remilapeyre, @tirkarthi
PRs
  • Closes bpo-33947: Dataclasses can raise RecursionError in __repr__ #9916
  • [3.7] bpo-33947: dataclasses no longer can raise RecursionError in repr (GF9916) #9970
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = <Date 2018-10-19.17:29:46.158>
    created_at = <Date 2018-06-23.12:58:46.131>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'Dataclasses can raise RecursionError in __repr__'
    updated_at = <Date 2018-10-19.17:29:46.157>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2018-10-19.17:29:46.157>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2018-10-19.17:29:46.158>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2018-06-23.12:58:46.131>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33947
    keywords = ['patch']
    message_count = 5.0
    messages = ['320305', '320322', '320327', '320331', '328055']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'eric.smith', 'remi.lapeyre', 'xtreak']
    pr_nums = ['9916', '9970']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33947'
    versions = ['Python 3.7', 'Python 3.8']

    @ericvsmith
    Copy link
    Member Author

    >>> @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=...)".

    @ericvsmith ericvsmith added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 23, 2018
    @remilapeyre
    Copy link
    Mannequin

    remilapeyre mannequin commented Jun 23, 2018

    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?

    @rhettinger
    Copy link
    Contributor

    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__.

    @ericvsmith
    Copy link
    Member Author

    Thanks, Raymond. I'm working on a patch.

    @ericvsmith ericvsmith added the 3.8 only security fixes label Jun 23, 2018
    @ericvsmith ericvsmith self-assigned this Oct 1, 2018
    @ericvsmith
    Copy link
    Member Author

    New changeset b9182aa by Eric V. Smith (Miss Islington (bot)) in branch '3.7':
    bpo-33947: dataclasses no longer can raise RecursionError in repr (GF9916) (bpo-9970)
    b9182aa

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants