nldi: fix several user-visible bugs in get_features, get_flowlines, search#252
nldi: fix several user-visible bugs in get_features, get_flowlines, search#252thodson-usgs wants to merge 2 commits intoDOI-USGS:mainfrom
Conversation
…earch Bundles six discrete fixes to the NLDI module: * `search(find='flowlines')` without `navigation_mode` crashed with `AttributeError: 'NoneType' object has no attribute 'upper'` from inside `_validate_navigation_mode`. `_validate_navigation_mode` now treats `None` as a clean `ValueError`, returns the upper-cased value (callers use the normalized form), and raises `ValueError` rather than `TypeError` for invalid modes — these are bad values, not bad types. `search` also surfaces a clear error before delegating to `get_flowlines`. * `get_flowlines(comid=...)` silently dropped the `trim_start` argument: the `comid` branch built `query_params` without `trimStart`. Move the shared params out of the branch. * `get_features(lat=0.0, ...)` and `search(lat=0.0, ...)` treated the equator/prime-meridian as missing because the code used `if lat:` / `if comid:` truthiness checks. Switch to `is None` checks throughout so coordinates of exactly zero are accepted and `comid=0` is no longer conflated with "no comid". * The error message produced by `get_features` for feature-source + data-source paths had an unbalanced quote (`"feature_id 'USGS-X, and data source ..."`). Closing quote restored via a small `_features_err_msg` helper that the two branches share. * The unconditional `_validate_data_source(feature_source)` in `get_features` is now guarded by `if feature_source:` to avoid validating `None` once the cache-bug fix (DOI-USGS#246) lands. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR applies a set of small, user-visible bug fixes in the nldi module (navigation-mode validation, query parameter handling, and “falsy” input handling) and adds regression tests to prevent reintroductions.
Changes:
- Normalize and harden
navigation_modevalidation (handleNone, return normalized uppercase value, raiseValueErrorfor bad values). - Fix
get_flowlines(comid=...)to includetrimStartconsistently (and refactor shared query params). - Fix “falsy” origin handling (accept
lat=0.0/long=0.0/comid=0) and unifyget_featureserror message formatting; add regression tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
dataretrieval/nldi.py |
Fixes navigation-mode validation/normalization, preserves trimStart for COMID flowline requests, corrects falsy checks for coordinates/COMIDs, and consolidates feature error messages. |
tests/nldi_test.py |
Adds regression coverage for the fixed crashes/parameter drops/falsy handling and the corrected error message formatting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| get_flowlines(navigation_mode="UM", comid=13294314, trim_start=True) | ||
|
|
||
| sent = requests_mock.request_history[-1] | ||
| assert sent.qs.get("trimstart") == ["true"] |
|
@copilot-pull-request-reviewer thanks for the review. Quick note on the one inline comment: Declining: |
# Conflicts: # dataretrieval/nldi.py
Summary
Six discrete fixes to the NLDI module, bundled because they all touch the same file and individually each is small.
search(find="flowlines")withoutnavigation_modecrashed withAttributeError: 'NoneType' object has no attribute 'upper'from inside_validate_navigation_mode._validate_navigation_modenow treatsNoneas a cleanValueError, returns the upper-cased value (so callers use the normalized form rather than silently sending lowercase), and raisesValueErrorrather thanTypeErrorfor invalid modes — these are bad values, not bad types.searchalso surfaces a clear error before delegating toget_flowlines.get_flowlines(comid=...)silently droppedtrim_start: thecomidbranch builtquery_paramswithouttrimStart. The shared params now live outside the branch.lat=0.0/long=0.0/comid=0were treated as missing because the code usedif lat:/if comid:truthiness. Switched tois Nonechecks throughout so the equator/prime meridian / COMID 0 are accepted.get_featureserror message had an unbalanced quote (feature_id 'USGS-X, and data source ...). Closing quote restored via a small_features_err_msghelper that the two branches share._validate_data_source(feature_source)inget_featuresis now guarded byif feature_source:so it doesn't validateNoneonce Fix nldi: validate data source on every call, not just cache-miss #246's cache fix lands.Test plan
tests/nldi_test.py:search(find='flowlines')without nav-mode raises ValueError;_validate_navigation_moderaises ValueError on bad input;_validate_navigation_modenormalizes lowercase to uppercase;get_flowlinesby comid sendstrimStart;get_features(lat=0, long=0)is accepted;lat=0withoutlongraises; error message has balanced quotes.API_USGS_PATset).Out of scope
The
distancedefault mismatch betweenget_flowlines(5km) andsearch(distance=50)is a behavioral debate (which default should win, or shouldsearchsimply pass through when unspecified) and worth a separate, focused PR if desired.Related PRs
nldi-data-source-validation) fixes a separate bug in_validate_data_source's cache check and adds the sameif feature_source:guard at one line inget_features. Trivial textual conflict at merge time; identical fix so either can land first.nldi-navigation-mode-valueerror) is a strict subset of this PR (just theTypeError->ValueErrorchange in_validate_navigation_mode). It can be closed if this PR merges.