From bdc07310846d253ddeac78f370db03df5d6e31b0 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Tue, 8 Jan 2019 09:03:34 +0100 Subject: resolves issue #274 replace dict with collections.OrderedDict when loading https://bitbucket.org/ruamel/yaml/issues/274/add-a-typ-ordered-for-a-middle-ground#comment-49783394 *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 | 8 ++++++++ README.rst | 12 ++++++++++-- __init__.py | 4 ++-- _doc/_static/pypi.svg | 2 +- _test/test_api_change.py | 1 + constructor.py | 10 ++++++---- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index bb6f0ae..bb25af7 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,11 @@ +[0, 15, 85]: 2019-01-08 + - the types used by `SafeConstructor` for mappings and sequences can + now by set by assigning to `XXXConstructor.yaml_base_dict_type` + (and `..._list_type`), preventing the need to copy two methods + with 50+ lines that had `var = {}` hardcoded. (Implemented to + help solve an feature request by `Anthony Sottile + `__ in an easier way) + [0, 15, 84]: 2019-01-07 - fix for `CommentedMap.copy()` not returning `CommentedMap`, let alone copying comments etc. (reported by `Anthony Sottile `__) diff --git a/README.rst b/README.rst index f42a5bd..7f929bb 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.84 -:updated: 2019-01-07 +:version: 0.15.85 +:updated: 2019-01-08 :documentation: http://yaml.readthedocs.io :repository: https://bitbucket.org/ruamel/ :pypi: https://pypi.org/project/ruamel.yaml/ @@ -54,6 +54,14 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key (with empty line) +0.15.85 (2019-01-08): + - the types used by `SafeConstructor` for mappings and sequences can + now by set by assigning to `XXXConstructor.yaml_base_dict_type` + (and `..._list_type`), preventing the need to copy two methods + with 50+ lines that had `var = {}` hardcoded. (Implemented to + help solve an feature request by `Anthony Sottile + `__ in an easier way) + 0.15.84 (2019-01-07): - fix for `CommentedMap.copy()` not returning `CommentedMap`, let alone copying comments etc. (reported by `Anthony Sottile `__) diff --git a/__init__.py b/__init__.py index 5719064..8a0ef9e 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, 84), - __version__='0.15.84', + version_info=(0, 15, 85), + __version__='0.15.85', 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 f62388f..4c05b38 100644 --- a/_doc/_static/pypi.svg +++ b/_doc/_static/pypi.svg @@ -1 +1 @@ - pypipypi0.15.840.15.84 + pypipypi0.15.850.15.85 diff --git a/_test/test_api_change.py b/_test/test_api_change.py index 9bb26b5..3f3d411 100644 --- a/_test/test_api_change.py +++ b/_test/test_api_change.py @@ -146,6 +146,7 @@ class TestRead: with pytest.raises(ConstructorError): yaml.load(s) # should parse fine + yaml = YAML(typ='safe') for _ in yaml.parse(s): pass diff --git a/constructor.py b/constructor.py index 65d6829..53e0d27 100644 --- a/constructor.py +++ b/constructor.py @@ -63,6 +63,8 @@ class BaseConstructor(object): if self.loader is not None and getattr(self.loader, '_constructor', None) is None: self.loader._constructor = self self.loader = loader + self.yaml_base_dict_type = dict + self.yaml_base_list_type = list self.constructed_objects = {} # type: Dict[Any, Any] self.recursive_objects = {} # type: Dict[Any, Any] self.state_generators = [] # type: List[Any] @@ -207,13 +209,13 @@ class BaseConstructor(object): raise ConstructorError( None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark ) - total_mapping = {} + total_mapping = self.yaml_base_dict_type() if getattr(node, 'merge', None) is not None: todo = [(node.merge, False), (node.value, False)] else: todo = [(node.value, True)] for values, check in todo: - mapping = {} # type: Dict[Any, Any] + mapping = self.yaml_base_dict_type() # type: Dict[Any, Any] for key_node, value_node in values: # keys can be list -> deep key = self.construct_object(key_node, deep=True) @@ -686,13 +688,13 @@ class SafeConstructor(BaseConstructor): def construct_yaml_seq(self, node): # type: (Any) -> Any - data = [] # type: List[Any] + data = self.yaml_base_list_type() # type: List[Any] yield data data.extend(self.construct_sequence(node)) def construct_yaml_map(self, node): # type: (Any) -> Any - data = {} # type: Dict[Any, Any] + data = self.yaml_base_dict_type() # type: Dict[Any, Any] yield data value = self.construct_mapping(node) data.update(value) -- cgit v1.2.1