Polish wqp.py: silence prints, fix exception shape, fix typos#256
Merged
thodson-usgs merged 8 commits intoDOI-USGS:mainfrom May 4, 2026
Merged
Polish wqp.py: silence prints, fix exception shape, fix typos#256thodson-usgs merged 8 commits intoDOI-USGS:mainfrom
thodson-usgs merged 8 commits intoDOI-USGS:mainfrom
Conversation
- 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) <[email protected]>
- 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) <[email protected]>
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) <[email protected]>
This was referenced May 4, 2026
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR polishes the WQP helper module by replacing stdout print side effects with warnings, correcting exception types/messages for invalid service names, and making minor consistency/typo fixes.
Changes:
- Replaced
print(...)statements in severalwhat_*helpers with a centralized_warn_wqx3_unavailable()warning helper and simplified redundant branching. - Fixed
wqp_url/wqx3_urlto raise a single-stringValueError(instead of tuple-argTypeError) for unrecognized services. - Added
low_memory=Falsetowhat_activity_metricsCSV parsing and corrected typos in docstrings/warning text.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
dataretrieval/wqp.py |
Replaces prints with warnings, fixes exception shape/type, aligns read_csv args, and corrects typos. |
tests/wqp_test.py |
Adds a regression test asserting legacy=False emits a warning for what_organizations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+713
to
+718
| def _warn_wqx3_unavailable(): | ||
| warnings.warn( | ||
| "WQX3.0 profile not available, returning legacy profile.", | ||
| UserWarning, | ||
| stacklevel=3, | ||
| ) |
Comment on lines
+228
to
+231
| with pytest.warns(UserWarning, match="WQX3.0 profile not available"): | ||
| what_organizations( | ||
| statecode="US:34", characteristicName="Chloride", legacy=False | ||
| ) |
Per copilot review on PR DOI-USGS#256: the six WQP helpers without WQX3.0 equivalents (what_organizations, what_projects, what_detection_limits, what_habitat_metrics, what_project_weights, what_activity_metrics) emit both _warn_wqx3_unavailable() (UserWarning) and _warn_legacy_use() (DeprecationWarning) when legacy=False is passed. The DeprecationWarning's text says 'Setting legacy=False will remove this warning' — a lie for endpoints that have no WQX3 alternative. Introduce a _legacy_only_url() helper that emits the WQX3-unavailable warning and suppresses the redundant legacy DeprecationWarning when legacy=False. Use it in all six helpers. Update the test to use catch_warnings(record=True) so it asserts both that the UserWarning fires AND that the misleading DeprecationWarning is suppressed. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
# Conflicts: # tests/wqp_test.py
Per /simplify review on PR DOI-USGS#256: - Unify the two `wqp_url(service)` returns in `_legacy_only_url` into a single return inside `warnings.catch_warnings()`, gating the warn and filter on `not legacy`. - Document the asymmetric `stacklevel=3` in `_warn_wqx3_unavailable`. - Hoist `import warnings` from inside the new regression test to the module-level imports in `tests/wqp_test.py`. - Compress the regression test's docstring. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
The legacy-only warning + DeprecationWarning suppression is small and the diff is self-documenting; a regression would be loud at the API surface (users would see the misleading "set legacy=False to remove this warning" message while already setting legacy=False). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.
Summary
A handful of small, discrete polish fixes in
dataretrieval/wqp.py:what_organizations,what_projects,what_detection_limits,what_habitat_metrics,what_project_weights,what_activity_metrics) hadprint("WQX3.0 profile not available, returning legacy profile.")statements that fire wheneverlegacy=Falseis passed. Library code shouldn'tprintto stdout; converted to a single_warn_wqx3_unavailable()helper (alongside the existing_warn_wqx3_use()/_warn_legacy_use()helpers) so callers can filter, redirect, or capture them viawarnings/pytest.warns. Also collapses the redundantif legacy is True: url = X else: warn(); url = Xbranches (both arms assigned the same legacy URL). The new_legacy_only_urlhelper additionally suppresses the legacyDeprecationWarningthatwqp_urlwould otherwise raise — its message claims settinglegacy=Falseremoves it, which is a lie for endpoints that have no WQX3.0 alternative.wqp_url/wqx3_url:TypeError("... Valid options are", f"{services}.")produced an exception whoseargsis a 2-tuple of strings, breaking the message rendering and using the wrong exception class (ValueErroris the right type for an unrecognized argument value). Replaced with a singleValueErrorcarrying one f-string.what_activity_metricswas the onlyread_csvsite in this module withoutlow_memory=False. Added it for parity with the other eight helpers.Retrun->Return(docstring);intermitttently->intermittently(warning text).Test plan
Related PRs
Other open PRs in this bug-review series that touch
dataretrieval/wqp.py(different functions, no functional conflicts):WQP_Metadata.site_infoproperty binding.dataProfileinget_results. An earlier draft of this PR also touchedget_results'sTypeError->ValueErrorshape; that overlap was reverted so Fix get_results: preserve user-supplied WQX3.0 dataProfile #250 fully owns theget_resultsrewrite.🤖 Generated with Claude Code