hydra: [Bug] Impossible to instantiate nested dataclasses
π Bug
Description
I want to instantiate recursively nested dataclass instances.
Because OmegaConf considers dataclasses instances to be configs, when calling _get_kwargs
during the instantiate
call, the dataclass instance is converted back to a config because parameters are added to a OmegaConf list / dict.
Checklist
- I checked on the latest version of Hydra
- I created a minimal repro
To reproduce
In a script my_app.py
"""Test hydra dataclass instantiation."""
from dataclasses import dataclass
from omegaconf import OmegaConf
from hydra.utils import instantiate
@dataclass
class A:
x: int
@dataclass
class B:
a: A
conf = {
"_target_": "__main__.B",
"a": {
"_target_": "__main__.A",
"x": 1
}
}
parsed = OmegaConf.create(conf)
instance = instantiate(parsed)
print(instance)
run python my_app.py
.
You get
B(a={'x': 1})
Expected Behavior
I expect the result to be B(a=A(x=1))
.
It seems strange to me that the config schema interferes with the instantiation.
I work with a codebase where a bunch of classes are dataclasses and using hydra for configuration would be a plus.
System information
- Hydra Version : master
- Python version : 3.6
- Virtual environment type and version : instantiated with
python -m venv
- Operating system : macOS 11.0.1
Additional context
Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 22 (1 by maintainers)
Ok, that makes sense.
It would indeed introduce unnecessary confusion and complexity.
Letβs try to do the boxing / unboxing trick on the hydra side.
I donβt like 2. Feels like should be able to differentiate instantiated dataclasses versus dataclasses passed in as config. For someone looking at OmegaConf alone (which is lower level than Hydra and does not know about it) - the change there makes no sense at all.