Skip to content

Commit 8f2de13

Browse files
thodson-usgsclaude
andcommitted
Tighten _legacy_only_url and clean up test imports
Per /simplify review on PR #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) <noreply@anthropic.com>
1 parent 44794b4 commit 8f2de13

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

dataretrieval/wqp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ def _warn_legacy_use():
691691

692692

693693
def _warn_wqx3_unavailable():
694+
# stacklevel=3: warn -> _warn_wqx3_unavailable -> _legacy_only_url -> what_*
694695
warnings.warn(
695696
"WQX3.0 profile not available, returning legacy profile.",
696697
UserWarning,
@@ -707,9 +708,8 @@ def _legacy_only_url(service: str, legacy: bool) -> str:
707708
message claims setting ``legacy=False`` removes the warning, which is
708709
a lie for endpoints that have no WQX3.0 alternative.
709710
"""
710-
if not legacy:
711-
_warn_wqx3_unavailable()
712-
with warnings.catch_warnings():
711+
with warnings.catch_warnings():
712+
if not legacy:
713+
_warn_wqx3_unavailable()
713714
warnings.simplefilter("ignore", DeprecationWarning)
714-
return wqp_url(service)
715-
return wqp_url(service)
715+
return wqp_url(service)

tests/wqp_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import warnings
23

34
import pytest
45
from pandas import DataFrame
@@ -219,13 +220,8 @@ def test_check_kwargs():
219220

220221

221222
def test_what_organizations_legacy_false_warns(requests_mock):
222-
"""legacy=False on a legacy-only helper:
223-
- emits the WQX3.0-unavailable UserWarning
224-
- suppresses the redundant legacy DeprecationWarning, whose text would
225-
misleadingly tell the user that setting legacy=False removes it.
226-
"""
227-
import warnings
228-
223+
"""legacy=False on a legacy-only helper warns and suppresses the
224+
misleading legacy DeprecationWarning."""
229225
request_url = (
230226
"https://www.waterqualitydata.us/data/Organization/Search?statecode=US%3A34"
231227
"&characteristicName=Chloride&mimeType=csv"

0 commit comments

Comments
 (0)