diff --git a/news/74.feature b/news/74.feature new file mode 100644 index 000000000..e7e8ea21a --- /dev/null +++ b/news/74.feature @@ -0,0 +1 @@ +Allow assignment of a tuple value into a Config diff --git a/omegaconf/config.py b/omegaconf/config.py index ccf9771de..1dcb8ddea 100644 --- a/omegaconf/config.py +++ b/omegaconf/config.py @@ -478,7 +478,7 @@ def _prepare_value_to_add(self, key, value): if isinstance(value, Config): value = value.to_container() - if isinstance(value, dict) or isinstance(value, list): + if isinstance(value, (dict, list, tuple)): from omegaconf import OmegaConf value = OmegaConf.create(value, parent=self) diff --git a/tests/test_basic_ops_list.py b/tests/test_basic_ops_list.py index d2e5a863c..9060228b4 100644 --- a/tests/test_basic_ops_list.py +++ b/tests/test_basic_ops_list.py @@ -107,18 +107,19 @@ def test_list_len(): assert len(c) == 2 -def test_assign_list_in_list(): - c = OmegaConf.create([10, 11]) - c[0] = ["a", "b"] - assert c == [["a", "b"], 11] - assert isinstance(c[0], ListConfig) - - -def test_assign_dict_in_list(): - c = OmegaConf.create([None]) - c[0] = dict(foo="bar") - assert c[0] == dict(foo="bar") - assert isinstance(c[0], DictConfig) +@pytest.mark.parametrize( + "parent, index, value, expected", + [ + ([10, 11], 0, ["a", "b"], [["a", "b"], 11]), + ([None], 0, {"foo": "bar"}, [{"foo": "bar"}]), + ({}, "foo", ["a", "b"], {"foo": ["a", "b"]}), + ({}, "foo", ("a", "b"), {"foo": ["a", "b"]}), + ], +) +def test_assign(parent, index, value, expected): + c = OmegaConf.create(parent) + c[index] = value + assert c == expected def test_nested_list_assign_illegal_value(): @@ -127,13 +128,6 @@ def test_nested_list_assign_illegal_value(): c.a[0] = IllegalType() -def test_assign_list_in_dict(): - c = OmegaConf.create(dict()) - c.foo = ["a", "b"] - assert c == dict(foo=["a", "b"]) - assert isinstance(c.foo, ListConfig) - - def test_list_append(): c = OmegaConf.create([]) c.append(1)