# Buggy code
def run_and_report(func: Any) -> Any:
    try:
        return func()
    except Exception as ex:
        if "HYDRA_FULL_ERROR" in os.environ and os.environ["HYDRA_FULL_ERROR"] == "1":
            print_exc()
        else:
            if isinstance(ex, ConfigCompositionException):
                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.
                # It is possible to add additional libraries to sanitize from the bottom later,
                # maybe even make it configurable.
                tb: Any = ex.__traceback__
                search_max = 10
                # strip Hydra frames from start of stack
                # will strip until it hits run_job()
                while search_max > 0:
                    if tb is None:
                        break
                    frame = tb.tb_frame
                    tb = tb.tb_next
                    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__

                # strip OmegaConf frames from bottom of stack
                end = tb
                num_frames = 0
                while end is not None:
                    frame = end.tb_frame
                    mdl = inspect.getmodule(frame)
                    assert mdl is not None
                    name = mdl.__name__
                    if name.startswith("omegaconf."):
                        break
                    end = end.tb_next
                    num_frames = num_frames + 1

                @dataclass
                class FakeTracebackType:
                    tb_next: Any = None  # Optional[FakeTracebackType]
                    tb_frame: Optional[FrameType] = None
                    tb_lasti: Optional[int] = None
                    tb_lineno: Optional[int] = None

                iter_tb = tb
                final_tb = FakeTracebackType()
                cur = final_tb
                added = 0
                while True:
                    cur.tb_lasti = iter_tb.tb_lasti
                    cur.tb_lineno = iter_tb.tb_lineno
                    cur.tb_frame = iter_tb.tb_frame

                    if added == num_frames - 1:
                        break
                    added = added + 1
                    cur.tb_next = FakeTracebackType()
                    cur = cur.tb_next
                    iter_tb = iter_tb.tb_next

                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"
            )
        sys.exit(1)


# Traceback (AttributeError)
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.

# Fixed code
def run_and_report(func: Any) -> Any:
    try:
        return func()
    except Exception as ex:
        if "HYDRA_FULL_ERROR" in os.environ and os.environ["HYDRA_FULL_ERROR"] == "1":
            print_exc()
        else:
            if isinstance(ex, ConfigCompositionException):
                sys.stderr.write(str(ex) + os.linesep)
                if ex.__cause__ is not None:
                    sys.stderr.write(str(ex.__cause__) + os.linesep)
            else:
                # Custom printing that strips the Hydra related stack frames from the top
                # And any omegaconf frames from the bottom.
                # It is possible to add additional libraries to sanitize from the bottom later,
                # maybe even make it configurable.
                tb: Any = ex.__traceback__
                search_max = 10
                # strip Hydra frames from start of stack
                # will strip until it hits run_job()
                while search_max > 0:
                    if tb is None:
                        break
                    frame = tb.tb_frame
                    tb = tb.tb_next
                    search_max = search_max - 1
                    if inspect.getframeinfo(frame).function == "run_job":
                        break

                if search_max == 0 or tb is None:
                    # 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
                num_frames = 0
                while end is not None:
                    frame = end.tb_frame
                    mdl = inspect.getmodule(frame)
                    assert mdl is not None
                    name = mdl.__name__
                    if name.startswith("omegaconf."):
                        break
                    end = end.tb_next
                    num_frames = num_frames + 1

                @dataclass
                class FakeTracebackType:
                    tb_next: Any = None  # Optional[FakeTracebackType]
                    tb_frame: Optional[FrameType] = None
                    tb_lasti: Optional[int] = None
                    tb_lineno: Optional[int] = None

                iter_tb = tb
                final_tb = FakeTracebackType()
                cur = final_tb
                added = 0
                while True:
                    cur.tb_lasti = iter_tb.tb_lasti
                    cur.tb_lineno = iter_tb.tb_lineno
                    cur.tb_frame = iter_tb.tb_frame

                    if added == num_frames - 1:
                        break
                    added = added + 1
                    cur.tb_next = FakeTracebackType()
                    cur = cur.tb_next
                    iter_tb = iter_tb.tb_next

                print_exception(etype=None, value=ex, tb=final_tb)  # type: ignore
            sys.stderr.write(
                "\nSet the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.\n"
            )
        sys.exit(1)