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

Updating values inside optional structured config container with default None is not possible #1038

Closed sstieger closed 1 year ago

sstieger commented 1 year ago

Describe the bug When creating and extending a structured config with an attribute opt: Optional[T] = None (where T is a container type), it is not possible to set any keys in opt using OmegaConf.update(), as trying to do so raises the error "Unexpected type for root" here (OmegaConf expects an existing Container, but receives None, our default value).

To Reproduce

from dataclasses import dataclass
from omegaconf import OmegaConf
from typing import Optional

@dataclass
class SubConfig:
    val: int = 0

@dataclass
class Config:
    opt: Optional[SubConfig] = None

if __name__ == "__main__":
    conf = OmegaConf.structured(Config)
    OmegaConf.update(conf, "opt.val", 42, merge=True)

Expected behavior The structured config attribute opt should be initialized as a container, followed by the requested update.

Additional context

sstieger commented 1 year ago

@Jasha10 I think this is a duplicate of your #1035, sorry - I'll close this :)