From 7c54df2c6ccb08034a55b43e83b5c94593a4e517 Mon Sep 17 00:00:00 2001 From: Omry Yadan Date: Wed, 25 Dec 2019 14:15:45 +0200 Subject: [PATCH] Renamed omegaconf.Config to omegaconf.Container --- news/103.removal | 1 + omegaconf/__init__.py | 4 +- omegaconf/_utils.py | 4 +- omegaconf/{config.py => container.py} | 57 ++++++++++++--------------- omegaconf/dictconfig.py | 10 ++--- omegaconf/listconfig.py | 10 ++--- omegaconf/omegaconf.py | 20 +++++----- tests/test_base_config.py | 6 +-- tests/test_basic_ops_dict.py | 6 +-- 9 files changed, 56 insertions(+), 62 deletions(-) create mode 100644 news/103.removal rename omegaconf/{config.py => container.py} (91%) diff --git a/news/103.removal b/news/103.removal new file mode 100644 index 000000000..f7b8584a7 --- /dev/null +++ b/news/103.removal @@ -0,0 +1 @@ +Renamed omegaconf.Config to omegaconf.Container diff --git a/omegaconf/__init__.py b/omegaconf/__init__.py index 237ebb6d8..c6bd28745 100644 --- a/omegaconf/__init__.py +++ b/omegaconf/__init__.py @@ -1,4 +1,4 @@ -from .config import Config +from .container import Container from .dictconfig import DictConfig from .errors import ( MissingMandatoryValue, @@ -27,7 +27,7 @@ "ReadonlyConfigError", "UnsupportedValueType", "UnsupportedKeyType", - "Config", + "Container", "ListConfig", "DictConfig", "OmegaConf", diff --git a/omegaconf/_utils.py b/omegaconf/_utils.py index dcca6e1f4..e85c81a17 100644 --- a/omegaconf/_utils.py +++ b/omegaconf/_utils.py @@ -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) diff --git a/omegaconf/config.py b/omegaconf/container.py similarity index 91% rename from omegaconf/config.py rename to omegaconf/container.py index 6ffb64297..0ad1539e6 100644 --- a/omegaconf/config.py +++ b/omegaconf/container.py @@ -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) @@ -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): @@ -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: @@ -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) @@ -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 @@ -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) @@ -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("")) @@ -380,13 +380,6 @@ 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 @@ -394,14 +387,14 @@ def _resolve_single(self, value): 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) @@ -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( @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/omegaconf/dictconfig.py b/omegaconf/dictconfig.py index 531c041c5..977de81a6 100644 --- a/omegaconf/dictconfig.py +++ b/omegaconf/dictconfig.py @@ -9,7 +9,7 @@ is_structured_config, _re_parent, ) -from .config import Config +from .container import Container from .errors import ( ReadonlyConfigError, MissingMandatoryValue, @@ -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) @@ -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) @@ -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): diff --git a/omegaconf/listconfig.py b/omegaconf/listconfig.py index 6a82135e4..024e25a81 100644 --- a/omegaconf/listconfig.py +++ b/omegaconf/listconfig.py @@ -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"] = [] @@ -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) @@ -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): diff --git a/omegaconf/omegaconf.py b/omegaconf/omegaconf.py index 736b9811b..47ab76d49 100644 --- a/omegaconf/omegaconf.py +++ b/omegaconf/omegaconf.py @@ -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 = "???" @@ -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): @@ -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): @@ -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 @@ -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): @@ -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 diff --git a/tests/test_base_config.py b/tests/test_base_config.py index fed1b4dae..aec1a02e9 100644 --- a/tests/test_base_config.py +++ b/tests/test_base_config.py @@ -8,7 +8,7 @@ IntegerNode, StringNode, ValidationError, - Config, + Container, ListConfig, DictConfig, ReadonlyConfigError, @@ -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" diff --git a/tests/test_basic_ops_dict.py b/tests/test_basic_ops_dict.py index 488d39a71..67dc4d205 100644 --- a/tests/test_basic_ops_dict.py +++ b/tests/test_basic_ops_dict.py @@ -11,7 +11,7 @@ UnsupportedKeyType, DictConfig, AnyNode, - Config, + Container, ) from . import IllegalType @@ -308,7 +308,7 @@ def test_pretty_with_resolve(): def test_instantiate_config_fails(): with pytest.raises(NotImplementedError): - Config(element_type=Any, parent=None) + Container(element_type=Any, parent=None) def test_dir(): @@ -396,7 +396,7 @@ def neq(a, b): def test_config_eq_mismatch_types(): c1 = OmegaConf.create({}) c2 = OmegaConf.create([]) - assert not Config._config_eq(c1, c2) + assert not Container._config_eq(c1, c2) def test_dict_not_eq_with_another_class():