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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Bug for overriding to sub-structured config #1515

Closed
2 tasks done
strx2322 opened this issue Mar 29, 2021 · 1 comment 路 Fixed by #1549
Closed
2 tasks done

[Bug] Bug for overriding to sub-structured config #1515

strx2322 opened this issue Mar 29, 2021 · 1 comment 路 Fixed by #1549
Labels
bug Something isn't working
Milestone

Comments

@strx2322
Copy link

strx2322 commented Mar 29, 2021

馃悰 Bug

Description

Overriding variable to sub-structured config fails when using CLI

Checklist

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

To reproduce

** Minimal Code/Config snippet to reproduce **

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


@dataclass
class A:
    i: int = 1


@dataclass
class Config:
    a: A = A()


@hydra.main(config_name="config")
def hydra_main(cfg: Config):
    print(cfg)


if __name__ == '__main__':
    cs = ConfigStore.instance()
    cs.store(name='config', node=Config)

    hydra_main()

** Stack trace/error message **

$ python hydra.py +a.j=2
Error merging override +a.j=2
Key 'j' not in 'A'
	full_key: a.j
	reference_type=A
	object_type=A

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

Expected Behavior

{'a': {'i': 1, 'j': 2}}

System information

  • Hydra Version : 1.0.6, 1.1.0 dev5
  • Python version : 3.7.9
  • Virtual environment type and version : pyenv, vent
  • Operating system : Mac, Ubuntu
@strx2322 strx2322 added the bug Something isn't working label Mar 29, 2021
@strx2322 strx2322 changed the title [Bug] [Bug] Bug for overriding to sub-structured config Mar 29, 2021
strx2322 added a commit to strx2322/hydra that referenced this issue Mar 29, 2021
Overriding variable to sub-structured config fails when using CLI
@omry
Copy link
Collaborator

omry commented Mar 29, 2021

Thanks for reporting.

You can enable adding keys to a Structured Config by extending Dict:

from dataclasses import dataclass,field
import hydra
from hydra.core.config_store import ConfigStore
from typing import Dict, Any

@dataclass
class A(Dict[str, Any]):
    i: int = 1


@dataclass
class Config:
    a: A = field(default_factory=lambda: A())


@hydra.main(config_name="config")
def hydra_main(cfg: Config):
    print(cfg)


if __name__ == '__main__':
    cs = ConfigStore.instance()
    cs.store(name='config', node=Config)

    hydra_main()

Output:

$ python 1.py  a.x=10
Could not override 'a.x'.
To append to your config use +a.x=10
Key 'x' is not in struct
    full_key: a.x
    reference_type=Dict[str, Any]
    object_type=A

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

$ python 1.py  +a.x=10
{'a': {'i': 1, 'x': 10}}

About your PR:
I will want to take a closer look at this. not sure I want it fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants