Skip to content

Patch v.1.3.2

Compare
Choose a tag to compare
@Trimatix Trimatix released this 20 Jun 22:23
· 18 commits to main since this release
8ab95db

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:

@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:

@dataclass
class MyClass(SerializableDataClass):
    myField: List[str] = TypeOverride(List[int], [])

To solve this issue, the python recommended pattern is now supported:

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.