Skip to content

Commit

Permalink
adding OmegaConf.{is_config,is_dict,is_list}
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Dec 24, 2019
1 parent 29c1fb6 commit fc58310
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
26 changes: 25 additions & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,32 @@ You can temporarily remove the struct flag from a config object:
Utility functions
-----------------

OmegaConf.is_missing
^^^^^^^^^^^^^^^^^^^^

Tests if a key is missing ('???')

.. doctest::

>>> cfg = OmegaConf.create({"foo" : 10, "bar": "???"})
>>> assert not OmegaConf.is_missing(cfg, "foo")
>>> assert OmegaConf.is_missing(cfg, "bar")

OmegaConf.{is_config, is_dict, is_list}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Tests if an object is an OmegaConf object, or if it's representing a list or a dict.

.. doctest::

>>> list_cfg = OmegaConf.create([1,2,3])
>>> dict_cfg = OmegaConf.create({"foo": "bar"})
>>> assert OmegaConf.is_config(list_cfg) and OmegaConf.is_config(dict_cfg)
>>> assert OmegaConf.is_dict(dict_cfg) and not OmegaConf.is_dict(list_cfg)
>>> assert OmegaConf.is_list(list_cfg) and not OmegaConf.is_list(dict_cfg)

OmegaConf.to_container
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^
OmegaConf config objects looks very similar to python dict and lists, but in fact are not.
Use OmegaConf.to_container(cfg, resolve) to convert to a primitive container.
If resolve is set to True the values will be resolved during conversion.
Expand Down
1 change: 1 addition & 0 deletions news/101.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add OmegaConf.{is_config, is_list, is_dict} to test if an Object is an OmegaConf object, and if it's a list or a dict
1 change: 1 addition & 0 deletions news/102.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add OmegaConf.is_missing(cfg, key) to test if a key is missing ('???') in a config
1 change: 0 additions & 1 deletion omegaconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@
"MISSING",
"SI",
"II",
"Config",
]
2 changes: 1 addition & 1 deletion omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _valid_value_annotation_type(type_):
def _valid_input_value_type(value):
if isinstance(value, Enum) or (isinstance(value, type) and issubclass(value, Enum)):
return True
return type(value) in (int, float, bool, str, dict, list, tuple, type(None))
return type(value) in (dict, list, tuple) or _is_primitive_type(value)


def _node_wrap(type_, parent, is_optional, value):
Expand Down
18 changes: 18 additions & 0 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ def is_missing(cfg, key):
except MissingMandatoryValue:
return True

@staticmethod
def is_list(cfg):
from . import ListConfig

return isinstance(cfg, ListConfig)

@staticmethod
def is_dict(cfg):
from . import DictConfig

return isinstance(cfg, DictConfig)

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

return isinstance(cfg, Config)


# register all default resolvers
register_default_resolvers()
Expand Down
20 changes: 20 additions & 0 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,23 @@ def test_omegaconf_create():
def test_is_missing(cfg, key, expected):
cfg = OmegaConf.create(cfg)
assert OmegaConf.is_missing(cfg, key) == expected


@pytest.mark.parametrize(
"cfg, is_conf, is_list, is_dict",
[
(None, False, False, False),
({}, False, False, False),
("aa", False, False, False),
(10, False, False, False),
(True, False, False, False),
(bool, False, False, False),
(StringNode("foo"), False, False, False),
(OmegaConf.create({}), True, False, True),
(OmegaConf.create([]), True, True, False),
],
)
def test_is_config(cfg, is_conf, is_list, is_dict):
assert OmegaConf.is_config(cfg) == is_conf
assert OmegaConf.is_list(cfg) == is_list
assert OmegaConf.is_dict(cfg) == is_dict

0 comments on commit fc58310

Please sign in to comment.