Skip to content

Commit

Permalink
Renamed omegaconf.Config to omegaconf.Container
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Dec 25, 2019
1 parent 5b1e575 commit 7c54df2
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 62 deletions.
1 change: 1 addition & 0 deletions news/103.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Renamed omegaconf.Config to omegaconf.Container
4 changes: 2 additions & 2 deletions omegaconf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .config import Config
from .container import Container
from .dictconfig import DictConfig
from .errors import (
MissingMandatoryValue,
Expand Down Expand Up @@ -27,7 +27,7 @@
"ReadonlyConfigError",
"UnsupportedValueType",
"UnsupportedKeyType",
"Config",
"Container",
"ListConfig",
"DictConfig",
"OmegaConf",
Expand Down
4 changes: 2 additions & 2 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def _maybe_wrap(annotated_type, value, is_optional, parent):
if isinstance(value, ValueNode):
return value

from omegaconf import Config, OmegaConf
from omegaconf import Container, OmegaConf

if isinstance(value, Config):
if isinstance(value, Container):
value = OmegaConf.to_container(value)

origin = getattr(annotated_type, "__origin__", None)
Expand Down
57 changes: 25 additions & 32 deletions omegaconf/config.py → omegaconf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
from .nodes import ValueNode


class Config(Node):
class Container(Node):
# static fields
_resolvers = {}

def __init__(self, element_type, parent: Node):
super().__init__(parent=parent)
if type(self) == Config:
if type(self) == Container:
raise NotImplementedError
self.__dict__["content"] = None
self.__dict__["_resolver_cache"] = defaultdict(dict)
Expand Down Expand Up @@ -195,14 +195,14 @@ def update(self, key, value=None):
k = split[i]
# if next_root is a primitive (string, int etc) replace it with an empty map
next_root, key_ = _select_one(root, k)
if not isinstance(next_root, Config):
root[key_] = DictConfig(content={}, parent=self)
if not isinstance(next_root, Container):
root[key_] = {}
root = root[key_]

last = split[-1]

assert isinstance(
root, (DictConfig, ListConfig)
root, Container
), f"Unexpected type for root : {type(root).__name__}"

if isinstance(root, DictConfig):
Expand Down Expand Up @@ -253,12 +253,12 @@ def convert(val):

return val

assert isinstance(conf, Config)
assert isinstance(conf, Container)
if isinstance(conf, DictConfig):
ret = {}
for key, value in conf.items(resolve=resolve):
if isinstance(value, Config):
ret[key] = Config._to_content(
if isinstance(value, Container):
ret[key] = Container._to_content(
value, resolve=resolve, enum_to_str=enum_to_str
)
else:
Expand All @@ -270,8 +270,8 @@ def convert(val):
if resolve:
item = conf[index]
item = convert(item)
if isinstance(item, Config):
item = Config._to_content(
if isinstance(item, Container):
item = Container._to_content(
item, resolve=resolve, enum_to_str=enum_to_str
)
ret.append(item)
Expand All @@ -284,7 +284,7 @@ def to_container(self, resolve=False):
stacklevel=2,
)

return Config._to_content(self, resolve)
return Container._to_content(self, resolve)

def pretty(self, resolve=False):
from omegaconf import OmegaConf
Expand Down Expand Up @@ -316,13 +316,13 @@ def _map_merge(dest, src):
dest[key] = DictConfig(content=dest_type, parent=dest)
dest_node = dest.get_node(key)

if isinstance(dest_node, Config):
if isinstance(value, Config):
if isinstance(dest_node, Container):
if isinstance(value, Container):
dest_node.merge_with(value)
else:
dest.__setitem__(key, value)
else:
if isinstance(value, Config):
if isinstance(value, Container):
dest.__setitem__(key, value)
else:
dest_node.set_value(value)
Expand All @@ -342,7 +342,7 @@ def merge_with(self, *others):
if other is None:
raise ValueError("Cannot merge with a None config")
if isinstance(self, DictConfig) and isinstance(other, DictConfig):
Config._map_merge(self, other)
Container._map_merge(self, other)
elif isinstance(self, ListConfig) and isinstance(other, ListConfig):
if self._get_flag("readonly"):
raise ReadonlyConfigError(self.get_full_key(""))
Expand Down Expand Up @@ -380,28 +380,21 @@ def _resolve_value(root_node, inter_type, inter_key):
def _resolve_single(self, value):
value_kind, match_list = get_value_kind(value=value, return_match_list=True)

assert value_kind in [
ValueKind.VALUE,
ValueKind.MANDATORY_MISSING,
ValueKind.INTERPOLATION,
ValueKind.STR_INTERPOLATION,
]

if value_kind in (ValueKind.VALUE, ValueKind.MANDATORY_MISSING):
return value

root = self._get_root()
if value_kind == ValueKind.INTERPOLATION:
# simple interpolation, inherit type
match = match_list[0]
return Config._resolve_value(root, match.group(1), match.group(2))
return Container._resolve_value(root, match.group(1), match.group(2))
elif value_kind == ValueKind.STR_INTERPOLATION:
# Concatenated interpolation, always a string
orig = value
new = ""
last_index = 0
for match in match_list:
new_val = Config._resolve_value(root, match.group(1), match.group(2))
new_val = Container._resolve_value(root, match.group(1), match.group(2))
new += orig[last_index : match.start(0)] + str(new_val)
last_index = match.end(0)

Expand All @@ -411,7 +404,7 @@ def _resolve_single(self, value):
# noinspection PyProtectedMember
def _set_item_impl(self, key, value):
must_wrap = isinstance(value, (dict, list))
input_config = isinstance(value, Config)
input_config = isinstance(value, Container)
input_node = isinstance(value, ValueNode)
if isinstance(self.__dict__["content"], dict):
target_node = key in self.__dict__["content"] and isinstance(
Expand Down Expand Up @@ -472,8 +465,8 @@ def _item_eq(c1, k1, c2, k2):
# noinspection PyProtectedMember
v2 = c2._resolve_single(v2)

if isinstance(v1, Config) and isinstance(v2, Config):
if not Config._config_eq(v1, v2):
if isinstance(v1, Container) and isinstance(v2, Container):
if not Container._config_eq(v1, v2):
return False
return v1 == v2

Expand All @@ -486,7 +479,7 @@ def _list_eq(l1, l2):
if len(l1) != len(l2):
return False
for i in range(len(l1)):
if not Config._item_eq(l1, i, l2, i):
if not Container._item_eq(l1, i, l2, i):
return False

return True
Expand All @@ -504,7 +497,7 @@ def _dict_conf_eq(d1, d2):
if k1 != k2:
return False
for k in k1:
if not Config._item_eq(d1, k, d2, k):
if not Container._item_eq(d1, k, d2, k):
return False

return True
Expand All @@ -514,11 +507,11 @@ def _config_eq(c1, c2):
from .listconfig import ListConfig
from .dictconfig import DictConfig

assert isinstance(c1, Config)
assert isinstance(c2, Config)
assert isinstance(c1, Container)
assert isinstance(c2, Container)
if isinstance(c1, DictConfig) and isinstance(c2, DictConfig):
return DictConfig._dict_conf_eq(c1, c2)
if isinstance(c1, ListConfig) and isinstance(c2, ListConfig):
return Config._list_eq(c1, c2)
return Container._list_eq(c1, c2)
# if type does not match objects are different
return False
10 changes: 5 additions & 5 deletions omegaconf/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
is_structured_config,
_re_parent,
)
from .config import Config
from .container import Container
from .errors import (
ReadonlyConfigError,
MissingMandatoryValue,
Expand All @@ -22,7 +22,7 @@
from .nodes import ValueNode


class DictConfig(Config):
class DictConfig(Container):
def __init__(self, content, parent: Optional[Node] = None, element_type=Any):
super().__init__(element_type=element_type, parent=parent)

Expand Down Expand Up @@ -81,7 +81,7 @@ def __setitem__(self, key, value):
self._validate_access(key)
self._validate_type(key, value)

if isinstance(value, Config):
if isinstance(value, Container):
value = copy.deepcopy(value)
value._set_parent(self)

Expand Down Expand Up @@ -229,9 +229,9 @@ def _next_pair(self):

def __eq__(self, other):
if isinstance(other, dict):
return Config._dict_conf_eq(self, DictConfig(other))
return Container._dict_conf_eq(self, DictConfig(other))
if isinstance(other, DictConfig):
return Config._dict_conf_eq(self, other)
return Container._dict_conf_eq(self, other)
return NotImplemented

def __ne__(self, other):
Expand Down
10 changes: 5 additions & 5 deletions omegaconf/listconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from typing import Any, Optional

from ._utils import _maybe_wrap, isint, _re_parent
from .config import Config
from .container import Container
from .errors import ReadonlyConfigError, UnsupportedValueType, UnsupportedKeyType
from .node import Node
from .nodes import ValueNode, AnyNode


class ListConfig(Config):
class ListConfig(Container):
def __init__(self, content, parent: Optional[Node] = None, element_type=Any):
super().__init__(parent=parent, element_type=element_type)
self.__dict__["content"] = []
Expand Down Expand Up @@ -65,7 +65,7 @@ def _set_at_index(self, index, value):
if self._get_flag("readonly"):
raise ReadonlyConfigError(self.get_full_key(index))

if isinstance(value, Config):
if isinstance(value, Container):
value = copy.deepcopy(value)
value._set_parent(self)

Expand Down Expand Up @@ -179,9 +179,9 @@ def key1(x):

def __eq__(self, other):
if isinstance(other, list):
return Config._list_eq(self, ListConfig(other))
return Container._list_eq(self, ListConfig(other))
if isinstance(other, ListConfig):
return Config._list_eq(self, other)
return Container._list_eq(self, other)
return NotImplemented

def __ne__(self, other):
Expand Down
20 changes: 10 additions & 10 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Any

from ._utils import is_structured_config, decode_primitive
from .config import Config
from .container import Container
from .errors import ValidationError, MissingMandatoryValue

MISSING: Any = "???"
Expand Down Expand Up @@ -67,7 +67,7 @@ def create(obj=None, parent=None):
else:
if obj is None:
obj = {}
if isinstance(obj, Config):
if isinstance(obj, Container):
obj = OmegaConf.to_container(obj)

if isinstance(obj, dict) or is_structured_config(obj):
Expand Down Expand Up @@ -152,7 +152,7 @@ def register_resolver(name, resolver):
assert callable(resolver), "resolver must be callable"
# noinspection PyProtectedMember
assert (
name not in Config._resolvers
name not in Container._resolvers
), "resolved {} is already registered".format(name)

def caching(config, key):
Expand All @@ -164,17 +164,17 @@ def caching(config, key):
return val

# noinspection PyProtectedMember
Config._resolvers[name] = caching
Container._resolvers[name] = caching

# noinspection PyProtectedMember
@staticmethod
def get_resolver(name):
return Config._resolvers[name] if name in Config._resolvers else None
return Container._resolvers[name] if name in Container._resolvers else None

# noinspection PyProtectedMember
@staticmethod
def clear_resolvers():
Config._resolvers = {}
Container._resolvers = {}
register_default_resolvers()

@staticmethod
Expand Down Expand Up @@ -236,9 +236,9 @@ def to_container(cfg, resolve=False, enum_to_str=False):
:param enum_to_str: True to convert Enum values to strings
:return: A dict or a list representing this config as a primitive container.
"""
assert isinstance(cfg, Config)
assert isinstance(cfg, Container)
# noinspection PyProtectedMember
return Config._to_content(cfg, resolve=resolve, enum_to_str=enum_to_str)
return Container._to_content(cfg, resolve=resolve, enum_to_str=enum_to_str)

@staticmethod
def is_missing(cfg, key):
Expand All @@ -262,9 +262,9 @@ def is_dict(cfg):

@staticmethod
def is_config(cfg):
from . import Config
from . import Container

return isinstance(cfg, Config)
return isinstance(cfg, Container)


# register all default resolvers
Expand Down
6 changes: 3 additions & 3 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
IntegerNode,
StringNode,
ValidationError,
Config,
Container,
ListConfig,
DictConfig,
ReadonlyConfigError,
Expand Down Expand Up @@ -397,8 +397,8 @@ def test_list_copy_is_shallow(self, copy_method):


def test_not_implemented(mocker):
mocker.patch("omegaconf.Config.__init__", lambda self: None)
obj = Config()
mocker.patch("omegaconf.Container.__init__", lambda self: None)
obj = Container()
with pytest.raises(NotImplementedError):
obj == "foo"

Expand Down
Loading

0 comments on commit 7c54df2

Please sign in to comment.