Skip to content

Fix CompoundModel.input_units to be independent of operand order#20020

Open
PranavAchar01 wants to merge 4 commits into
astropy:mainfrom
PranavAchar01:fix/17040-compound-input-units-order
Open

Fix CompoundModel.input_units to be independent of operand order#20020
PranavAchar01 wants to merge 4 commits into
astropy:mainfrom
PranavAchar01:fix/17040-compound-input-units-order

Conversation

@PranavAchar01

Copy link
Copy Markdown

Description

This pull request fixes CompoundModel.input_units returning a result that depends on the operand order for arithmetic operators.

Fixes #17040.

For arithmetic operators (+ - * / **) the inputs are shared by both operands, but inputs_map() only records the left operand. input_units therefore never consulted the right operand, so an expression whose left operand has no input units (e.g. Const1D) returned None, while the reversed order returned the correct units:

import astropy.units as u
import astropy.modeling.models as m
g = m.Gaussian1D(mean=976.8 * u.AA, amplitude=4 * u.DN, stddev=1 * u.AA)
c = m.Const1D(amplitude=1 * u.DN)
(c + g).input_units   # was None      -> now {'x': Unit("Angstrom")}
(g + c).input_units   # {'x': Unit("Angstrom")}

The fix fills in the units of any input the left operand does not constrain from the right operand, so the result is independent of operand order. If neither operand constrains the units the result is still None. Composition (|) and join (&) are unaffected.

A regression test is added in astropy/modeling/tests/test_quantities_model.py. The astropy/modeling test suite passes locally.

Note: this change was prepared with the assistance of Claude Code; it has been reviewed and tested locally by the submitter.

For arithmetic operators the inputs are shared by both operands, but
inputs_map only records the left one, so input_units ignored the right
operand. An expression like Const1D() + Gaussian1D() (whose left operand has
no input units) returned None, while the reversed order returned the correct
units. Fill in the units of any input the left operand does not constrain from
the right operand.

Closes astropy#17040
@PranavAchar01 PranavAchar01 requested a review from a team as a code owner July 3, 2026 07:31
Copilot AI review requested due to automatic review settings July 3, 2026 07:31
@github-actions

github-actions Bot commented Jul 3, 2026

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes CompoundModel.input_units for arithmetic compound models so the computed input units no longer depend on operand order, addressing the case where only the right-hand operand constrains input units (e.g., Const1D() + Gaussian1D()).

Changes:

  • Update CompoundModel.input_units to fill unconstrained input units from the right operand for arithmetic operators (+ - * / **).
  • Add a regression test covering operand-order independence for input_units.
  • Add a changelog fragment documenting the bugfix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
astropy/modeling/core.py Ensures arithmetic compound models can derive missing input-unit constraints from the right operand.
astropy/modeling/tests/test_quantities_model.py Adds a regression test to prevent operand-order-dependent input_units.
docs/changes/modeling/20020.bugfix.rst Documents the bugfix in the modeling changelog.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread astropy/modeling/core.py
Comment on lines +3900 to +3905
if self.op in ("+", "-", "*", "/", "**"):
right_units = self.right.input_units
if right_units:
for left_key, right_key in zip(self.inputs, self.right.inputs):
if left_key not in input_units_dict and right_key in right_units:
input_units_dict[left_key] = right_units[right_key]
Comment thread astropy/modeling/tests/test_quantities_model.py Outdated
PranavAchar01 and others added 2 commits July 3, 2026 02:11
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

CompoundModel.input_units does not consider both sides of the expression when computing input_units

2 participants