diff options
author | Anthon van der Neut <anthon@mnt.org> | 2017-05-29 21:57:25 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2017-05-29 21:57:25 +0200 |
commit | 6ba66a44af41d072f5ceddfcdf2c21611c2a7cd0 (patch) | |
tree | 1d6ce2221f7e08e831b9955b23c361d6dd84b124 /comments.py | |
parent | 37721c337037bda3136e964b1eb5972e2587b4a0 (diff) | |
download | ruamel.yaml-6ba66a44af41d072f5ceddfcdf2c21611c2a7cd0.tar.gz |
preserve value type, support pathlib.Path as stream
Diffstat (limited to 'comments.py')
-rw-r--r-- | comments.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/comments.py b/comments.py index 6bee2ac..93974d9 100644 --- a/comments.py +++ b/comments.py @@ -12,7 +12,8 @@ import copy from collections import MutableSet, Sized, Set -from ruamel.yaml.compat import ordereddict, PY2 +from ruamel.yaml.compat import ordereddict, PY2, string_types +from ruamel.yaml.scalarstring import ScalarString if False: # MYPY from typing import Any, Dict, Optional, List, Union # NOQA @@ -435,6 +436,16 @@ class CommentedSeq(list, CommentedBase): self.copy_attributes(res, deep=True) return res + def __setitem__(self, idx, value): + # type: (Any, Any) -> None + # try to preserve the scalarstring type if setting an existing key to a new value + if idx < len(self): + if isinstance(value, string_types) and \ + not isinstance(value, ScalarString) and \ + isinstance(self[idx], ScalarString): + value = type(self[idx])(value) + list.__setitem__(self, idx, value) + class CommentedKeySeq(tuple, CommentedBase): """This primarily exists to be able to roundtrip keys that are sequences""" @@ -684,6 +695,16 @@ class CommentedMap(ordereddict, CommentedBase): return merged[1][key] raise + def __setitem__(self, key, value): + # type: (Any, Any) -> None + # try to preserve the scalarstring type if setting an existing key to a new value + if key in self: + if isinstance(value, string_types) and \ + not isinstance(value, ScalarString) and \ + isinstance(self[key], ScalarString): + value = type(self[key])(value) + ordereddict.__setitem__(self, key, value) + def _unmerged_contains(self, key): # type: (Any) -> Any if ordereddict.__contains__(self, key): |