MCPcopy Index your code
hub / github.com/python/cpython / _astuple_inner

Function _astuple_inner

Lib/dataclasses.py:1592–1625  ·  view source on GitHub ↗
(obj, tuple_factory)

Source from the content-addressed store, hash-verified

1590
1591
1592def _astuple_inner(obj, tuple_factory):
1593 if type(obj) in _ATOMIC_TYPES:
1594 return obj
1595 elif _is_dataclass_instance(obj):
1596 return tuple_factory([
1597 _astuple_inner(getattr(obj, f.name), tuple_factory)
1598 for f in fields(obj)
1599 ])
1600 elif isinstance(obj, tuple) and hasattr(obj, '_fields'):
1601 # obj is a namedtuple. Recurse into it, but the returned
1602 # object is another namedtuple of the same type. This is
1603 # similar to how other list- or tuple-derived classes are
1604 # treated (see below), but we just need to create them
1605 # differently because a namedtuple's __init__ needs to be
1606 # called differently (see bpo-34363).
1607 return type(obj)(*[_astuple_inner(v, tuple_factory) for v in obj])
1608 elif isinstance(obj, (list, tuple)):
1609 # Assume we can create an object of this type by passing in a
1610 # generator (which is not true for namedtuples, handled
1611 # above).
1612 return type(obj)(_astuple_inner(v, tuple_factory) for v in obj)
1613 elif isinstance(obj, dict):
1614 obj_type = type(obj)
1615 if hasattr(obj_type, 'default_factory'):
1616 # obj is a defaultdict, which has a different constructor from
1617 # dict as it requires the default_factory as its first arg.
1618 result = obj_type(getattr(obj, 'default_factory'))
1619 for k, v in obj.items():
1620 result[_astuple_inner(k, tuple_factory)] = _astuple_inner(v, tuple_factory)
1621 return result
1622 return obj_type((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory))
1623 for k, v in obj.items())
1624 else:
1625 return copy.deepcopy(obj)
1626
1627
1628def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,

Callers 1

astupleFunction · 0.85

Calls 3

_is_dataclass_instanceFunction · 0.85
fieldsFunction · 0.85
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…