Skip to content

Polish wqp.py: silence prints, fix exception shape, fix typos#256

Draft
thodson-usgs wants to merge 3 commits intoDOI-USGS:mainfrom
thodson-usgs:fix/wqp-polish
Draft

Polish wqp.py: silence prints, fix exception shape, fix typos#256
thodson-usgs wants to merge 3 commits intoDOI-USGS:mainfrom
thodson-usgs:fix/wqp-polish

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

Summary

A handful of small, discrete polish fixes in dataretrieval/wqp.py:

  • Silence stdout: Six WQP helpers (what_organizations, what_projects, what_detection_limits, what_habitat_metrics, what_project_weights, what_activity_metrics) had print(\"WQX3.0 profile not available, returning legacy profile.\") statements that fire whenever legacy=False is passed. Library code shouldn't print to stdout; converted to warnings.warn(..., UserWarning, stacklevel=2) so callers can filter, redirect, or capture them via warnings/pytest.warns.
  • Fix exception shape in wqp_url/wqx3_url: TypeError(\"... Valid options are\", f\"{services}.\") produced an exception whose args is a 2-tuple of strings, breaking the message rendering and using the wrong exception class (ValueError is the right type for an unrecognized argument value). Replaced with a single ValueError carrying one f-string.
  • Consistency: what_activity_metrics was the only read_csv site in this module without low_memory=False. Added it for parity with the other eight helpers.
  • Typos: Retrun -> Return (docstring); intermitttently -> intermittently (warning text).

Test plan

  • Existing 14 wqp tests still pass.
  • New tests:
    • test_wqp_url_unknown_service_raises_value_error — confirms ValueError (not TypeError) with single readable message.
    • test_wqx3_url_unknown_service_raises_value_error — same for WQX3.0 helper.
    • test_what_organizations_legacy_false_warns — confirms legacy=False emits a UserWarning (not stdout print).
  • Full suite (excluding deprecated nwis_test.py): 199 passed.

🤖 Generated with Claude Code

thodson-usgs and others added 3 commits May 3, 2026 18:44
- Replace 6 stdout `print(...)` calls in `what_organizations`,
  `what_projects`, `what_detection_limits`, `what_habitat_metrics`,
  `what_project_weights`, and `what_activity_metrics` with
  `warnings.warn(..., UserWarning)` so callers can capture or filter
  the message instead of getting unconditional stdout pollution.
- Convert `wqp_url`/`wqx3_url` from `TypeError(msg, msg)` (which raises
  with `args=(msg1, msg2)` and is the wrong type for an unrecognized
  argument) to `ValueError(single_msg)`.
- Add `low_memory=False` to `what_activity_metrics` `read_csv` for
  consistency with the other 8 helpers.
- Fix typos: `Retrun` -> `Return`, `intermitttently` -> `intermittently`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Extract `_warn_wqx3_unavailable()` next to `_warn_wqx3_use()` and
  `_warn_legacy_use()`. Replaces 6 copies of an identical 4-line
  `warnings.warn(...)` block with a single helper call. `stacklevel=3`
  preserves the original `stacklevel=2` semantics through the extra
  helper frame.
- Collapse `if legacy is True: url = ... else: warn(); url = ...`
  branches in 6 helpers — both arms assigned the same legacy URL — to
  `if not legacy: warn()` followed by a single assignment.
- Apply the same `TypeError(msg, msg)` -> `ValueError(single_msg)` fix
  to `get_results`'s two `dataProfile` validators (the same broken
  pattern this PR already fixed in `wqp_url`/`wqx3_url`); also fixes
  the missing space in the joined "WQX3.0profile" message.
- Add 2 regression tests for the `get_results` error paths.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The simplify pass added `TypeError(msg, msg)` -> `ValueError(single_msg)`
fixes to `get_results`'s two `dataProfile` validators. PR DOI-USGS#250 ("Fix
get_results: preserve user-supplied WQX3.0 dataProfile") restructures
the same code more thoroughly and addresses the same exception-shape
bug as a side effect, so this PR should not duplicate that work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant