MCPcopy Index your code
hub / github.com/RustPython/RustPython / _astuple_inner

Function _astuple_inner

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

Source from the content-addressed store, hash-verified

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

Callers 1

astupleFunction · 0.85

Calls 6

_is_dataclass_instanceFunction · 0.85
getattrFunction · 0.85
fieldsFunction · 0.85
isinstanceFunction · 0.85
hasattrFunction · 0.85
itemsMethod · 0.45

Tested by

no test coverage detected