Skip to content

Reject logarithmic units with a non-default scale in Quantity#19892

Open
gaoflow wants to merge 2 commits into
astropy:mainfrom
gaoflow:fix/19867-quantity-rejects-logarithmic-unit
Open

Reject logarithmic units with a non-default scale in Quantity#19892
gaoflow wants to merge 2 commits into
astropy:mainfrom
gaoflow:fix/19867-quantity-rejects-logarithmic-unit

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 10, 2026

Copy link
Copy Markdown

Fixes #19867.

Initializing a Quantity with a logarithmic unit is meant to fail:

>>> u.Quantity(u.Magnitude(5.0 * u.m))
UnitTypeError: Quantity instances require normal units, not MagUnit instances.

but with a non-default function unit it silently succeeded:

>>> u.Quantity(u.Magnitude(5.0 * u.m, 2 * u.mag))
<Quantity -0.87371251 2 mag(m)>

Quantity._set_unit routes a non-UnitBase unit through Unit(str(unit), parse_strict="silent") so that, e.g., a dimensionless Magnitude becomes a Quantity in mag. For mag(m) the round-trip yields a MagUnit, which the existing guard rejects. For 2 mag(m) the string "2 mag(m)" does not parse, and parse_strict="silent" turns it into an UnrecognizedUnit — which is a UnitBase, so it slipped through.

The fix also rejects an UnrecognizedUnit result, so the non-default-scale case raises UnitTypeError like the default one. The intended conversions (dimensionless Magnitude/Dex/DecibelQuantity in mag/dex/dB) still work, and Quantity(..., 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.

@github-actions github-actions Bot added the units label Jun 10, 2026
@github-actions

Copy link
Copy Markdown
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.

  • 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 instructions for rebase and squash.
  • 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?
  • 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.

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.
@gaoflow gaoflow force-pushed the fix/19867-quantity-rejects-logarithmic-unit branch from bbb6ee1 to b1ea725 Compare June 10, 2026 06:10
@gaoflow gaoflow force-pushed the fix/19867-quantity-rejects-logarithmic-unit branch from 00d0e32 to bb80f8c Compare June 10, 2026 06:25
@pllim pllim added this to the v8.1.0 milestone Jun 10, 2026
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.

Corner case abilty to initialize a Quantity with a logarithmic unit

2 participants