You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
dict_factory is one way of dropping fields from top-level dict to be returned, but that includes expensive copy.deepcopy computations on fields that will eventually get dropped.
def_asdict_inner(obj, dict_factory, exclude_list=[]):
if_is_dataclass_instance(obj):
result= []
forfinfields(obj):
iff.nameisnotinexclude_list:
value=_asdict_inner(getattr(obj, f.name), dict_factory)
result.append((f.name, value))
returndict_factory(result)
elifisinstance(obj, tuple) andhasattr(obj, '_fields'):
# obj is a namedtuple. Recurse into it, but the returned# object is another namedtuple of the same type. This is# similar to how other list- or tuple-derived classes are# treated (see below), but we just need to create them# differently because a namedtuple's __init__ needs to be# called differently (see bpo-34363).# I'm not using namedtuple's _asdict()# method, because:# - it does not recurse in to the namedtuple fields and# convert them to dicts (using dict_factory).# - I don't actually want to return a dict here. The main# use case here is json.dumps, and it handles converting# namedtuples to lists. Admittedly we're losing some# information here when we produce a json list instead of a# dict. Note that if we returned dicts here instead of# namedtuples, we could no longer call asdict() on a data# structure where a namedtuple was used as a dict key.returntype(obj)(*[_asdict_inner(v, dict_factory) forvinobj])
elifisinstance(obj, (list, tuple)):
# Assume we can create an object of this type by passing in a# generator (which is not true for namedtuples, handled# above).returntype(obj)(_asdict_inner(v, dict_factory) forvinobj)
elifisinstance(obj, dict):
returntype(obj)((_asdict_inner(k, dict_factory),
_asdict_inner(v, dict_factory))
fork, vinobj.items())
else:
returncopy.deepcopy(obj)
The above fix of passing exclude_list on the first call to _asdict_inner will work.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered:
I agree this should be discussed on https://discuss.python.org/c/ideas/6 first. @pranaliyawalkar : I'm going to close this until it's discussed there and a conclusion is reached. If need be, this can be re-opened.
My general stance is that adding as_dict was probably a mistake. There are just too many options on how it could work, and you should probably write your own version to suit your needs.
Bug report
Bug description:
dict_factory is one way of dropping fields from top-level dict to be returned, but that includes expensive copy.deepcopy computations on fields that will eventually get dropped.
The above fix of passing exclude_list on the first call to _asdict_inner will work.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: