This patch allows for TypeOverrides in SerializableDataClassFields to have mutable default values.
Python's dataclasses do not allow for mutable default values, otherwise instances that use the default will all point to the same object:
py
dataclass
class MyClass:
myField: List[str] = []
The above will throw an exception (even without instancing the class)
The same exception will be thrown when the type is overridden in Carica:
py
dataclass
class MyClass(SerializableDataClass):
myField: List[str] = TypeOverride(List[int], [])
To solve this issue, the python recommended pattern is now supported:
py
from dataclasses import field
dataclass
class MyClass(SerializableDataClass):
myField: List[str] = field(default_factory=TypeOverride(List[int], list))
no more errors :)
The factory (given as the second argument to TypeOverride here) can be any callable, as long as it returns your hinted type. lambdas work great here.