Skip to content

[Bug] Schema validation for recursively instantiated targets failing #2575

Description

@classner

🐛 Bug

Description

I would like Hydra to instantiate a nested target which comes from interpolation with schema validation (following the example from #1420). Example:

config_test.yaml

defaults:
  - config_test_schema
  - _self_

child:
  _target_: config_test.Child
parent:
  _target_: config_test.Parent
  child: ${child}

config_test.py

import hydra
from hydra.utils import instantiate
from dataclasses import dataclass
from hydra.core.config_store import ConfigStore


@dataclass
class ConfigChild:
    _target_: str = "Child"


@dataclass
class ConfigParent:
    _target_: str = "Parent"
    child: ConfigChild = ConfigChild()


@dataclass
class TestConfig:
    child: ConfigChild = ConfigChild()
    parent: ConfigParent = ConfigParent()


cs = ConfigStore.instance()
cs.store(name="config_test_schema", node=TestConfig)


class Child:
    pass


class Parent:
    def __init__(self, child):
        self.child = child


@hydra.main(config_path=".", version_base=None, config_name="config_test")
def run(cfg):
    parent = instantiate(cfg.parent)
    print(parent.child)


if __name__ == "__main__":
    run()

This runs successfully if I remove the line - config_test_schema from the defaults in config_test.yaml. However, once the line is there running fails with

In 'config_test': ValidationError raised while composing config:
Merge error: str is not a subclass of ConfigChild. value: ${child}
    full_key: 
    object_type=TestConfig

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro (See this for tips).

Expected Behavior

Instantiation of parent and child successful.

System information

  • Hydra Version : 1.3.1
  • Python version : 3.8.1
  • Virtual environment type and version : venv 2.3.12
  • Operating system : Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions