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

gh-109653: Just import recursive_repr in dataclasses #109822

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import types
import inspect
import keyword
import functools
import itertools
import abc
import _thread
from reprlib import recursive_repr
from types import FunctionType, GenericAlias


Expand Down Expand Up @@ -245,25 +244,6 @@ def __repr__(self):
property,
})

# This function's logic is copied from "recursive_repr" function in
# reprlib module to avoid dependency.
def _recursive_repr(user_function):
# Decorator to make a repr function return "..." for a recursive
# call.
repr_running = set()

@functools.wraps(user_function)
def wrapper(self):
key = id(self), _thread.get_ident()
if key in repr_running:
return '...'
repr_running.add(key)
try:
result = user_function(self)
finally:
repr_running.discard(key)
return result
return wrapper

class InitVar:
__slots__ = ('type', )
Expand Down Expand Up @@ -322,7 +302,7 @@ def __init__(self, default, default_factory, init, repr, hash, compare,
self.kw_only = kw_only
self._field_type = None

@_recursive_repr
@recursive_repr()
def __repr__(self):
return ('Field('
f'name={self.name!r},'
Expand Down Expand Up @@ -632,7 +612,7 @@ def _repr_fn(fields, globals):
for f in fields]) +
')"'],
globals=globals)
return _recursive_repr(fn)
return recursive_repr()(fn)


def _frozen_get_del_attr(cls, fields, globals):
Expand Down
Loading