Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ If you load them and merge them with ``list_merge_mode=ListMergeMode.EXTEND_UNIQ
>>>
>>> cfg_1 = OmegaConf.load('source/example2.yaml')
>>> cfg_2 = OmegaConf.load('source/example4.yaml')
>>>
>>>
>>> mode = ListMergeMode.EXTEND_UNIQUE
>>> conf = OmegaConf.merge(cfg_1, cfg_2, list_merge_mode=mode)
>>> print(OmegaConf.to_yaml(conf))
Expand All @@ -541,6 +541,32 @@ If you load them and merge them with ``list_merge_mode=ListMergeMode.EXTEND_UNIQ
- user3
<BLANKLINE>

Missing values in ``merge()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``merge()`` treats ``MISSING`` (``"???"``) on the *source* side as "no value
to apply" rather than as a value to write — a non-missing value on the
target is never overwritten by a missing value coming from a later config.
Merging in the opposite order does fill the missing value:

.. doctest::

>>> from omegaconf import OmegaConf
>>> base = OmegaConf.create({"port": 80})
>>> override = OmegaConf.create({"port": "???"})
>>>
>>> # MISSING on the source side does not overwrite an existing value.
>>> print(OmegaConf.merge(base, override))
{'port': 80}
>>>
>>> # The reverse order does fill the missing value.
>>> print(OmegaConf.merge(override, base))
{'port': 80}

This is useful when a later config (e.g. a base schema) declares some keys
as mandatory: those declarations do not clobber concrete values supplied by
an earlier config.

Union operator (``|`` and ``|=``)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions news/771.docs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Documented `OmegaConf.merge` behavior with `MISSING` values: a missing value on the source side does not overwrite a non-missing value on the target.
Loading