init_takes_fields

init_takes_fields#

treescope.dataclass_util.init_takes_fields(cls: type[Any]) bool[source]#

Returns True if cls.__init__ takes exactly one argument per field.

This is a heuristic for determining whether this dataclass can be rebuilt from its attributes using a simple repr-like format (e.g. Foo(bar=1, baz=2)) or whether safely rebuilding it requires using dataclass_from_attributes() above. This is used during pretty-printing to determine whether to switch to a more verbose form when a round-trippable representation is requested.

Note that it’s technically possible to override __init__ so that it takes the fields as attributes and then modifies them; it’s not really possible to check for this, so we just check that the signature looks correct.

Parameters:

cls – The dataclass to check.