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.
Feature or enhancement
Proposal:
typing.dataclass_transform
(PEP 681 – Data Class Transforms) allows users define their owndataclass
decorator that can be recognized by the type checker.Here is a real-world example use case:
flax.struct.dataclass
Also,
dataclasses.asdict
anddataclasses.astuple
allow users pass an extra argument for the factory of the returned instance.https://github.com/python/cpython/blob/0fb18b02c8ad56299d6a2910be0bab8ad601ef24/Lib/dataclasses.py#L1299-L1317
https://github.com/python/cpython/blob/0fb18b02c8ad56299d6a2910be0bab8ad601ef24/Lib/dataclasses.py#L1380-L1397
However, the
make_dataclass
function does not support third-partydataclass
factory (e.g.,flax.struct.dataclass
):https://github.com/python/cpython/blob/0fb18b02c8ad56299d6a2910be0bab8ad601ef24/Lib/dataclasses.py#L1441-L1528
It can only apply
dataclasses.dataclass
(see thereturn
statement above).This feature request issue will discuss the possibility of adding a new
dataclass_factory
argument to thedataclasses.make_dataclass
to support third-party dataclasss transformation, similar todict_factory
fordataclasses.asdict
.Has this already been discussed elsewhere?
https://discuss.python.org/t/add-dataclass-factory-argument-to-dataclasses-make-dataclass-for-custom-dataclass-transformation-support/53188
Links to previous discussion of this feature:
No response