from dataclasses import dataclass
from serde.yaml import to_yaml
@dataclass
class Foo:
a: int
@dataclass
class Bar:
b: int
@dataclass
class ContainerUnion:
data: Foo | Bar
c = ContainerUnion(Foo(1))
print(to_yaml(c)) # ok
type FooBar = Foo | Bar
@dataclass
class ContainerType:
data: FooBar
c = ContainerType(Foo(1))
print(to_yaml(c))
# serde.compat.SerdeError: Unsupported type: Foo
Repro (requires Python >3.12):
https://typing.readthedocs.io/en/latest/spec/aliases.html#type-statement