Skip to content

Pass none in physical value converter's convert_back - #47

Merged
cmeyer merged 3 commits into
nion-software:masterfrom
MattRoyle:pass-none-fix
Aug 26, 2026
Merged

cmeyer merged 3 commits into
nion-software:masterfrom
MattRoyle:pass-none-fix

Conversation

@MattRoyle

@MattRoyle MattRoyle commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

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

…he float to string converter used by convert back
@MattRoyle MattRoyle self-assigned this Aug 19, 2026
@MattRoyle
MattRoyle requested a review from KRLango August 19, 2026 15:51
Comment thread nion/utils/Converter.py Outdated
Comment thread nion/utils/Converter.py Outdated
@MattRoyle
MattRoyle requested a review from Tiomat85 August 20, 2026 12:23

@Tiomat85 Tiomat85 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MattRoyle
MattRoyle requested a review from Ion-e August 20, 2026 13:14
@cmeyer

cmeyer commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Is this used anywhere? It's always worth extra precaution to ensure this doesn't change existing behavior unintentionally.

@MattRoyle

Copy link
Copy Markdown
Contributor Author

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 cmeyer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MattRoyle
MattRoyle marked this pull request as ready for review August 20, 2026 15:26
@MattRoyle

Copy link
Copy Markdown
Contributor Author

Does this convert_back function handle "42 nm"?

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.0

However 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.

@cmeyer

cmeyer commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

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.

@MattRoyle
MattRoyle requested a review from KRLango August 25, 2026 09:08

@KRLango KRLango left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cmeyer
cmeyer merged commit faa1216 into nion-software:master Aug 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PhysicalValueToStringConverter convert back ignores pass_none

4 participants