With dataclasses Rich will essentially replace the dataclass __repr__
by inspecting the dataclass fields. It does this so it can know how to expand the dataclass on to multiple lines with indentation.
For example, here is a dataclass
@dataclass
class DC:
foo: str
bar: int
Rich can inspect the fields and generate this:
DC(
foo="hello",
bar=5
)
However Rich will respect a custom rerpr. For example:
@dataclass
class DC:
foo: str
bar: int
def __repr__(self):
return "DC is better than Marvel"
In the above case, Rich will call repr(dc)
rather than inspecting the fields.
So Rich needs to know if that custom __repr__
is present.
The problem is that the cases with or without the custom __repr__
, the dataclass instances both contain a __repr__
. And Rich needs to know if it is the default one generated by the @dataclass
decorator OR one provided by the user.
Since there is no dataclass base class, I can't just compare it to the baseclasses (via __mro__
). In both cases the mro is (DC, object).
Before Python3.10 the __qualname__
attribute was different in dataclass generated reprs, and I could use that. Now in Python3.10 the __qualname__
attribute is identical in both cases.
Have you looked at the standard library implementation for pprint, specifically https://bugs.python.org/issue43080 and https://github.com/python/cpython/pull/24389/files ? That also checks for a custom repr, etc. Other than using "object" as a parameter name 😠, they use
dataclasses.is_dataclass(item)
to see if something is a dataclass, then they check if__wrapped__
is an attribute of the__repr__
, followed by a check for"__create_fn__" in item.__repr__.__wrapped__.__qualname__
.