Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that allowed containers to be set as a value in ValueNodes #335

Merged
merged 17 commits into from
Aug 29, 2020
Merged
Prev Previous commit
Next Next commit
test anynode test_basic_ops_dict
pereman2 committed Aug 27, 2020

Verified

This commit was signed with the committer’s verified signature.
commit 12849d5c7f78949fc1800d4f995f7016ae0d7b6b
12 changes: 6 additions & 6 deletions omegaconf/errors.py
Original file line number Diff line number Diff line change
@@ -33,12 +33,6 @@ class MissingMandatoryValue(OmegaConfBaseException):
indicate that the value was not set"""


class UnsupportedValueType(OmegaConfBaseException, ValueError):
"""
Thrown when an input value is not of supported type
"""


class KeyValidationError(OmegaConfBaseException, ValueError):
"""
Thrown when an a key of invalid type is used
@@ -51,6 +45,12 @@ class ValidationError(OmegaConfBaseException, ValueError):
"""


class UnsupportedValueType(ValidationError, ValueError):
"""
Thrown when an input value is not of supported type
"""


class ReadonlyConfigError(OmegaConfBaseException):
"""
Thrown when someone tries to modify a frozen config
34 changes: 28 additions & 6 deletions tests/test_basic_ops_dict.py
Original file line number Diff line number Diff line change
@@ -530,17 +530,39 @@ def test_set_with_invalid_key() -> None:
cfg[1] = "a" # type: ignore


def test_set_anynode() -> None:
@pytest.mark.parametrize("value", [1, 3.14, True, None, Enum1.FOO]) # type: ignore
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests should probably be in test_nodes.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this one tests if the node is not replaced. That cannot be done in test_nodes.

Copy link
Contributor Author

@pereman2 pereman2 Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But yeah, I missed adding these testcases in test_valid_inputs in test_nodes

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's okay. Please move the tests to test_nodes.

def test_set_anynode_with_primitive_type(value: Any) -> None:
cfg = OmegaConf.create({"a": 5})
a_before = cfg._get_node("a")
cfg.a = "a"
cfg.a = value
# changing anynode's value with a primitive type should set value
assert id(cfg._get_node("a")) == id(a_before)
a_before2 = cfg._get_node("a")
cfg.a = []
assert cfg.a == value


@pytest.mark.parametrize(
"value, container_type", # type: ignore
[
(ListConfig(content=[1, 2]), ListConfig),
([1, 2], ListConfig),
(DictConfig(content={"foo": "var"}), DictConfig),
({"foo": "var"}, DictConfig),
],
)
def test_set_anynode_with_container(value: Any, container_type: Any) -> None:
cfg = OmegaConf.create({"a": 5})
a_before = cfg._get_node("a")
cfg.a = value
# changing anynode's value with a container should wrap a new node
assert id(cfg._get_node("a")) != id(a_before2)
assert isinstance(cfg.a, ListConfig)
assert id(cfg._get_node("a")) != id(a_before)
assert isinstance(cfg.a, container_type)
assert cfg.a == value


def test_set_anynode_with_illegal_type() -> None:
cfg = OmegaConf.create({"a": 5})
with pytest.raises(ValidationError):
cfg.a = IllegalType()


def test_set_valuenode() -> None: