@@ -243,6 +243,28 @@ class AstropyDumper(yaml.SafeDumper):
243243 def _represent_tuple (self , data ):
244244 return self .represent_sequence ("tag:yaml.org,2002:python/tuple" , data )
245245
246+ def represent_float (self , data ):
247+ # Override to change repr(data) to str(data) since otherwise all the
248+ # numpy scalars fail in not NUMPY_LT_1_20.
249+ if data != data or (data == 0.0 and data == 1.0 ):
250+ value = ".nan"
251+ elif data == self .inf_value :
252+ value = ".inf"
253+ elif data == - self .inf_value :
254+ value = "-.inf"
255+ else :
256+ value = str (data ).lower ()
257+ # Note that in some cases `repr(data)` represents a float number
258+ # without the decimal parts. For instance:
259+ # >>> repr(1e17)
260+ # '1e17'
261+ # Unfortunately, this is not a valid float representation according
262+ # to the definition of the `!!float` tag. We fix this by adding
263+ # '.0' before the 'e' symbol.
264+ if "." not in value and "e" in value :
265+ value = value .replace ("e" , ".0e" , 1 )
266+ return self .represent_scalar ("tag:yaml.org,2002:float" , value )
267+
246268
247269AstropyDumper .add_multi_representer (u .UnitBase , _unit_representer )
248270AstropyDumper .add_multi_representer (u .FunctionUnitBase , _unit_representer )
@@ -273,9 +295,7 @@ def _represent_tuple(self, data):
273295 np_type , yaml .representer .SafeRepresenter .represent_int
274296 )
275297for np_type in [np .float16 , np .float32 , np .float64 , np .longdouble ]:
276- AstropyDumper .add_representer (
277- np_type , yaml .representer .SafeRepresenter .represent_float
278- )
298+ AstropyDumper .add_representer (np_type , AstropyDumper .represent_float )
279299for np_type in [complex , np .complex64 , np .complex128 ]:
280300 AstropyDumper .add_representer (np_type , _complex_representer )
281301
0 commit comments