Description
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.
Lines 1299 to 1317 in 0fb18b0
Lines 1380 to 1397 in 0fb18b0
However, the make_dataclass
function does not support third-party dataclass
factory (e.g., flax.struct.dataclass
):
Lines 1441 to 1528 in 0fb18b0
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)
Has this already been discussed elsewhere?
Links to previous discussion of this feature:
No response