Skip to content

Commit

Permalink
fixed bug that caused lists to not be wrapped in ListConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Dec 24, 2019
1 parent cc37fcd commit 16286d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
19 changes: 11 additions & 8 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ def get_yaml_loader():
return loader


def _is_primitive_type(type_):
if not isinstance(type_, type):
type_ = type(type_)

return issubclass(type_, Enum) or type_ in (int, float, bool, str, type(None))


def _valid_value_annotation_type(type_):
return (
(isinstance(type_, type) and issubclass(type_, Enum))
or is_structured_config(type_)
or type_ in (int, float, bool, str, Any)
)
return type_ is Any or _is_primitive_type(type_) or is_structured_config(type_)


def _valid_input_value_type(value):
Expand Down Expand Up @@ -127,9 +130,9 @@ def _maybe_wrap(annotated_type, value, is_optional, parent):
else:
element_type = Any

if not (
_valid_value_annotation_type(element_type)
) and not is_structured_config(element_type):
if not _valid_value_annotation_type(element_type) and not is_structured_config(
element_type
):
raise ValidationError(f"Unsupported value type : {element_type}")

value = DictConfig(parent=None, content=value, element_type=element_type)
Expand Down
5 changes: 4 additions & 1 deletion omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def _resolve_single(self, value):
return new

def _set_item_impl(self, key, value):
must_wrap = isinstance(value, (dict, list))
input_config = isinstance(value, Config)
input_node = isinstance(value, ValueNode)
if isinstance(self.__dict__["content"], dict):
Expand All @@ -429,7 +430,9 @@ def wrap(val):
parent=self,
)

if input_node and target_node:
if must_wrap:
self.__dict__["content"][key] = wrap(value)
elif input_node and target_node:
# both nodes, replace existing node with new one
self.__dict__["content"][key] = value
elif not input_node and target_node:
Expand Down
11 changes: 7 additions & 4 deletions omegaconf/nodes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copy
import math
from abc import abstractmethod
from enum import Enum

import math
from typing import Optional, Dict

from .errors import ValidationError, UnsupportedValueType
Expand Down Expand Up @@ -76,10 +77,12 @@ def __init__(self, value=None, parent: Optional[Node] = None, is_optional=True):
self.set_value(value)

def validate_and_convert(self, value):
from ._utils import _valid_input_value_type
from ._utils import _is_primitive_type

if not _valid_input_value_type(value):
raise UnsupportedValueType(f"Unsupported value type {value}")
if not _is_primitive_type(value):
raise UnsupportedValueType(
f"Unsupported value type, type={type(value)}, value={value}"
)
return value

def __deepcopy__(self, memo={}):
Expand Down

0 comments on commit 16286d5

Please sign in to comment.