summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-08-09 06:58:32 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-08-09 06:58:32 +0200
commitc2d45fe4a14cb67f52b04bf8c82c39aac74eece6 (patch)
treede3919bb42450f286cc54928dc9bee17cfe07abb
parent593c5df4d4f34c340bfd9f2d7bb12358dabf400f (diff)
downloadruamel.yaml-c2d45fe4a14cb67f52b04bf8c82c39aac74eece6.tar.gz
adding test for issue 196
-rw-r--r--_test/test_anchor.py44
-rw-r--r--comments.py41
2 files changed, 63 insertions, 22 deletions
diff --git a/_test/test_anchor.py b/_test/test_anchor.py
index 3a92036..102e48b 100644
--- a/_test/test_anchor.py
+++ b/_test/test_anchor.py
@@ -1,5 +1,7 @@
# coding: utf-8
+from __future__ import print_function
+
"""
testing of anchors and the aliases referring to them
"""
@@ -381,7 +383,9 @@ class TestMergeKeysValues:
d = safe_load(self.yaml_str)
data = round_trip_load(self.yaml_str)
x = data[2].items()
+ print('d2 items', d[2].items(), len(d[2].items()), x, len(x))
ref = len(d[2].items())
+ print('ref', ref)
assert len(x) == ref
del data[2]['m']
if PY3:
@@ -396,6 +400,43 @@ class TestMergeKeysValues:
ref -= 1
assert len(x) == ref
+ def test_issue_196_cast_of_dict(self, capsys):
+ from ruamel.yaml import YAML
+ yaml = YAML()
+ mapping = yaml.load("""\
+ anchored: &anchor
+ a : 1
+
+ mapping:
+ <<: *anchor
+ b: 2
+ """)['mapping']
+
+ for k in mapping:
+ print('k', k)
+ for k in mapping.copy():
+ print('kc', k)
+
+ print('v', list(mapping.keys()))
+ print('v', list(mapping.values()))
+ print('v', list(mapping.items()))
+ print(len(mapping))
+ print('-----')
+
+ # print({**mapping})
+ # print(type({**mapping}))
+ # assert 'a' in {**mapping}
+ assert 'a' in mapping
+ x = {}
+ for k in mapping:
+ x[k] = mapping[k]
+ assert 'a' in x
+ assert 'a' in mapping.keys()
+ assert mapping['a'] == 1
+ assert mapping.__getitem__('a') == 1
+ assert 'a' in dict(mapping)
+ assert 'a' in dict(mapping.items())
+
def test_issue_213_copy_of_merge(self):
from ruamel.yaml import YAML
yaml = YAML()
@@ -409,7 +450,8 @@ class TestMergeKeysValues:
assert d['a'] == 'a'
d2 = d.copy()
assert d2['a'] == 'a'
- d.pop('a')
+ print('d', d)
+ del d['a']
assert 'a' not in d
assert 'a' in d2
diff --git a/comments.py b/comments.py
index 88ccea3..22040fa 100644
--- a/comments.py
+++ b/comments.py
@@ -16,9 +16,9 @@ from ruamel.yaml.compat import ordereddict, PY2, string_types
from ruamel.yaml.scalarstring import ScalarString
if PY2:
- from collections import MutableSet, Sized, Set
+ from collections import MutableSet, Sized, Set, MutableMapping
else:
- from collections.abc import MutableSet, Sized, Set
+ from collections.abc import MutableSet, Sized, Set, MutableMapping
if False: # MYPY
from typing import Any, Dict, Optional, List, Union, Optional # NOQA
@@ -530,24 +530,8 @@ class CommentedMapView(Sized):
def __len__(self):
# type: () -> int
count = len(self._mapping)
- done = [] # type: List[Any] # list of processed merge items, kept for masking
- for merged in getattr(self._mapping, merge_attrib, []):
- for x in merged[1]:
- if self._mapping._unmerged_contains(x):
- continue
- for y in done:
- if x in y:
- break
- else:
- count += 1
- done.append(merged[1])
return count
- def __repr__(self):
- # type: () -> str
- return '{0.__class__.__name__}({0._mapping!r})'.format(self)
-
-
class CommentedMapKeysView(CommentedMapView, Set):
__slots__ = ()
@@ -562,6 +546,7 @@ class CommentedMapKeysView(CommentedMapView, Set):
def __iter__(self):
# type: () -> Any # yield from self._mapping # not in py27, pypy
+ # for x in self._mapping._keys():
for x in self._mapping:
yield x
@@ -602,7 +587,7 @@ class CommentedMapValuesView(CommentedMapView):
def __iter__(self):
# type: () -> Any
- for key in self._mapping:
+ for key in self._mapping._keys():
yield self._mapping[key]
@@ -803,7 +788,7 @@ class CommentedMap(ordereddict, CommentedBase):
yield x
done = [] # type: List[Any] # list of processed merge items, kept for masking
for merged in getattr(self, merge_attrib, []):
- for x in merged[1]:
+ for x in merged[1].keys():
if ordereddict.__contains__(self, x):
continue
for y in done:
@@ -813,6 +798,21 @@ class CommentedMap(ordereddict, CommentedBase):
yield x
done.append(merged[1])
+ def __len__(self):
+ count = ordereddict.__len__(self)
+ done = [] # type: List[Any] # list of processed merge items, kept for masking
+ for merged in getattr(self, merge_attrib, []):
+ for x in merged[1]:
+ if ordereddict.__contains__(self, x):
+ continue
+ for y in done:
+ if x in y:
+ break
+ else:
+ count += 1
+ done.append(merged[1])
+ return count
+
if PY2:
def keys(self):
@@ -915,7 +915,6 @@ class CommentedMap(ordereddict, CommentedBase):
def copy(self):
x = {} # update doesn't work
for k, v in self._items():
- print(k, v)
x[k] = v
return x