omry / omegaconf

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

handle SCMode.INSTANTIATE with throw_on_missing=False #1104

Open Jasha10 opened 1 year ago

Jasha10 commented 1 year ago

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='???')
omry commented 1 year ago

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