diff options
author | Anthon van der Neut <anthon@mnt.org> | 2017-08-21 17:50:19 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2017-08-21 17:50:19 +0200 |
commit | a3ebcf7603851375fb882717f1697c9813b262cf (patch) | |
tree | 919811658adf8c43973e2e2737a3e9365b1475fd | |
parent | a0f924806ef8d20caf7a91171b7e631a7d74d47a (diff) | |
download | ruamel.yaml-a3ebcf7603851375fb882717f1697c9813b262cf.tar.gz |
fix issue #149: operated on ScalarFloat cannot be dumped.0.15.32
Thanks for reporting
I "fixed" this by making the operators return a normal (bare) float, as the preservation is
mostly there for round-trips without operations.
Contrary to ScalarInt you cannot as easily preserve the style, and that needs a lot more testing,
and probably much smarter code (i.e. 1.23e4 *= 10 should probably give 1.23e5 and not 12.3e4).
*If this change makes things work again without your explicit convert to bare float, please
**Close** this issue.*
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | README.rst | 5 | ||||
-rw-r--r-- | __init__.py | 4 | ||||
-rw-r--r-- | _test/test_api_change.py | 3 | ||||
-rw-r--r-- | _test/test_float.py | 11 | ||||
-rw-r--r-- | representer.py | 2 | ||||
-rw-r--r-- | scalarfloat.py | 6 |
7 files changed, 32 insertions, 4 deletions
@@ -1,3 +1,8 @@ +[0, 15, 32]: 2017-08-21 + - allow setting ``yaml.default_flow_style = None`` (default: ``False``) for + for ``typ='rt'``. + - fix for issue 149: multiplications on ``ScalarFloat`` now return ``float`` + [0, 15, 31]: 2017-08-15 - fix Comment dumping @@ -35,6 +35,11 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key +0.15.32 (2017-08-21): + - allow setting ``yaml.default_flow_style = None`` (default: ``False``) for + for ``typ='rt'``. + - fix for issue 149: multiplications on ``ScalarFloat`` now return ``float`` + 0.15.31 (2017-08-15): - fix Comment dumping diff --git a/__init__.py b/__init__.py index 7f40acf..a90d9d2 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, 31), - __version__='0.15.31', + version_info=(0, 15, 32), + __version__='0.15.32', 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/_test/test_api_change.py b/_test/test_api_change.py index 4066603..c0f492c 100644 --- a/_test/test_api_change.py +++ b/_test/test_api_change.py @@ -126,6 +126,7 @@ class TestDuplSet: ? a """)) + class TestDumpLoadUnicode: # test triggered by SamH on stackoverflow (https://stackoverflow.com/q/45281596/1307905) # and answer by randomir (https://stackoverflow.com/a/45281922/1307905) @@ -152,7 +153,7 @@ class TestFlowStyle: yaml.default_flow_style = None data = yaml.map() data['b'] = 1 - data['a'] = [[1, 2],[3, 4]] + data['a'] = [[1, 2], [3, 4]] yaml.dump(data, sys.stdout) out, err = capsys.readouterr() assert out == "b: 1\na:\n- [1, 2]\n- [3, 4]\n" diff --git a/_test/test_float.py b/_test/test_float.py index 498c271..f9d060d 100644 --- a/_test/test_float.py +++ b/_test/test_float.py @@ -141,3 +141,14 @@ class TestFloat: --- - 1e6 """) + + +class TestCalculations(object): + def test_mul_00(self): + # issue 149 reported by jan.brezina@tul.cz + d = round_trip_load("""\ + - 0.1 + """) + d[0] *= -1 + x = round_trip_dump(d) + assert x == '- -0.1\n' diff --git a/representer.py b/representer.py index 2279897..70746bc 100644 --- a/representer.py +++ b/representer.py @@ -653,7 +653,7 @@ class RoundTripRepresenter(SafeRepresenter): def __init__(self, default_style=None, default_flow_style=None, dumper=None): # type: (Any, Any, Any) -> None if not hasattr(dumper, 'typ') and default_flow_style is None: - default_flow_style = False + default_flow_style = False SafeRepresenter.__init__(self, default_style=default_style, default_flow_style=default_flow_style, dumper=dumper) diff --git a/scalarfloat.py b/scalarfloat.py index 7f7d26d..fca8be2 100644 --- a/scalarfloat.py +++ b/scalarfloat.py @@ -35,6 +35,7 @@ class ScalarFloat(float): def __iadd__(self, a): # type: ignore # type: (Any) -> Any + return float(self) + a x = type(self)(self + a) x._width = self._width # type: ignore x._underscore = self._underscore[:] if self._underscore is not None else None # type: ignore # NOQA @@ -42,6 +43,7 @@ class ScalarFloat(float): def __ifloordiv__(self, a): # type: ignore # type: (Any) -> Any + return float(self) // a x = type(self)(self // a) x._width = self._width # type: ignore x._underscore = self._underscore[:] if self._underscore is not None else None # type: ignore # NOQA @@ -49,13 +51,16 @@ class ScalarFloat(float): def __imul__(self, a): # type: ignore # type: (Any) -> Any + return float(self) * a x = type(self)(self * a) x._width = self._width # type: ignore x._underscore = self._underscore[:] if self._underscore is not None else None # type: ignore # NOQA + x._prec = self._prec # check for others return x def __ipow__(self, a): # type: ignore # type: (Any) -> Any + return float(self) ** a x = type(self)(self ** a) x._width = self._width # type: ignore x._underscore = self._underscore[:] if self._underscore is not None else None # type: ignore # NOQA @@ -63,6 +68,7 @@ class ScalarFloat(float): def __isub__(self, a): # type: ignore # type: (Any) -> Any + return float(self) - a x = type(self)(self - a) x._width = self._width # type: ignore x._underscore = self._underscore[:] if self._underscore is not None else None # type: ignore # NOQA |