Forward GitHub issue: python/cpython#118974
Feature or enhancement
Proposal:
typing.dataclass_transform
(PEP 681 β Data Class Transforms) allows users define their own dataclass
decorator that can be recognized by the type checker.
Here is a real-world example use case:
Also, dataclasses.asdict
and dataclasses.astuple
allow users pass an extra argument for the factory of the returned instance.
However, the make_dataclass
function does not support third-party dataclass
factory (e.g., flax.struct.dataclass
):
It can only apply dataclasses.dataclass
(see the return
statement above).
This feature request issue will discuss the possibility of adding a new dataclass_factory
argument to the dataclasses.make_dataclass
to support third-party dataclasss transformation, similar to dict_factory
for dataclasses.asdict
.
# dataclasses.py
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
repr=True, eq=True, order=False, unsafe_hash=False,
frozen=False, match_args=True, kw_only=False, slots=False,
weakref_slot=False, module=None,
dataclass_factory=dataclass):
...
# Apply the normal decorator.
return dataclass_factory(cls, init=init, repr=repr, eq=eq, order=order,
unsafe_hash=unsafe_hash, frozen=frozen,
match_args=match_args, kw_only=kw_only, slots=slots,
weakref_slot=weakref_slot)