Skip to content

Stabilize NWIS Tests and Improve 5xx Error Handling#223

Open
thodson-usgs wants to merge 8 commits intoDOI-USGS:mainfrom
thodson-usgs:ci-fix
Open

Stabilize NWIS Tests and Improve 5xx Error Handling#223
thodson-usgs wants to merge 8 commits intoDOI-USGS:mainfrom
thodson-usgs:ci-fix

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

@thodson-usgs thodson-usgs commented Apr 3, 2026

This PR addresses several recent CI failures by stabilizing the NWIS test suite and improving HTTP error handling. It also makes a few other behind-the-scenes improvements to the waterdata module.

Key Changes:

  1. Improving Error Handling: Updated utils.py to better handle 500, 502, and 503 HTTP status codes from USGS services, which have been causing transient CI failures.
  2. Internal Refactoring: Fixed regressions in NWIS_Metadata.variable_info and get_record to ensure consistent and informative behavior during service deprecation.
  3. Efficient Pagination: Refactored _walk_pages in waterdata/utils.py to use list-based aggregation, reducing memory copying overhead from $O(n^2)$ to $O(n)$.
  4. Centralized Parameter Handling: Introduced a shared _get_args helper and refactored all 11 API functions in waterdata/api.py to use it.
  5. Utility Optimization: Enhanced to_str in utils.py with map(str, ...) and broader iterable support (sets, tuples, generators).
  6. Improved Testing: Added waterdata_utils_test.py and expanded tests/utils_test.py.

@thodson-usgs thodson-usgs requested a review from Copilot April 3, 2026 18:25
@thodson-usgs thodson-usgs changed the title Deprecate defunct NWIS functions and update tests Stabilize NWIS Tests and Improve 5xx Error Handling Apr 3, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 continues the NWIS deprecation work by ensuring defunct NWIS entry points fail in a controlled way, updating tests to reflect the deprecations (and reducing reliance on live NWIS responses for some cases), and improving handling of transient NWIS 5xx/HTML error responses.

Changes:

  • Updated tests/nwis_test.py to use mocked JSON fixtures for IV parsing/empty responses and to assert defunct NWIS functions/services raise errors.
  • Added JSON fixtures to support mocked NWIS IV responses in tests.
  • Improved utils.query() handling for 5xx responses and added HTML-instead-of-JSON detection in get_dv()/get_iv().

Reviewed changes

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

Show a summary per file
File Description
tests/nwis_test.py Switches some IV tests to mocked responses, adds defunct-service assertions, and adds a new live NWIS sanity check.
tests/data/nwis_iv_mock.json Adds a minimal JSON response fixture for IV parsing tests.
tests/data/nwis_iv_empty_mock.json Adds an empty timeSeries JSON fixture for empty-response behavior.
dataretrieval/utils.py Raises a clearer ValueError for HTTP 500/502/503 “service unavailable” responses.
dataretrieval/nwis.py Adds HTML-response detection for get_dv()/get_iv(), updates get_record docs/service validation, and deprecates NWIS_Metadata.variable_info.

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

@thodson-usgs
Copy link
Copy Markdown
Collaborator Author

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 5 out of 5 changed files in this pull request and generated 8 comments.


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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 6 out of 6 changed files in this pull request and generated 6 comments.


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

@thodson-usgs thodson-usgs self-assigned this Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants