omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.94k stars 105 forks source link

structured config types are discarded on list merge #1058

Open raphCode opened 1 year ago

raphCode commented 1 year ago

Describe the bug OmegaConf.merge(config, structured) fails to merge dataclasses contained in a list. This means type information are not present in the resultant config. Related: https://github.com/facebookresearch/hydra/discussions/2586

To Reproduce

from omegaconf import OmegaConf
from attrs import frozen

@frozen
class A:
    num: int

@frozen
class C:
    a: A
    aa: list[A]

c = OmegaConf.structured(C)
c = OmegaConf.merge(dict(a=dict(num=1), aa=[dict(num=2)]), c)
assert OmegaConf.get_type(c.a) == A
assert OmegaConf.get_type(c.aa[0]) == A  # fails

Expected behavior All asserts pass. Note that reversing the merge order to merge(c, dict(...)) correctly propagates the types into the list.

Additional context

Jasha10 commented 1 year ago

Thanks, @raphCode. I agree that this looks like a bug. A few notes:

@frozen class A: num: int

@frozen class C: a: A aa: dict[int, A]

c = OmegaConf.structured(C) c = OmegaConf.merge(dict(a=dict(num=1), aa={0: dict(num=2)}), c) assert OmegaConf.get_type(c.a) == A assert OmegaConf.get_type(c.aa[0]) == A # fails