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

Allow "console" and "unicode" formats for function units #14407

Merged
merged 3 commits into from Feb 17, 2023

Conversation

olebole
Copy link
Member

@olebole olebole commented Feb 16, 2023

Description

Originally, function unit format were restricted to generic, unscaled, latex and latex_inline:

>>> import astropy.units as u
>>> print(u.decibel(u.m**-1).to_string('unicode'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/astropy/units/function/core.py", line 395, in to_string
    raise ValueError(
ValueError: Function units cannot be written in unicode format. Only 'generic', 'unscaled', 'latex' and 'latex_inline' are supported.

Also, functional units didn't support the __format__ function:

>>> print(f"{u.decibel(u.m**-1):generic}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to DecibelUnit.__format__

This PR implements both, including the required implementation of function units with multiline unit format:

>>> import astropy.units as u
>>> print(u.decibel(u.m**-1).to_string('unicode'))
    1
dB( ─)
    m
>>> print(f"{u.decibel(u.m**-1):generic}")
dB(1 / m)
>>> print(f"{u.decibel(u.m**-1):unicode}")
    1
dB( ─)
    m

If #14393 is merged before this, we need to adjust this one a bit depending on the outcome of #14393. Specifically, we could to add **kwargs propagation to the to_string() function to enable/disable multiline.

@github-actions github-actions bot added the units label Feb 16, 2023
@github-actions
Copy link

github-actions bot commented Feb 16, 2023

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@pllim pllim added this to the v5.3 milestone Feb 16, 2023
@pllim pllim requested a review from mhvk February 16, 2023 18:20
@olebole olebole force-pushed the allow-unicode-for-function-units branch from 0f76ee8 to 745678c Compare February 16, 2023 18:22
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good beyond a few nitpicks, but I feel we might as well propagate inline here too -- i.e., get #14393 in first and then rebase. What do you think?

astropy/units/function/core.py Show resolved Hide resolved
astropy/units/function/core.py Outdated Show resolved Hide resolved
astropy/units/function/core.py Outdated Show resolved Hide resolved
astropy/units/tests/test_format.py Outdated Show resolved Hide resolved
docs/changes/units/14407.feature.rst Outdated Show resolved Hide resolved
@olebole olebole force-pushed the allow-unicode-for-function-units branch from 0776524 to 3bafd56 Compare February 17, 2023 11:25
@olebole
Copy link
Member Author

olebole commented Feb 17, 2023

After merging #14393, we get

>>> print(f"{u.decibel(u.m**-1):unicode}")
dB(m⁻¹)
>>> print(f"{u.decibel(u.m**-1):console}")
dB(m^-1)

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@mhvk mhvk merged commit 6989544 into astropy:main Feb 17, 2023
@olebole olebole deleted the allow-unicode-for-function-units branch February 17, 2023 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants