From 565e87e615599a8bf03357f81f9ac7c1af36236d Mon Sep 17 00:00:00 2001 From: aishwaryaanegundi <4752676+aishwaryaanegundi@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:35:09 +0100 Subject: [PATCH 1/2] Explicitly adds decimal point to mantissa before computing the width. This fixes the incorrect trucating of decimal point in representer. --- constructor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/constructor.py b/constructor.py index 6aa6371..0a4401f 100644 --- a/constructor.py +++ b/constructor.py @@ -1217,6 +1217,8 @@ def leading_zeros(v): except ValueError: mantissa, exponent = value_so.split('E') exp = 'E' + if not '.' in mantissa: + mantissa = mantissa + '.0' if self.resolver.processing_version != (1, 2): # value_s is lower case independent of input if '.' not in mantissa: From 5bb7816a0866b6a4939c3f5dcd7912f29c713963 Mon Sep 17 00:00:00 2001 From: aishwaryaanegundi <4752676+aishwaryaanegundi@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:37:40 +0100 Subject: [PATCH 2/2] Always add decimal point to mantissa irrespective of the version of the serialiser --- representer.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/representer.py b/representer.py index 820559b..125e36e 100644 --- a/representer.py +++ b/representer.py @@ -287,16 +287,16 @@ def represent_float(self, data): value = '-.inf' else: value = repr(data).lower() - if getattr(self.serializer, 'use_version', None) == (1, 1): - if '.' not in value and 'e' in value: - # Note that in some cases `repr(data)` represents a float number - # without the decimal parts. For instance: - # >>> repr(1e17) - # '1e17' - # Unfortunately, this is not a valid float representation according - # to the definition of the `!!float` tag in YAML 1.1. We fix - # this by adding '.0' before the 'e' symbol. - value = value.replace('e', '.0e', 1) + # if getattr(self.serializer, 'use_version', None) == (1, 1): + if '.' not in value and 'e' in value: + # Note that in some cases `repr(data)` represents a float number + # without the decimal parts. For instance: + # >>> repr(1e17) + # '1e17' + # Unfortunately, this is not a valid float representation according + # to the definition of the `!!float` tag in YAML 1.1. We fix + # this by adding '.0' before the 'e' symbol. + value = value.replace('e', '.0e', 1) return self.represent_scalar('tag:yaml.org,2002:float', value) def represent_list(self, data):