Repository navigation
Avoid resorting units when converting to strings - #14439
Conversation
|
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.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
| else: | ||
| raise ValueError(f"Syntax error parsing unit '{s}'") | ||
|
|
||
| @classmethod |
There was a problem hiding this comment.
This is really the main change. Since FITS and VOUNIT inherit from generic, this means that they had the same sorting -- undoing the more logical sorting in their classes. Indeed, the sorting that was done in FITS/VOUNIT just followed what CompositeUnit itself does anyway, so in that sense for those formats this change makes them behave as intended...
| flam = u.erg / u.cm / u.cm / u.s / u.AA | ||
| x = u.format.VOUnit.to_string(flam) | ||
| assert x == "Angstrom**-1.cm**-2.erg.s**-1" | ||
| assert x == "erg.Angstrom**-1.s**-1.cm**-2" |
cae748e to
2ed9d09
Compare
|
Well, with one approval, I'll open this for general review! Thanks, @nstarman! |
c24ea1a to
6ee3e5c
Compare
|
Rebased this PR that removes extra sorting when units are represented as strings with
|
I think so, esp if point 2 isn't completed by v5.3!
I think that would be fantastic! |
WilliamJamieson
left a comment
There was a problem hiding this comment.
Modeling changes look good to me
6ee3e5c to
6def838
Compare
|
Thanks! I've added a what's-new entry (and rebased for good measure). It is probably not a bad idea to merge this well before the feature freeze date, so we can get some feedback (e.g., if many doctests break). That also gives some time to experiment with possibly using a default representation based on physical type. |
|
Need to fix doc warning: |
|
I don't know. I too agree that this PR is good improvement. I need to sleep on this over the weekend. |
|
What about the |
|
One possible issue is that the unit is not only printed as part of Aside, could one use something like |
Unfortunately, no... scientific-python/pytest-doctestplus#193 But somehow you can do it via |
|
You mean this? scientific-python/pytest-doctestplus#112 Actually, it may be a reasonable solution to tell people that they can set the style with a version constraint in |
|
Yes, but alas you cannot just ask doctestplus to only require certain astropy version for that one line, it is whole module. Not ideal but might be the best we can do for now. |
|
I don't think that is that bad -- it shows that examples work with the most recent version of astropy; presumably, the tests cover the rests. |
|
If you are curious on how I am handling this downstream, please see the following PRs and feel free to comment on them if you have better ideas. Thanks! |
|
Hey, so one can do So, I guess the to-do item may be to have this as part of either the what's-new or the API change fragment... |
|
Might be easier to explain in What's New. Do you want to do a follow-up PR? If not, I can take a stab when I have time. |
|
Makes sense. If you can have a stab, that would be great - I'll be mostly out of action until term ends in April... |
workaround astropy#14439 downstream
|
Please see #14526 |
workaround astropy#14439 downstream
|
This bit astroquery as well, and the suggested workaround in setup.cfg is not exactly nice (we do want to test both old and new astropy versions). |
|
@bsipocz - sorry to hear that this bit you... I love the principle of doctests, of ensuring that what the user will see when they run some example is actually what is in the documentation, but it does seem they raise the barrier rather a lot for making changes to how output looks - one shouldn't have to think of formatting as part of the API! |
|
OTOH, if formatting is not important enough to consider backward compatibility, it may not be worth changing it and causing such widespread issues downstream, at least not without also adding tools that provide reasonable workarounds (globally skipping doctesting in rst files is not really a nice workaround). |
|
It's a hard one - the On balance, we felt it was worth it. It is a bit like numpy in 1.13 getting around to finally clean-up the number of significant digits that get included in output (following python doing the same). Output looks much better now but it was a gigantic pain (even with |
|
Unit string changes will break ASDF unit validation as jsonschema only exactly matches the listed unit string. Resulting in the need for spacetelescope/roman_datamodels#258. I think this maybe the only project currently using ASDF's unit support, so this should not be a big issue right now. In the future, please tag me on any unit string changes. |
|
@WilliamJamieson , you can always add yourself to the "units" section in codeowners, then you will get pinged automatically. FYI. |
|
@WilliamJamieson - I think this is going to be the last string change in a very long time.... Hopefully mostly because it is now as "right" as it can be, but certainly also to avoid again causing breakage and thus loss of time that could be better spent elsewhere... |
When converting units to strings, the order of the base units is now always the same as it is in the unit itself: by decreasing power to which the unit is raised, and then alphabetical. This avoids super-ugly representations such as
beam-1 Jy(such as could happen for FITS; see #13217).However, as can be seen from the PR, it also means some (doc)tests had to be adapted, and it can reasonably be foreseen that this will cause similar down-stream failures.
Hence, opening as draft for now.EDIT: given initial positive feedback, changed to regular PR.(Note based on #14437 for now; will rebase once that is in.)EDIT: rebased now previous format PRs are in.Fixes #13217