Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use custom formatting for dimensionless units #1486

Open
aulemahal opened this issue Mar 16, 2022 · 2 comments
Open

Unable to use custom formatting for dimensionless units #1486

aulemahal opened this issue Mar 16, 2022 · 2 comments

Comments

@aulemahal
Copy link
Contributor

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:

pint/pint/formatting.py

Lines 397 to 401 in 7394cd9

if not unit:
if spec.endswith("%"):
return ""
else:
return "dimensionless"

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.

@huard
Copy link

huard commented Apr 5, 2022

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.

@keewis
Copy link
Contributor

keewis commented Apr 7, 2022

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.

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

No branches or pull requests

3 participants