Pass none in physical value converter's convert_back - #47
Conversation
…he float to string converter used by convert back
Tiomat85
left a comment
There was a problem hiding this comment.
The PR is fine for what it identifies to fix - pass_none with convert_back.
This converter looks to have other, unrelated, pretty significant flaws.
|
Is this used anywhere? It's always worth extra precaution to ensure this doesn't change existing behavior unintentionally. |
Currently the pass_none and fuzzy parameters are not used anywhere for PhysicalValueToStringConverter, however something I am working on needed to use this converter but with pass_none and I noticed it was not passing None as expected so updated it to properly handle the parameter. |
cmeyer
left a comment
There was a problem hiding this comment.
I'm actually ok with the reformatting - I do that as minor cleanup. Technically @KRLango is correct, but it's minor.
I'm also ok with this "as is".
However, there is an incongruity - the __float_to_string_converter is used in convert_back but not in convert. This highlights that one direction goes from a raw float (e.g. 3.1) to a string scaled with units (e.g. "31 nm") and then the user may type units on the way back (e.g. "42 nm") resulting in a new underlying value (e.g. 4.2).
Does this convert_back function handle "42 nm"?
Overall, this class is kind of a hack (by me, years ago). Whatever fixes it for your needs is ok with me, but we might consider thinking about the bigger picture of how we want a proper unit converting converter to work.
Yes the convert back by default can handle the units being there. >>> from nion.utils import Converter
>>> con = Converter.PhysicalValueToStringConverter(units="nm")
>>> print(con.convert_back("42 nm"))
42.0However if you set fuzzy to false then there will be a traceback. >>> con2 = Converter.PhysicalValueToStringConverter(units="nm", fuzzy=False)
>>> print(con2.convert_back("42 nm"))
Traceback (most recent call last):
File "<python-input-5>", line 1, in <module>
print(con2.convert_back("42 nm"))
~~~~~~~~~~~~~~~~~^^^^^^^^^
File "C:\NionApps\Developer\main\nionutils\nion\utils\Converter.py", line 154, in convert_back
value = self.__float_to_string_converter.convert_back(formatted_value)
File "C:\NionApps\Developer\main\nionutils\nion\utils\Converter.py", line 91, in convert_back
return locale.atof(formatted_value) if formatted_value else 0.0
~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\matthew.royle\AppData\Roaming\uv\python\cpython-3.14.2-windows-x86_64-none\Lib\locale.py", line 328, in atof
return func(delocalize(string))Maybe I should change this PR to only pass the pass_none parameter and not pass fuzzy since for a physical value it should always be true to not hit the traceback. |
|
Now with unexpected behavior exposed during this conversation, I think we should add a test case or two for this. They will be useful for catching regressions if we migrate this to something with more features. |
Fixes #46
Passes the 'pass_none' parameter into the float to string converter used in the convert back function.
Searching for usages currently no PhysicalValueToStringConverter's use the pass_none or fuzzy parameters so there shouldn't be any behavior changes