Skip to content

Commit 99d7598

Browse files
thodson-usgsclaude
andcommitted
Polish wqp.py: silence prints, fix exception shape, fix typos
- 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>
1 parent 51ac674 commit 99d7598

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

dataretrieval/wqp.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ def what_organizations(
258258
if legacy is True:
259259
url = wqp_url("Organization")
260260
else:
261-
print("WQX3.0 profile not available, returning legacy profile.")
261+
warnings.warn(
262+
"WQX3.0 profile not available, returning legacy profile.",
263+
UserWarning,
264+
stacklevel=2,
265+
)
262266
url = wqp_url("Organization")
263267

264268
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
@@ -309,7 +313,11 @@ def what_projects(ssl_check=True, legacy=True, **kwargs):
309313
if legacy is True:
310314
url = wqp_url("Project")
311315
else:
312-
print("WQX3.0 profile not available, returning legacy profile.")
316+
warnings.warn(
317+
"WQX3.0 profile not available, returning legacy profile.",
318+
UserWarning,
319+
stacklevel=2,
320+
)
313321
url = wqp_url("Project")
314322

315323
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
@@ -435,7 +443,11 @@ def what_detection_limits(
435443
if legacy is True:
436444
url = wqp_url("ResultDetectionQuantitationLimit")
437445
else:
438-
print("WQX3.0 profile not available, returning legacy profile.")
446+
warnings.warn(
447+
"WQX3.0 profile not available, returning legacy profile.",
448+
UserWarning,
449+
stacklevel=2,
450+
)
439451
url = wqp_url("ResultDetectionQuantitationLimit")
440452

441453
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
@@ -490,7 +502,11 @@ def what_habitat_metrics(
490502
if legacy is True:
491503
url = wqp_url("BiologicalMetric")
492504
else:
493-
print("WQX3.0 profile not available, returning legacy profile.")
505+
warnings.warn(
506+
"WQX3.0 profile not available, returning legacy profile.",
507+
UserWarning,
508+
stacklevel=2,
509+
)
494510
url = wqp_url("BiologicalMetric")
495511

496512
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
@@ -516,7 +532,7 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
516532
ssl_check : bool
517533
Check the SSL certificate. Default is True.
518534
legacy : bool
519-
Retrun the legacy WQX data profile. Default is True.
535+
Return the legacy WQX data profile. Default is True.
520536
**kwargs : optional
521537
Accepts the same parameters as :obj:`dataretrieval.wqp.get_results`
522538
@@ -546,7 +562,11 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
546562
if legacy is True:
547563
url = wqp_url("ProjectMonitoringLocationWeighting")
548564
else:
549-
print("WQX3.0 profile not available, returning legacy profile.")
565+
warnings.warn(
566+
"WQX3.0 profile not available, returning legacy profile.",
567+
UserWarning,
568+
stacklevel=2,
569+
)
550570
url = wqp_url("ProjectMonitoringLocationWeighting")
551571

552572
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
@@ -602,12 +622,16 @@ def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):
602622
if legacy is True:
603623
url = wqp_url("ActivityMetric")
604624
else:
605-
print("WQX3.0 profile not available, returning legacy profile.")
625+
warnings.warn(
626+
"WQX3.0 profile not available, returning legacy profile.",
627+
UserWarning,
628+
stacklevel=2,
629+
)
606630
url = wqp_url("ActivityMetric")
607631

608632
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
609633

610-
df = pd.read_csv(StringIO(response.text), delimiter=",")
634+
df = pd.read_csv(StringIO(response.text), delimiter=",", low_memory=False)
611635

612636
return df, WQP_Metadata(response)
613637

@@ -619,9 +643,8 @@ def wqp_url(service):
619643
_warn_legacy_use()
620644

621645
if service not in services_legacy:
622-
raise TypeError(
623-
"Legacy service not recognized. Valid options are",
624-
f"{services_legacy}.",
646+
raise ValueError(
647+
f"Legacy service not recognized. Valid options are {services_legacy}."
625648
)
626649

627650
return f"{base_url}{service}/Search?"
@@ -634,9 +657,8 @@ def wqx3_url(service):
634657
_warn_wqx3_use()
635658

636659
if service not in services_wqx3:
637-
raise TypeError(
638-
"WQX3.0 service not recognized. Valid options are",
639-
f"{services_wqx3}.",
660+
raise ValueError(
661+
f"WQX3.0 service not recognized. Valid options are {services_wqx3}."
640662
)
641663

642664
return f"{base_url}{service}/search?"
@@ -708,7 +730,7 @@ def _check_kwargs(kwargs):
708730
def _warn_wqx3_use():
709731
message = (
710732
"Support for the WQX3.0 profiles is experimental. "
711-
"Queries may be slow or fail intermitttently."
733+
"Queries may be slow or fail intermittently."
712734
)
713735
warnings.warn(message, UserWarning, stacklevel=2)
714736

tests/wqp_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
what_project_weights,
1515
what_projects,
1616
what_sites,
17+
wqp_url,
18+
wqx3_url,
1719
)
1820

1921

@@ -216,3 +218,28 @@ def test_check_kwargs():
216218
kwargs = {"mimeType": "foo"}
217219
with pytest.raises(ValueError):
218220
kwargs = _check_kwargs(kwargs)
221+
222+
223+
def test_wqp_url_unknown_service_raises_value_error():
224+
"""Unknown legacy service should raise ValueError with a single readable message."""
225+
with pytest.raises(ValueError, match="Legacy service not recognized"):
226+
wqp_url("NotAService")
227+
228+
229+
def test_wqx3_url_unknown_service_raises_value_error():
230+
"""Unknown WQX3.0 service should raise ValueError with a single readable message."""
231+
with pytest.raises(ValueError, match="WQX3.0 service not recognized"):
232+
wqx3_url("NotAService")
233+
234+
235+
def test_what_organizations_legacy_false_warns(requests_mock):
236+
"""legacy=False on what_organizations should emit a warning, not print to stdout."""
237+
request_url = (
238+
"https://www.waterqualitydata.us/data/Organization/Search?statecode=US%3A34"
239+
"&characteristicName=Chloride&mimeType=csv"
240+
)
241+
mock_request(requests_mock, request_url, "tests/data/wqp_organizations.txt")
242+
with pytest.warns(UserWarning, match="WQX3.0 profile not available"):
243+
what_organizations(
244+
statecode="US:34", characteristicName="Chloride", legacy=False
245+
)

0 commit comments

Comments
 (0)