From e63c1e186f88812d705de8f572373d2abe5f195b Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Thu, 1 Nov 2018 15:41:15 +0100 Subject: fix issue #255 and #256 empty collection defaulting to flow-style an empty collection now defaults to best style. I.e. if elements are inserted their style is not forced to flow-style *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so using the WorkFlow pull-down (close to the top right of this page))* --- CHANGES | 5 +++++ README.rst | 9 +++++++-- __init__.py | 4 ++-- _doc/_static/pypi.svg | 2 +- .../data/duplicate-merge-key.former-loader-error.data | 4 ---- anchor.py | 3 +-- constructor.py | 19 +++++++++++-------- representer.py | 7 +++++-- 8 files changed, 32 insertions(+), 21 deletions(-) delete mode 100644 _test/data/duplicate-merge-key.former-loader-error.data diff --git a/CHANGES b/CHANGES index 51b6be3..2c8e973 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +[0, 15, 76]: 2018-11-01 + - fix issue with empty mapping and sequence loaded as flow-style + (mapping reported by `Min RK `__, sequence + by `Maged Ahmed `__) + [0, 15, 75]: 2018-10-27 - fix issue with single '?' scalar (reported by `Terrance `__) diff --git a/README.rst b/README.rst index a05eb5f..09960a4 100644 --- a/README.rst +++ b/README.rst @@ -4,8 +4,8 @@ ruamel.yaml ``ruamel.yaml`` is a YAML 1.2 loader/dumper package for Python. -:version: 0.15.75 -:updated: 2018-10-27 +:version: 0.15.76 +:updated: 2018-11-01 :documentation: http://yaml.readthedocs.io :repository: https://bitbucket.org/ruamel/ :pypi: https://pypi.org/project/ruamel.yaml/ @@ -54,6 +54,11 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key (with empty line) +0.15.76 (2018-11-01): + - fix issue with empty mapping and sequence loaded as flow-style + (mapping reported by `Min RK `__, sequence + by `Maged Ahmed `__) + 0.15.75 (2018-10-27): - fix issue with single '?' scalar (reported by `Terrance `__) diff --git a/__init__.py b/__init__.py index 764755b..9b28251 100644 --- a/__init__.py +++ b/__init__.py @@ -7,8 +7,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 15, 75), - __version__='0.15.75', + version_info=(0, 15, 76), + __version__='0.15.76', author='Anthon van der Neut', author_email='a.van.der.neut@ruamel.eu', description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA diff --git a/_doc/_static/pypi.svg b/_doc/_static/pypi.svg index 2aa33a3..ef9b8ff 100644 --- a/_doc/_static/pypi.svg +++ b/_doc/_static/pypi.svg @@ -1 +1 @@ - pypipypi0.15.750.15.75 + pypipypi0.15.760.15.76 diff --git a/_test/data/duplicate-merge-key.former-loader-error.data b/_test/data/duplicate-merge-key.former-loader-error.data deleted file mode 100644 index cebc3a1..0000000 --- a/_test/data/duplicate-merge-key.former-loader-error.data +++ /dev/null @@ -1,4 +0,0 @@ ---- -<<: {x: 1, y: 2} -foo: bar -<<: {z: 3, t: 4} diff --git a/anchor.py b/anchor.py index e1b5432..4d77ad3 100644 --- a/anchor.py +++ b/anchor.py @@ -1,8 +1,8 @@ - anchor_attrib = '_yaml_anchor' + class Anchor(object): __slots__ = 'value', 'always_dump' attrib = anchor_attrib @@ -11,4 +11,3 @@ class Anchor(object): # type: () -> None self.value = None self.always_dump = False - diff --git a/constructor.py b/constructor.py index 05fbb34..bf484e1 100644 --- a/constructor.py +++ b/constructor.py @@ -846,7 +846,7 @@ class Constructor(SafeConstructor): lobject_name = [name] try: __import__(module_name) - except ImportError: + except ImportError as exc: raise ConstructorError( 'while constructing a Python object', mark, @@ -1289,7 +1289,7 @@ class RoundTripConstructor(SafeConstructor): return value # merge = [] - merge_map_list = [] + merge_map_list = [] # type: List[Any] index = 0 while index < len(node.value): key_node, value_node = node.value[index] @@ -1494,25 +1494,28 @@ class RoundTripConstructor(SafeConstructor): # type: (Any) -> Any data = CommentedSeq() data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) - if node.flow_style is True: - data.fa.set_flow_style() - elif node.flow_style is False: - data.fa.set_block_style() if node.comment: data._yaml_add_comment(node.comment) yield data data.extend(self.construct_rt_sequence(node, data)) + self.set_collection_style(data, node) def construct_yaml_map(self, node): # type: (Any) -> Any data = CommentedMap() data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) + yield data + self.construct_mapping(node, data) + self.set_collection_style(data, node) + + def set_collection_style(self, data, node): + # type: (Any, Any) -> None + if len(data) == 0: + return if node.flow_style is True: data.fa.set_flow_style() elif node.flow_style is False: data.fa.set_block_style() - yield data - self.construct_mapping(node, data) def construct_yaml_object(self, node, cls): # type: (Any, Any) -> Any diff --git a/representer.py b/representer.py index 28ec9a2..e0ac31d 100644 --- a/representer.py +++ b/representer.py @@ -895,7 +895,7 @@ class RoundTripRepresenter(SafeRepresenter): best_style = False value.append(node_item) if flow_style is None: - if self.default_flow_style is not None: + if len(sequence) != 0 and self.default_flow_style is not None: node.flow_style = self.default_flow_style else: node.flow_style = best_style @@ -961,11 +961,13 @@ class RoundTripRepresenter(SafeRepresenter): except AttributeError: item_comments = {} merge_list = [m[1] for m in getattr(mapping, merge_attrib, [])] + item_count = 0 if bool(merge_list): items = mapping.non_merged_items() else: items = mapping.items() for item_key, item_value in items: + item_count += 1 node_key = self.represent_key(item_key) node_value = self.represent_data(item_value) item_comment = item_comments.get(item_key) @@ -984,7 +986,8 @@ class RoundTripRepresenter(SafeRepresenter): best_style = False value.append((node_key, node_value)) if flow_style is None: - if self.default_flow_style is not None: + if ((item_count != 0) or bool(merge_list)) \ + and self.default_flow_style is not None: node.flow_style = self.default_flow_style else: node.flow_style = best_style -- cgit v1.2.1