Open denialofsandwich opened 8 months ago
I found another few examples. Here with a dict[str, list[union]]
type:
from dataclasses import dataclass, field
from typing import Union
from omegaconf import OmegaConf
@dataclass
# Default is z.a.b = 1
class HasUnion:
z: dict[str, list[Union[str, int]]] = field(
default_factory=lambda: {"a": [1,2]}
)
# z.a = [1] (This works)
# z.c = [1] (This doesn't work)
cfg = OmegaConf.merge(
OmegaConf.structured(HasUnion),
OmegaConf.create("""
z:
c:
- 1
""")
)
And here with a dict[str, list[dataclass]]
type. The validation doesn't work, if the parent key wasn't part of the default value before:
from dataclasses import dataclass, field
from omegaconf import OmegaConf
@dataclass
class Foo:
exist1: int
exist2: str
@dataclass
class BaseStructure:
z: dict[str, list[Foo]] = field(
default_factory=lambda: {"a": [Foo(1, "lol")]}
)
cfg = OmegaConf.merge(
OmegaConf.structured(BaseStructure),
OmegaConf.create("""
z:
c:
- exist1: 1
dontexist2: lol
""")
)
EDIT: I accidentally clicked on close, sry.
Thanks for the bug report, @denialofsandwich.
Describe the bug The validation of unions, which are nested in a dict seems to be broken and throws a ValidationError during a merge, even if the types should be correct.
To Reproduce The last line is throwing the ValidationError:
This is the exception thwown at the last line:
Expected behavior I expect
z.c.b = 1
not to fail, since1
is a valid type ofUnion[str, int]
and if the Union is not nested inside a dict.Additional context