Fix invalid z_unit error to list accepted unit names#2870
Merged
Conversation
The geodesic z_unit check in aspect() and slope() built its error message from sorted(set(Z_UNITS.values())), which are the numeric conversion factors, not the unit strings a user can pass. The message told users their unit must be one of [0.3048, 1.0, 1000.0, 1609.344], which is not actionable. Report sorted(Z_UNITS) (the keys) instead, so the message lists the valid unit names. Extend the invalid-z_unit tests in both modules to assert the message contains the accepted names and not bare factors.
brendancol
commented
Jun 2, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
PR Review: Fix invalid z_unit error to list accepted unit names
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
None.
Nits (optional improvements)
- The test guards against the old factors by name (
assert "0.3048" not in msg). That works. A stricter version would reject any bare float, e.g.assert not re.search(r"\b\d+\.\d+", msg), so a future regression that swaps in different factors still trips it. Optional; the current asserts already fail against the old code.
What looks good
- Root cause is right.
Z_UNITSkeys are the unit names users pass; the values are numeric factors.sorted(Z_UNITS)reports the keys, which is what the message should say. - The same bug was in
aspect.py:448andslope.py:415. Fixing both keeps the two messages in sync. sorted(Z_UNITS)sorts the keys lexically, so the message order is stable across runs.- This runs in the validation path before backend dispatch, so there's no numpy/cupy/dask/dask-cupy parity question.
- Tests check both the presence of accepted names and the absence of the old factors, and they pass.
Checklist
- Algorithm matches reference: n/a (error-message fix)
- All implemented backends consistent: n/a (pre-dispatch validation)
- NaN handling correct: n/a
- Edge cases covered by tests: yes (invalid unit string)
- Dask chunk boundaries handled: n/a
- No premature materialization: n/a
- Benchmark exists or not needed: not needed
- README feature matrix updated: not needed (no API change)
- Docstrings present and accurate: unchanged
Replace the name-specific factor checks (0.3048, 1609.344) with a regex that rejects any bare decimal in the message, so a future regression that swaps in different conversion factors still trips the test.
brendancol
commented
Jun 2, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
Follow-up review (after nit fix)
The earlier nit is addressed: both test_invalid_z_unit_raises tests now assert not re.search(r"\d+\.\d+", msg), so any bare decimal in the message fails the test rather than just the two old factors. import re was added to both test files. Tests pass.
No new blockers, suggestions, or nits. The remaining F841 flake8 warning on the unused flat variable in test_geodesic_aspect.py predates this PR and sits outside the changed lines, so it is left alone.
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.
Closes #2858
What changed
z_unitcheck inaspect()andslope()built its error message fromsorted(set(Z_UNITS.values())), which are the numeric conversion factors (0.3048,1.0,1000.0,1609.344), not the unit names a user can pass. It now reportssorted(Z_UNITS)so the message lists the accepted unit names.aspect.pyandslope.pysince they share the same check and message format.Before:
After:
Backend coverage
This is a validation-path change that runs before any backend dispatch, so it applies to all four backends (numpy / cupy / dask+numpy / dask+cupy).
Test plan
test_invalid_z_unit_raisesintest_geodesic_aspect.pyandtest_geodesic_slope.pyto assert the message contains the accepted unit names and not bare numeric factors.pytestpasses for both updated tests.