omegaconf icon indicating copy to clipboard operation
omegaconf copied to clipboard

handle SCMode.INSTANTIATE with throw_on_missing=False

Open Jasha10 opened this issue 2 years ago • 1 comments

This PR enables OmegaConf.to_container(..., throw_on_missing=False) to work for structured configs when structured_config_mode=SCMode.INSTANTIATE. Closes #1103.

In addition, this PR moves MISSING from omegaconf.py into base.py to avoid a circular import issue.

Here is an example of the behavior enabled by this PR:

# repro.py
from dataclasses import dataclass
from enum import Enum
import omegaconf
from omegaconf import OmegaConf

@dataclass(frozen=True)
class A:
    x: int

a = OmegaConf.create(A)
container = OmegaConf.to_container(
    a,
    throw_on_missing=False,
    structured_config_mode=omegaconf.SCMode.INSTANTIATE,
)

assert isinstance(container, A)
print(container)

BEFORE:

$ python repro.py
Traceback (most recent call last):
  File "/home/homestar/dev/omegaconf/repro.py", line 12, in <module>
    cfg = OmegaConf.to_container(
...
omegaconf.errors.MissingMandatoryValue: Structured config of type `A` has missing mandatory value: x
    full_key: x
    object_type=A

AFTER:

$ python repro.py
A(x='???')

Jasha10 avatar Jul 16 '23 07:07 Jasha10

Looks good overall and I agree that it's a better behavior. I think there is still some circular dependency.

omry avatar Jul 21 '23 08:07 omry