Repository: facebookresearch/hydra

Issue:
[Bug] Optional field as config group set to None throws AttributeError
# 🐛 Bug

Setting the type of a field in the root dataclass config to another dataclass throws an AttributeError, setting this type to 

## To reproduce

```
import dataclasses
from typing import(
    List,
    Any,
    Optional
)

import hydra
from hydra.core.config_store import ConfigStore
import omegaconf


@dataclasses.dataclass
class MyModuleConfig:
    some_field: str = dataclasses.field(default='abc')


defaults = [{'module': None}]


@dataclasses.dataclass
class MyModelConfig:
    defaults: List[Any] = dataclasses.field(default_factory=lambda: defaults)
    module: Optional[MyModuleConfig] = dataclasses.field(default=None)


cs = ConfigStore.instance()
cs.store(group='module', name='my_module', node=MyModuleConfig)
cs.store(name='model', node=MyModelConfig)


@hydra.main(config_name='model', config_path='../')
def main(cfg: omegaconf.DictConfig) -> None:
    print(cfg.pretty())


if __name__ == '__main__':
    main()
```

This will run without any problems
```shell
python my_app.py
```
While this will throw an exception
```shell
python my_app.py module=my_module
```

** Stack trace/error message **
```
WARNING, failed to detect relevant stack start
Traceback (most recent call last):
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/utils.py", line 198, in run_and_report
    return func()
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/utils.py", line 333, in <lambda>
    overrides=args.overrides,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/hydra.py", line 102, in run
    run_mode=RunMode.RUN,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/hydra.py", line 512, in compose_config
    from_shell=from_shell,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/config_loader_impl.py", line 150, in load_configuration
    from_shell=from_shell,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/config_loader_impl.py", line 251, in _load_configuration
    run_mode=run_mode,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/config_loader_impl.py", line 769, in _merge_defaults_into_config
    hydra_cfg = merge_defaults_list_into_config(hydra_cfg, user_list)
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/config_loader_impl.py", line 747, in merge_defaults_list_into_config
    package_override=default1.package,
  File "/home/as/anaconda3/lib/python3.7/site-packages/hydra/_internal/config_loader_impl.py", line 686, in _merge_config
    ret = OmegaConf.merge(cfg, loaded_cfg)
AttributeError: 'NoneType' object has no attribute 'get'
	full_key:
	reference_type=Optional[MyModelConfig]
	object_type=MyModelConfig

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.
```

## Expected Behavior
It should set the `module` field of the root config to `MyModuleConfig`

## System information
- **Hydra Version** :  1.0.0rc2
- **Python version** : 3.7.4
- **Virtual environment type and version** : Conda
- **Operating system** :  Ubuntu 18.04

## Additional context
Add any other context about the problem here.



Discussion:
### workaround
Change
```python
@dataclasses.dataclass
class MyModelConfig:
    defaults: List[Any] = dataclasses.field(default_factory=lambda: defaults)
    module: Optional[MyModuleConfig] = dataclasses.field(default=None)
```
to
```python
@dataclasses.dataclass
class MyModelConfig:
    module: Optional[MyModuleConfig]
    defaults: List[Any] = dataclasses.field(default_factory=lambda: defaults)
```

EDIT: fixed syntax highlighting
This is an OmegaConf bug, see the issue there for a minimal repro with OmegaConf.


Resolving patch:
diff --git a/hydra/_internal/utils.py b/hydra/_internal/utils.py
--- a/hydra/_internal/utils.py
+++ b/hydra/_internal/utils.py
@@ -204,8 +204,6 @@ def run_and_report(func: Any) -> Any:
                 sys.stderr.write(str(ex) + os.linesep)
                 if ex.__cause__ is not None:
                     sys.stderr.write(str(ex.__cause__) + os.linesep)
-
-                sys.stderr.write(os.linesep)
             else:
                 # Custom printing that strips the Hydra related stack frames from the top
                 # And any omegaconf frames from the bottom.
@@ -223,9 +221,12 @@ def run_and_report(func: Any) -> Any:
                     search_max = search_max - 1
                     if inspect.getframeinfo(frame).function == "run_job":
                         break
+
                 if search_max == 0 or tb is None:
-                    sys.stderr.write("WARNING, failed to detect relevant stack start\n")
-                    tb = ex.__traceback__
+                    # could not detect run_job, probably a runtime exception before we got there.
+                    # do not sanitize the stack trace.
+                    print_exc()
+                    sys.exit(1)
 
                 # strip OmegaConf frames from bottom of stack
                 end = tb
@@ -265,7 +266,7 @@ class FakeTracebackType:
 
                 print_exception(etype=None, value=ex, tb=final_tb)  # type: ignore
             sys.stderr.write(
-                "Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.\n"
+                "\nSet the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.\n"
             )
         sys.exit(1)
 


Test patch: