|
|
|
@ -66,6 +66,18 @@ def is_dataclass(obj): |
|
|
|
|
return hasattr(obj, _FIELDS) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _asdictref_inner(obj) -> dict[str, Any] | Any: |
|
|
|
|
if is_dataclass(obj): |
|
|
|
|
ret = {} |
|
|
|
|
for field in getattr(obj, _FIELDS): # similar to dataclasses.fields() |
|
|
|
|
ret[field] = _asdictref_inner(getattr(obj, field)) |
|
|
|
|
return ret |
|
|
|
|
elif isinstance(obj, (tuple, list)): |
|
|
|
|
return type(obj)(_asdictref_inner(v) for v in obj) |
|
|
|
|
else: |
|
|
|
|
return obj |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def asdictref(obj) -> dict[str, Any]: |
|
|
|
|
""" |
|
|
|
|
Similar to dataclasses.asdict without recursive type checking and copy.deepcopy |
|
|
|
@ -74,17 +86,6 @@ def asdictref(obj) -> dict[str, Any]: |
|
|
|
|
if not is_dataclass(obj): |
|
|
|
|
raise TypeError("asdictref() should be called on dataclass instances") |
|
|
|
|
|
|
|
|
|
def _asdictref_inner(obj) -> dict[str, Any] | Any: |
|
|
|
|
if is_dataclass(obj): |
|
|
|
|
ret = {} |
|
|
|
|
for field in getattr(obj, _FIELDS): # similar to dataclasses.fields() |
|
|
|
|
ret[field] = _asdictref_inner(getattr(obj, field)) |
|
|
|
|
return ret |
|
|
|
|
elif isinstance(obj, (tuple, list)): |
|
|
|
|
return type(obj)(_asdictref_inner(v) for v in obj) |
|
|
|
|
else: |
|
|
|
|
return obj |
|
|
|
|
|
|
|
|
|
return _asdictref_inner(obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|