Skip to content

Commit 43e2633

Browse files
committed
Unvendor unwrap, call it only when needed
1 parent b1578d5 commit 43e2633

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

Lib/dataclasses.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -431,28 +431,6 @@ def _tuple_str(obj_name, fields):
431431
# Note the trailing comma, needed if this turns out to be a 1-tuple.
432432
return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
433433

434-
# NOTE: This is a (simplified) vendored copy of `inspect.unwrap` to speed up import time
435-
def _unwrap(func):
436-
"""Get the object wrapped by *func*.
437-
438-
Follows the chain of :attr:`__wrapped__` attributes returning the last
439-
object in the chain.
440-
441-
:exc:`ValueError` is raised if a cycle is encountered.
442-
"""
443-
f = func # remember the original func for error reporting
444-
# Memoise by id to tolerate non-hashable objects, but store objects to
445-
# ensure they aren't destroyed, which would allow their IDs to be reused.
446-
memo = {id(f): f}
447-
recursion_limit = sys.getrecursionlimit()
448-
while not isinstance(func, type) and hasattr(func, '__wrapped__'):
449-
func = func.__wrapped__
450-
id_func = id(func)
451-
if (id_func in memo) or (len(memo) >= recursion_limit):
452-
raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
453-
memo[id_func] = func
454-
return func
455-
456434

457435
class _FuncBuilder:
458436
def __init__(self, globals):
@@ -1009,6 +987,7 @@ class AutoDocstring:
1009987
"""
1010988

1011989
def __get__(self, _obj, cls):
990+
# TODO: Make this top-level lazy import once PEP810 lands
1012991
import inspect
1013992

1014993
try:
@@ -1410,8 +1389,12 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
14101389
# make an update, since all closures for a class will share a
14111390
# given cell.
14121391
for member in newcls.__dict__.values():
1392+
14131393
# If this is a wrapped function, unwrap it.
1414-
member = _unwrap(member)
1394+
if not isinstance(member, type) and hasattr(member, '__wrapped__'):
1395+
# TODO: Make this top-level lazy import once PEP810 lands
1396+
import inspect
1397+
member = inspect.unwrap(member)
14151398

14161399
if isinstance(member, types.FunctionType):
14171400
if _update_func_cell_for__class__(member, cls, newcls):

0 commit comments

Comments
 (0)