diff --git a/news/418.bugfix b/news/418.bugfix new file mode 100644 index 000000000..b846ed1da --- /dev/null +++ b/news/418.bugfix @@ -0,0 +1 @@ +OmegaConf.to_container() raises a ValueError on invalid input diff --git a/omegaconf/omegaconf.py b/omegaconf/omegaconf.py index c085a6617..6eb69939f 100644 --- a/omegaconf/omegaconf.py +++ b/omegaconf/omegaconf.py @@ -514,8 +514,11 @@ def to_container( (DictConfigs backed by a dataclass) :return: A dict or a list representing this config as a primitive container. """ - assert isinstance(cfg, Container) - # noinspection PyProtectedMember + if not OmegaConf.is_config(cfg): + raise ValueError( + f"Input cfg is not an OmegaConf config object ({type_str(type(cfg))})" + ) + return BaseContainer._to_content( cfg, resolve=resolve, diff --git a/tests/test_base_config.py b/tests/test_base_config.py index 8638b8bfa..d8fa32c59 100644 --- a/tests/test_base_config.py +++ b/tests/test_base_config.py @@ -1,4 +1,5 @@ import copy +import re from enum import Enum from typing import Any, Dict, List, Union @@ -210,6 +211,14 @@ def test_to_container(src: Any, expected: Any, expected_with_resolve: Any) -> No assert container == expected_with_resolve +def test_to_container_invalid_input() -> None: + with pytest.raises( + ValueError, + match=re.escape("Input cfg is not an OmegaConf config object (dict)"), + ): + OmegaConf.to_container({}) + + def test_string_interpolation_with_readonly_parent() -> None: cfg = OmegaConf.create({"a": 10, "b": {"c": "hello_${a}"}}) OmegaConf.set_readonly(cfg, True)