Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting non-dictconfig attributes #480

Closed
carmocca opened this issue Jan 19, 2021 · 1 comment
Closed

Setting non-dictconfig attributes #480

carmocca opened this issue Jan 19, 2021 · 1 comment

Comments

@carmocca
Copy link

carmocca commented Jan 19, 2021

Hi @omry! First of all, thanks for the work you've put into this library!

I was wondering if there is any way to set a DictConfig attribute to a dataclass, without it getting converted into a DictConfig

from dataclasses import dataclass
from omegaconf import OmegaConf

@dataclass
class Foo:
    bar: int = 1

# baseline
x = OmegaConf.create({"foo": {"bar": 1}})
print(type(x["foo"]), x)

# assignment
x["foo"] = Foo()
print(type(x["foo"]), x)

# update
OmegaConf.update(x, "foo", Foo())
print(type(x["foo"]), x)

# creation
x = OmegaConf.create({"foo": Foo()})
print(type(x["foo"]), x)

All of these print the same output and convert Foo() into {bar: 1}

My use case is wanting to parse some of the DictConfig attributes into dataclasses, while keeping others as DictConfigs.

Thank you!

@omry
Copy link
Owner

omry commented Jan 19, 2021

Hi @carmocca!

No, this is a fundamental design choice.

The dataclass objects are assumed to be Structured Configs and are converted to a type safe DictConfig.
You can access the underlying type with OmegaConf.get_type(cfg), and in principle you can instantiate the dataclass again from the data in the config.
This is not implemented now and has a large surface area. I am open to a pull request to add this functionality.

See my comment on #472.

Closing as a dup of #472 (but feel free to continue the discussion here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants