Skip to content

Commit

Permalink
OmegaConf.to_container() : proper error on invalid input
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Feb 3, 2021
1 parent 4e13e90 commit bc8a406
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/418.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OmegaConf.to_container() raises a ValueError on invalid input
7 changes: 5 additions & 2 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import re
from enum import Enum
from typing import Any, Dict, List, Union

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bc8a406

Please sign in to comment.