-
Notifications
You must be signed in to change notification settings - Fork 468
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
custom unit formats #1371
custom unit formats #1371
Conversation
this currently fails because |
actually, maybe this is a case for the Since it's pretty difficult to do that without breaking anything, I renamed the |
this still needs proper documentation (e.g. there should be a complete list of formats available, with examples), but I guess that depends on #972 |
I just noticed that The tests fail because |
Regarding |
pint/formatting.py
Outdated
>>> @pint.register_unit_format("custom") | ||
... def format_custom(unit, registry, **options): | ||
... result = "<formatted unit>" # do the formatting | ||
... return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is executed twice (once for pint.formatting.register_unit_format
and once for pint.register_unit_format
), and because there's no isolation in between the runs, the second run will try to register a pre-existing name. Not sure how to avoid that... does sphinx
have a test isolation feature, or should we switch to python -m pytest --doctest-modules
and the ipython
/ jupyter
sphinx
directives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I can tell, there's not much we can do here, except switching the doctest runner to pytest
(and the doctest
blocks in the rst
files to ipython
/ jupyter-execute
), to replace autodoc
with autosummary
, or to make sure autodoc
does not generate pages for both pint.register_unit_format
and pint.formatting.register_unit_format
.
Since all of these are out-of-scope for this PR, I'll convert the example to a code block and we can discuss further changes in a dedicated issue / PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is no way out.
Good work @keewis looks promising. 👍 |
I didn't bring this up before, but I basically disallowed passing quantity modifiers (just f"{u:#C}" is a error, but ureg.default_format = "#C"
f"{u}" is fine. This might be a breaking change (it caused a few tests to fail in Edit: if we choose to keep the breaking change I think we should improve the error message: |
pint.formatting.format_unit() changed slightly between pint 0.17 and 0.18; see hgrecco/pint@8c719483, part of hgrecco/pint#1371. - Discard the "~" format modifier once it has been checked. - Discard any leading ":". - Adjust docstring.
This adds a
register_unit_format
function and implements the currently available formats using that. I'd also like to switch thesiunitx
code ("Lx"
) to use the new format, but because that is implemented in a function that expects aUnit
object instead of aUnitContainer
I still need to figure out a way to make it compatible.I also renamed the default
""
format to"D"
.pre-commit run --all-files
with no errors