Remove attributes from asdict method when dataclass field(repr=False)

Hello, as I was trying to not use some attributes from a dataclass when creating a pandas.DataFrame, I noticed that even though the specific attribute was set as field(repr=False), it persisted in the data frame.

It would be nice to “hide” those attributes in the dict representation.

I did some tests changing two lines in the _asdict_inner function and it worked nicely.

def _asdict_inner(obj, dict_factory):
    if _is_dataclass_instance(obj):
        result = []
        for f in fields(obj):
            value = _asdict_inner(getattr(obj,, dict_factory)
            if not f.repr:
            result.append((, value))
        return dict_factory(result)

So, I don’t fully know the implications of this modification, but it would be nice to also remove a attribute from dict representation when repr=False, or even create a field where you choose not to pass along these attributes.

Controlling this with repr would be a breaking change, and we can’t do that. It would need to be a new attribute that controls it. Or maybe an optional param to addiction that takes a list of field names to exclude.

But I’m not convinced either of those is worth doing.

1 Like

I should also add that I think asdict was a mistake, and I should have left it to everyone to create their own version. There are just too many combinations of features people want: deep copy or not, what objects to recurse in to, and now what fields to include. I’m sure there are more things as well.