Skip to content

One should be allowed to re-instantiate a structured object with missing values #1103

@amatsukawa

Description

@amatsukawa

Defining dataclasses to introduce schema seems like a great idea. But, sometimes we have missing values, and there is omegaconf.MISSING to express this. However:

from omegaconf import OmegaConf, MISSING
import omegaconf
from dataclasses import dataclass

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

# If we are allowed to instantiate this
a = OmegaConf.create(a)
print(a)
# ==> {'a': {'x': '???'}}

# Then we should be allowed to instantiate it again:
OmegaConf.to_container(
    a, 
    throw_on_missing=False, 
    structured_config_mode = omegaconf.SCMode.INSTANTIATE
)
# ...
# MissingMandatoryValue: Structured config of type `A` has missing mandatory value: x
#     full_key: x
#     object_type=A

The use case is we may have partially filled objects that we want to manipulate, construct new configs out of, etc.

The problem is that this line forgets .to_container's throw_on_missing argument:

return conf._to_object()

and inside this function, it could be allowed here:

if node._is_missing():

It's currently hard for me logistically to contribute the simple PR, but hopefully I have identified the changes necessary sufficiently if the maintainers agree this should be supported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions