|
| 1 | +"""Utilities for :mod:`.dataclasses`. |
| 2 | +
|
| 3 | +This module currently backports one function, :func:`asdict`, from Python 3.13.0, in |
| 4 | +order to avoid https://github.com/python/cpython/issues/79721. This issue specifically |
| 5 | +occurs with :attr:`.ScenarioInfo.set`, which is of class :class:`defaultdict`. The |
| 6 | +backported function **should** be used when (a) Python 3.11 or earlier is in use and (b) |
| 7 | +ScenarioInfo is handled directly or indirectly. |
| 8 | +""" |
| 9 | +# NB Comments are deleted |
| 10 | + |
| 11 | +import copy |
| 12 | +import types |
| 13 | +from dataclasses import fields |
| 14 | + |
| 15 | +__all__ = [ |
| 16 | + "asdict", |
| 17 | +] |
| 18 | + |
| 19 | +_ATOMIC_TYPES = frozenset( |
| 20 | + { |
| 21 | + types.NoneType, |
| 22 | + bool, |
| 23 | + int, |
| 24 | + float, |
| 25 | + str, |
| 26 | + complex, |
| 27 | + bytes, |
| 28 | + types.EllipsisType, |
| 29 | + types.NotImplementedType, |
| 30 | + types.CodeType, |
| 31 | + types.BuiltinFunctionType, |
| 32 | + types.FunctionType, |
| 33 | + type, |
| 34 | + range, |
| 35 | + property, |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +_FIELDS = "__dataclass_fields__" |
| 40 | + |
| 41 | + |
| 42 | +def _is_dataclass_instance(obj): |
| 43 | + return hasattr(type(obj), _FIELDS) |
| 44 | + |
| 45 | + |
| 46 | +def asdict(obj, *, dict_factory=dict): |
| 47 | + if not _is_dataclass_instance(obj): |
| 48 | + raise TypeError("asdict() should be called on dataclass instances") |
| 49 | + return _asdict_inner(obj, dict_factory) |
| 50 | + |
| 51 | + |
| 52 | +def _asdict_inner(obj, dict_factory): # noqa: C901 |
| 53 | + obj_type = type(obj) |
| 54 | + if obj_type in _ATOMIC_TYPES: |
| 55 | + return obj |
| 56 | + elif hasattr(obj_type, _FIELDS): |
| 57 | + if dict_factory is dict: |
| 58 | + return { |
| 59 | + f.name: _asdict_inner(getattr(obj, f.name), dict) for f in fields(obj) |
| 60 | + } |
| 61 | + else: |
| 62 | + return dict_factory( |
| 63 | + [ |
| 64 | + (f.name, _asdict_inner(getattr(obj, f.name), dict_factory)) |
| 65 | + for f in fields(obj) |
| 66 | + ] |
| 67 | + ) |
| 68 | + elif obj_type is list: |
| 69 | + return [_asdict_inner(v, dict_factory) for v in obj] |
| 70 | + elif obj_type is dict: |
| 71 | + return { |
| 72 | + _asdict_inner(k, dict_factory): _asdict_inner(v, dict_factory) |
| 73 | + for k, v in obj.items() |
| 74 | + } |
| 75 | + elif obj_type is tuple: |
| 76 | + return tuple([_asdict_inner(v, dict_factory) for v in obj]) |
| 77 | + elif issubclass(obj_type, tuple): |
| 78 | + if hasattr(obj, "_fields"): |
| 79 | + return obj_type(*[_asdict_inner(v, dict_factory) for v in obj]) |
| 80 | + else: |
| 81 | + return obj_type(_asdict_inner(v, dict_factory) for v in obj) |
| 82 | + elif issubclass(obj_type, dict): |
| 83 | + if hasattr(obj_type, "default_factory"): |
| 84 | + result = obj_type(obj.default_factory) |
| 85 | + for k, v in obj.items(): |
| 86 | + result[_asdict_inner(k, dict_factory)] = _asdict_inner(v, dict_factory) |
| 87 | + return result |
| 88 | + return obj_type( |
| 89 | + (_asdict_inner(k, dict_factory), _asdict_inner(v, dict_factory)) |
| 90 | + for k, v in obj.items() |
| 91 | + ) |
| 92 | + elif issubclass(obj_type, list): |
| 93 | + return obj_type(_asdict_inner(v, dict_factory) for v in obj) |
| 94 | + else: |
| 95 | + return copy.deepcopy(obj) |
0 commit comments