Open peterschutt opened 4 months ago
Possible implementation:
def __repr__(self) -> str:
"""Exclude fields with values equal to the field default."""
kv_pairs = (f"{f.name}={getattr(self, f.name)!r}" for f in fields(self) if getattr(self, f.name) != f.default)
return f"{self.__class__.__name__}({', '.join(kv_pairs)})"
Summary
We have some dataclasses with a lot of fields, and the reprs can be a bit overwhelming, especially when included in logs/warnings/exceptions.
Request is for reprs to exclude fields where their value is
==
the field default.KwargDefinition
(and subtypes) and the OpenAPI spec models are examples of types that would benefit from this.Basic Example
vs
Drawbacks and Impact
Pros: easier to parse and reason about. Cons: maybe some users prefer the verbosity.
Unresolved questions
Could changing the reprs of these things be considered breaking??