Reject logarithmic units with a non-default scale in Quantity#19892
Open
gaoflow wants to merge 2 commits into
Open
Reject logarithmic units with a non-default scale in Quantity#19892gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
Contributor
|
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.
|
Quantity._set_unit went through a silent string round-trip so that a dimensionless Magnitude becomes a Quantity in mag. For a function unit with a non-default scale like 2 mag(m), that round-trip produced an UnrecognizedUnit (a UnitBase), which slipped past the guard and yielded a nonsensical Quantity. Reject the UnrecognizedUnit result too.
bbb6ee1 to
b1ea725
Compare
00d0e32 to
bb80f8c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19867.
Initializing a
Quantitywith a logarithmic unit is meant to fail:but with a non-default function unit it silently succeeded:
Quantity._set_unitroutes a non-UnitBaseunit throughUnit(str(unit), parse_strict="silent")so that, e.g., a dimensionlessMagnitudebecomes aQuantityinmag. Formag(m)the round-trip yields aMagUnit, which the existing guard rejects. For2 mag(m)the string"2 mag(m)"does not parse, andparse_strict="silent"turns it into anUnrecognizedUnit— which is aUnitBase, so it slipped through.The fix also rejects an
UnrecognizedUnitresult, so the non-default-scale case raisesUnitTypeErrorlike the default one. The intended conversions (dimensionlessMagnitude/Dex/Decibel→Quantityinmag/dex/dB) still work, andQuantity(..., subok=True)is unaffected.Regression test parametrizes the default and non-default function-unit cases in
TestLogQuantityCreation; the non-default case fails without the change.