Skip to content

fix(units): restore registry when add-enabled-units raises - #20795

Closed
Sudhakar7J wants to merge 1 commit into
astropy:mainfrom
Sudhakar7J:fix/add-enabled-units-exception-restore
Closed

Sudhakar7J wants to merge 1 commit into
astropy:mainfrom
Sudhakar7J:fix/add-enabled-units-exception-restore

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Description

This pull request is to address #20790 where add_enabled_units can leave a unit-registry context active when enabling a conflicting alias raises.

The public factories constructed/pushed _UnitContext before validation, so a ValueError leaked the push — the enclosing context then popped the inner level on exit, leaving the custom definition enabled.

Fix: each of the six factories (add/set_enabled_units, add/set_enabled_equivalencies, add/set_enabled_aliases) now restores the registry on BaseException by unwinding its own context before re-raising. Deferred construction was avoided to preserve the documented permanent (no-with) mode.

Testing

  • New regression tests: test_enabled_units_context_restored_on_failed_add (full kcal issue repro, registry identity + restoration) and test_enabled_unit_factories_restore_registry_on_error (other five factories)
  • pytest --pyargs astropy.units.tests.test_units -k unit -> 390 passed, 2 skipped
  • pytest --pyargs astropy.units.tests.test_units astropy.units.tests.test_equivalencies -> 470 passed, 2 skipped

Fixes #20790

AI Disclosure

Used Crouton Alpha (opencode/crouton-alpha-free) via OpenCode as a coding assistant to implement the fix and regression tests under the astropy AI policy (https://github.com/astropy/astropy-project/blob/main/policies/ai-policy.md). AI generated the initial patch and test cases; human reviewed the diff, verified behavior on astropy 8.0.2, and ran the full test suite.

  • I certify that I am human and that I take full responsibility for this pull request including all interactions with reviewers.

Each public units context factory pushed a new registry via _UnitContext and then mutated it. If validation raised (e.g. a name clash), the caller never received the context manager, so the pushed registry was never popped. A nested failure therefore corrupted the registry stack: the enclosing with-block popped the wrong level on exit and its custom definitions stayed enabled (astropygh-20790).

Wrap the mutation in each of the six factories (add/set_enabled_units, add/set_enabled_equivalencies, add/set_enabled_aliases) with try/except BaseException that unwinds the pushed context before re-raising.

Closes astropy#20790

Signed-off-by: Sudhakar <[email protected]>
@github-actions github-actions Bot added the units label Nov 6, 2026
@giles

giles Bot commented Nov 6, 2026

Copy link
Copy Markdown

Hi 👋 and thank you for your contribution! 🙏

This pull request is being closed automatically - but don't worry, we may re-open it if you carefully follow these instructions.

Join the astropy slack using this invite link, head over to the #new-contributor-requests channel, and introduce yourself with a post that includes:

  1. Background about your current occupation or affiliation.

  2. How the bug or missing feature affects your work.

We strongly encourage contributions from people that use astropy or actively develop related scientific Python packages. We look forward to hearing from you and growing the Astropy community!

Notes for maintainers

If you know this is a genuine user and the PR is is compliance with the AI policy, you may re-open this straight away. After two successful merged pull requests, the user will be added to the allowlist and future PRs will no longer be closed automatically.

This user has not made any pull requests to this repository prior to this one.

In addition, here are some statistics on the user's activity on GitHub:

Last 24 hours Last 7 days
Pull requests opened 1 14
Issues opened 0 0

@github-actions

github-actions Bot commented Nov 6, 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.

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.

add_enabled_units leaves a registry context active when alias validation raises

1 participant