I'm trying to use a dataclass as a (more strongly typed) dictionary in my application, and found this strange behavior when using a custom type within the dataclass. I'm using Python 3.11.3 on Windows.
from dataclasses import dataclass, asdictclass CustomFloatList(list): def __init__(self, args): for i, arg in enumerate(args): assert isinstance(arg, float), f"Expected index {i} to be a float, but it's a {type(arg).__name__}" super().__init__(args) @classmethod def from_list(cls, l: list[float]): return cls(l)@dataclassclass Poc: x: CustomFloatListp = Poc(x=CustomFloatList.from_list([3.0]))print(p) # Prints Poc(x=[3.0])print(p.x) # Prints [3.0]print(asdict(p)) # Prints {'x': []}
This does not occur if I use a regular list[float], but I'm using a custom class here to enforce some runtime constraints.
How do I do this correctly?
I'm open to just using .__dict__
directly, but I thought asdict()
was the more "official" way to handle this
A simple modification makes the code behave as expected, but is slightly less efficient:
from dataclasses import dataclass, asdictclass CustomFloatList(list): def __init__(self, args): dup_args = list(args) for i, arg in enumerate(dup_args): assert isinstance(arg, float), f"Expected index {i} to be a float, but it's a {type(arg).__name__}" super().__init__(dup_args) @classmethod def from_list(cls, l: list[float]): return cls(l)@dataclassclass Poc: x: CustomFloatListp = Poc(x=CustomFloatList.from_list([3.0]))print(p)print(p.x)print(asdict(p))