summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-08-08 22:39:09 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-08-08 22:39:09 +0200
commit593c5df4d4f34c340bfd9f2d7bb12358dabf400f (patch)
tree8b2589e2c4425de7f519660e2e50e2bebce6a8f9
parentfd91cad90c37a36a6574e14feb7c7f2199bba745 (diff)
downloadruamel.yaml-593c5df4d4f34c340bfd9f2d7bb12358dabf400f.tar.gz
fix issue #213 : CommentedMap.copy() incomplete result
Only the "native" keys were copied, not any of the merged keys. Now .copy() is implemented and a "flattened" new dict is returned. *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so usingthe WorkFlow pull-down (close to the top right of this page)*
-rw-r--r--_test/test_anchor.py17
-rw-r--r--comments.py7
2 files changed, 24 insertions, 0 deletions
diff --git a/_test/test_anchor.py b/_test/test_anchor.py
index 3838833..3a92036 100644
--- a/_test/test_anchor.py
+++ b/_test/test_anchor.py
@@ -396,6 +396,23 @@ class TestMergeKeysValues:
ref -= 1
assert len(x) == ref
+ def test_issue_213_copy_of_merge(self):
+ from ruamel.yaml import YAML
+ yaml = YAML()
+ d = yaml.load("""\
+ foo: &foo
+ a: a
+ foo2:
+ <<: *foo
+ b: b
+ """)['foo2']
+ assert d['a'] == 'a'
+ d2 = d.copy()
+ assert d2['a'] == 'a'
+ d.pop('a')
+ assert 'a' not in d
+ assert 'a' in d2
+
class TestDuplicateKeyThroughAnchor:
def test_duplicate_key_00(self):
diff --git a/comments.py b/comments.py
index 84fecbc..88ccea3 100644
--- a/comments.py
+++ b/comments.py
@@ -912,6 +912,13 @@ class CommentedMap(ordereddict, CommentedBase):
setattr(self, merge_attrib, [])
return getattr(self, merge_attrib)
+ def copy(self):
+ x = {} # update doesn't work
+ for k, v in self._items():
+ print(k, v)
+ x[k] = v
+ return x
+
def add_yaml_merge(self, value):
# type: (Any) -> None
self.merge.extend(value)