Skip to content
New issue

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

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

Already on GitHub? # to your account

Error merging into Optional[List[Any]] = None #336

Closed
ananthsub opened this issue Aug 21, 2020 · 1 comment · Fixed by #337
Closed

Error merging into Optional[List[Any]] = None #336

ananthsub opened this issue Aug 21, 2020 · 1 comment · Fixed by #337
Labels
bug Something isn't working

Comments

@ananthsub
Copy link

ananthsub commented Aug 21, 2020

This is my dataclass and yaml file:

@dataclass
class MultiStepLR:
    _target_: str = "torch.optim.lr_scheduler.MultiStepLR"
    milestones: Optional[List[int]] = None
    gamma: float = MISSING
    last_epoch: int = -1

Here's the yaml content of a file called multi_step.yaml

# @package _group_
milestones:
  - 30
  - 60
  - 80
gamma: 0.1
last_epoch: -1

This is the registation with ConfigStore:

ConfigStore.instance().store(group="lr_scheduler", name="multi_step", node=MultiStepLR)

The registration leads to this runtime error

Traceback (most recent call last):
  File "hydra/_internal/config_load
er_impl.py", line 610, in _load_config_impl
    merged = OmegaConf.merge(schema.config, ret.config)
  File "omegaconf/omegaconf.py", li
ne 316, in merge
    target.merge_with(*others[1:])
  File "omegaconf/basecontainer.py"
, line 324, in merge_with
    self._format_and_raise(key=None, value=None, cause=e)
  File "omegaconf/base.py", line 10
1, in _format_and_raise
    type_override=type_override,
  File "omegaconf/_utils.py", line
675, in format_and_raise
    _raise(ex, cause)
  File "omegaconf/_utils.py", line
591, in _raise
    raise ex  # set end OC_CAUSE=1 for full backtrace
omegaconf.errors.ValidationError: Invalid value assigned : _GenericAlias is not a subclass of ListConfig or list.
        full_key:
        reference_type=Optional[Dict[Union[str, Enum], Any]]
        object_type=dict

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "hydra/_internal/utils.py",
line 203, in run_and_report
    return func()
  File "hydra/_internal/utils.py",
line 355, in <lambda>
    overrides=args.overrides,
  File "hydra/_internal/hydra.py",
line 102, in run
    run_mode=RunMode.RUN,
  File "hydra/_internal/hydra.py",
line 512, in compose_config
    from_shell=from_shell,
  File "hydra/_internal/config_loader_impl.py", line 153, in load_configuration
    from_shell=from_shell,
  File "hydra/_internal/config_loader_impl.py", line 256, in _load_configuration
    run_mode=run_mode,
  File "hydra/_internal/config_loader_impl.py", line 797, in _merge_defaults_into_config
    hydra_cfg = merge_defaults_list_into_config(hydra_cfg, user_list)
  File "hydra/_internal/config_loader_impl.py", line 775, in merge_defaults_list_into_config
    package_override=default1.package,
  File "hydra/_internal/config_loader_impl.py", line 690, in _merge_config
    package_override=package_override,
  File "hydra/_internal/config_loader_impl.py", line 622, in _load_config_impl
    ) from e
hydra.errors.ConfigCompositionException: Error merging 'lr_scheduler/multi_step' with schema

To reproduce in IPython

from dataclasses import dataclass
from omegaconf import MISSING
from typing import Any, Optional, List
@dataclass
class MultiStepLR:
    target: str = "torch.optim.lr_scheduler.MultiStepLR"
    milestones: Optional[List[int]] = None
    gamma: float = MISSING
    last_epoch: int = -1
from omegaconf import MISSING, OmegaConf
base = OmegaConf.structured(MultiStepLR)
base
yml = OmegaConf.create({"milestones": [1,2,3], "gamma": 0.1, "last_epoch": -1})
res = OmegaConf.merge(base, yml)
@omry omry added the bug Something isn't working label Aug 21, 2020
@omry omry added this to the OmegaConf 2.0.1 milestone Aug 21, 2020
@omry
Copy link
Owner

omry commented Aug 21, 2020

This is introduced in omegaconf 2.0.1rc11.
It used to work before that, but it was never tested probably.

It's probably reasonable to interpret merging into None list or dict as an assignment.

@omry omry changed the title Runtime error with hydra initialization of structured configs Error merging into Optional[List[Any]] = None Aug 21, 2020
@omry omry closed this as completed in #337 Aug 21, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants