Skip to content

Commit

Permalink
Protocols: fix mutation of overrides (#650)
Browse files Browse the repository at this point in the history
Currently the `overrides` input dictionary provided to the
`get_builder_from_protocol` method can be mutated to also include the
defaults from the protocol. Here we make a copy of the `right`
dictionary in the `recursive_merge` function, to prevent the key/value
pairs of the `left` dictionary to be added to the `right` one. This
prevents the mutation of the `overrides` argument.
  • Loading branch information
mbercx authored Feb 18, 2021
1 parent bde3225 commit 128ec59
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aiida_quantumespresso/workflows/protocols/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def recursive_merge(left, right):
"""
import collections

# Note that a deepcopy is not necessary, since this function is called recusively.
right = right.copy()

for key, value in left.items():
if key in right:
if isinstance(value, collections.abc.Mapping) and isinstance(right[key], collections.abc.Mapping):
Expand Down
15 changes: 15 additions & 0 deletions tests/workflows/protocols/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""Tests for the utility functions for the protocols."""


def test_recursive_merge():
"""Test the recursive merge function."""
from aiida_quantumespresso.workflows.protocols.utils import recursive_merge

left_dict = {'a': {'b': 1, 'c': {'d': 2}}, 'g': 3}
right_dict = {'a': {'c': {'d': 'D'}}, 'e': {'f': 'F'}}
merged = recursive_merge(left_dict, right_dict)

assert right_dict == {'a': {'c': {'d': 'D'}}, 'e': {'f': 'F'}}

assert merged == {'a': {'b': 1, 'c': {'d': 'D'}}, 'e': {'f': 'F'}, 'g': 3}

0 comments on commit 128ec59

Please sign in to comment.