Skip to content

Commit 32706a7

Browse files
authored
Document OmegaConf.merge handling of MISSING values (#771)
Document that OmegaConf.merge treats MISSING values on the source side as no value to apply, so a concrete target value is not overwritten. Fixes #771.
1 parent daa8bac commit 32706a7

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

docs/source/usage.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ If you load them and merge them with ``list_merge_mode=ListMergeMode.EXTEND_UNIQ
529529
>>>
530530
>>> cfg_1 = OmegaConf.load('source/example2.yaml')
531531
>>> cfg_2 = OmegaConf.load('source/example4.yaml')
532-
>>>
532+
>>>
533533
>>> mode = ListMergeMode.EXTEND_UNIQUE
534534
>>> conf = OmegaConf.merge(cfg_1, cfg_2, list_merge_mode=mode)
535535
>>> print(OmegaConf.to_yaml(conf))
@@ -541,6 +541,32 @@ If you load them and merge them with ``list_merge_mode=ListMergeMode.EXTEND_UNIQ
541541
- user3
542542
<BLANKLINE>
543543

544+
Missing values in ``merge()``
545+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
546+
547+
``merge()`` treats ``MISSING`` (``"???"``) on the *source* side as "no value
548+
to apply" rather than as a value to write — a non-missing value on the
549+
target is never overwritten by a missing value coming from a later config.
550+
Merging in the opposite order does fill the missing value:
551+
552+
.. doctest::
553+
554+
>>> from omegaconf import OmegaConf
555+
>>> base = OmegaConf.create({"port": 80})
556+
>>> override = OmegaConf.create({"port": "???"})
557+
>>>
558+
>>> # MISSING on the source side does not overwrite an existing value.
559+
>>> print(OmegaConf.merge(base, override))
560+
{'port': 80}
561+
>>>
562+
>>> # The reverse order does fill the missing value.
563+
>>> print(OmegaConf.merge(override, base))
564+
{'port': 80}
565+
566+
This is useful when a later config (e.g. a base schema) declares some keys
567+
as mandatory: those declarations do not clobber concrete values supplied by
568+
an earlier config.
569+
544570
Union operator (``|`` and ``|=``)
545571
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
546572

news/771.docs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

0 commit comments

Comments
 (0)