pint icon indicating copy to clipboard operation
pint copied to clipboard

Unable to use custom formatting for dimensionless units

Open aulemahal opened this issue 2 years ago • 2 comments

Hi!

Some time ago, cf-xarray implemented a CF-compliant unit formatter using pint.register_unit_format. It works well except for dimensionless quantities. The current code for format_unit hardcodes what happens with dimensionless units. It's either "" if there was a ~ flag or:

https://github.com/hgrecco/pint/blob/7394cd91c2a1c895fe3f9d456b1b5cd33f4ca129/pint/formatting.py#L397-L401

Which makes the custom formatter unable to modify the behaviour. In our case here, the CF version would be "" (and sometimes "1" when not attached to a magnitude).

Could we simply remove the code I linked? Pass the container to the formatting function, as with other units?

Tagging @dcherian that might be interested in this issue.

aulemahal avatar Mar 16 '22 22:03 aulemahal

I'm interested in seeing this move forward. Looking at the code, it's not obvious to me where the logic for the if not unit case should live though.

huard avatar Apr 05 '22 19:04 huard

As far as I understand it, % is some kind of unit modifier? If that's correct, I think we should try to treat it the same way as the other modifiers (and document it in the "string formatting" guide). That way, #1448 would allow you to customize its behavior.

keewis avatar Apr 07 '22 08:04 keewis