Skip to content

🐛 fix(interop-xarray): reject quantify() unit specs for unknown names - #794

Merged
nstarman merged 4 commits into
GalacticDynamics:mainfrom
nstarman:fix-xarray-quantify
Jul 24, 2026
Merged

🐛 fix(interop-xarray): reject quantify() unit specs for unknown names#794
nstarman merged 4 commits into
GalacticDynamics:mainfrom
nstarman:fix-xarray-quantify

Conversation

@nstarman

Copy link
Copy Markdown
Contributor

Summary

attach_units iterated the object's variables/coords doing units.get(name), so any key that matched nothing — most easily a typo in a **unit_kwargs name — was silently dropped:

ds.unxt.quantify(temperatrue="K")   # typo → temperature left completely unquantified, no error

Now both the DataArray and Dataset attach_units dispatches validate the supplied names against the object's variables/coordinates (plus the None key for a DataArray's own data) and raise a ValueError naming the unmatched keys.

Verification (TDD)

New test_unknown_unit_keys.py: a typo'd key raises for both Dataset and DataArray (naming the bad key), while the correct names still quantify. unxts.interop.xarray tests: 15 passed.

Notes

  • The related HIGH "stale units attr after quantify" finding is already fixed (verified — attrs come back {}; handled by 🐛 fix(interop): drop consumed xarray units attr #748).
  • The LOW "units silently dropped on dimension coordinates" finding is left for a follow-up: warning on it correctly requires distinguishing an explicitly-requested unit from an attribute-derived one (a roundtrip, where the attr preserves the record) — that distinction lives in the accessor, not attach_units, so a naive warning is noisy and breaks the roundtrip contract.

🤖 Generated with Claude Code

`attach_units` iterated the object's variables/coords with `units.get(name)`,
so a key that matched nothing -- e.g. a typo in a `**unit_kwargs` name like
`ds.unxt.quantify(temperatrue="K")` -- was silently dropped, leaving the data
completely unquantified with no error or warning.

Validate the supplied names against the object's variables/coordinates (plus
the `None` key for a DataArray's own data) and raise a `ValueError` naming the
unmatched keys, for both the DataArray and Dataset dispatches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 24, 2026 01:45
@nstarman nstarman added this to the v2.0.0 milestone Jul 24, 2026
@github-actions github-actions Bot added 🧩 unxts-interop-xarray Issues/PRs affecting the unxts.interop.xarray namespace package 🐛 Fix a bug Fix a bug. labels Jul 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 hardens the unxts.interop.xarray unit-attachment path so that quantify()/attach_units() no longer silently ignore unit specifications for unknown variable/coordinate names (e.g., typos), and instead raise a ValueError identifying the bad keys.

Changes:

  • Add _reject_unknown_unit_names() and call it from both DataArray and Dataset attach_units dispatches to validate supplied unit keys.
  • Add regression tests ensuring typos in unit keyword names raise for both Dataset and DataArray, while valid names still quantify correctly.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/unxts.interop.xarray/src/unxts/interop/xarray/_src/conversion.py Validate unit-name keys before attaching units, raising on unknown names.
packages/unxts.interop.xarray/tests/test_unknown_unit_keys.py Regression tests covering unknown unit-name keys for both Dataset and DataArray.

Comment thread packages/unxts.interop.xarray/src/unxts/interop/xarray/_src/conversion.py Outdated
@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.75%. Comparing base (9a570a3) to head (0027194).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #794      +/-   ##
==========================================
+ Coverage   91.85%   94.75%   +2.90%     
==========================================
  Files          83       48      -35     
  Lines        3734     2748     -986     
  Branches      310      199     -111     
==========================================
- Hits         3430     2604     -826     
+ Misses        228      102     -126     
+ Partials       76       42      -34     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Address PR GalacticDynamics#794 review: the `None` (DataArray own-data) key was filtered out
of the "Valid names" list, so a DataArray with no coords produced the
confusing `Valid names: .`. Render `None` as `None (the data)` and fall back
to `(none)` when the valid set is otherwise empty.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 24, 2026 01:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Early-return on the no-unknowns case instead of nesting the whole
error-building block under ``if unknown:``, and split the valid-names
generator onto its own line so the ``sorted(...) or "(none)"`` fallback
reads flat rather than through three levels of parens. Behaviour is
unchanged; net 4 lines shorter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 24, 2026 02:14
@github-actions github-actions Bot added the ♻️ Refactor code Refactor code. label Jul 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread packages/unxts.interop.xarray/src/unxts/interop/xarray/_src/conversion.py Outdated
Comment thread packages/unxts.interop.xarray/tests/test_unknown_unit_keys.py Outdated
Address PR GalacticDynamics#794 review:
- Sort the unknown-key list in the ValueError. `units` insertion order follows
  the accessor's `set(...)` iteration, which is not stable across processes, so
  the message was non-deterministic with multiple bad keys.
- Assert the coordinate unit in test_dataarray_typo_coord_key_raises. Switched
  to a non-dimension coordinate because a dimension coordinate is pandas-indexed
  by xarray and drops its unit (a separate, deferred limitation); the non-dim
  coord verifies a valid coord name actually quantifies.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 24, 2026 02:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@nstarman
nstarman merged commit f006bdc into GalacticDynamics:main Jul 24, 2026
40 checks passed
@nstarman
nstarman deleted the fix-xarray-quantify branch July 24, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 Fix a bug Fix a bug. ♻️ Refactor code Refactor code. 🧩 unxts-interop-xarray Issues/PRs affecting the unxts.interop.xarray namespace package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants