Skip to content

Commit 8c86ebd

Browse files
thodson-usgsclaude
andcommitted
Apply simplify findings: extract _warn_wqx3_unavailable, fix get_results
- 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>
1 parent 99d7598 commit 8c86ebd

2 files changed

Lines changed: 44 additions & 60 deletions

File tree

dataretrieval/wqp.py

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def get_results(
131131
"dataProfile" in kwargs
132132
and kwargs["dataProfile"] not in result_profiles_legacy
133133
):
134-
raise TypeError(
135-
f"dataProfile {kwargs['dataProfile']} is not a legacy profile.",
136-
f"Valid options are {result_profiles_legacy}.",
134+
raise ValueError(
135+
f"dataProfile {kwargs['dataProfile']} is not a legacy profile. "
136+
f"Valid options are {result_profiles_legacy}."
137137
)
138138

139139
url = wqp_url("Result")
@@ -143,9 +143,9 @@ def get_results(
143143
"dataProfile" in kwargs
144144
and kwargs["dataProfile"] not in result_profiles_wqx3
145145
):
146-
raise TypeError(
147-
f"dataProfile {kwargs['dataProfile']} is not a valid WQX3.0"
148-
f"profile. Valid options are {result_profiles_wqx3}.",
146+
raise ValueError(
147+
f"dataProfile {kwargs['dataProfile']} is not a valid WQX3.0 "
148+
f"profile. Valid options are {result_profiles_wqx3}."
149149
)
150150
else:
151151
kwargs["dataProfile"] = "fullPhysChem"
@@ -255,15 +255,9 @@ def what_organizations(
255255

256256
kwargs = _check_kwargs(kwargs)
257257

258-
if legacy is True:
259-
url = wqp_url("Organization")
260-
else:
261-
warnings.warn(
262-
"WQX3.0 profile not available, returning legacy profile.",
263-
UserWarning,
264-
stacklevel=2,
265-
)
266-
url = wqp_url("Organization")
258+
if not legacy:
259+
_warn_wqx3_unavailable()
260+
url = wqp_url("Organization")
267261

268262
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
269263

@@ -310,15 +304,9 @@ def what_projects(ssl_check=True, legacy=True, **kwargs):
310304

311305
kwargs = _check_kwargs(kwargs)
312306

313-
if legacy is True:
314-
url = wqp_url("Project")
315-
else:
316-
warnings.warn(
317-
"WQX3.0 profile not available, returning legacy profile.",
318-
UserWarning,
319-
stacklevel=2,
320-
)
321-
url = wqp_url("Project")
307+
if not legacy:
308+
_warn_wqx3_unavailable()
309+
url = wqp_url("Project")
322310

323311
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
324312

@@ -440,15 +428,9 @@ def what_detection_limits(
440428

441429
kwargs = _check_kwargs(kwargs)
442430

443-
if legacy is True:
444-
url = wqp_url("ResultDetectionQuantitationLimit")
445-
else:
446-
warnings.warn(
447-
"WQX3.0 profile not available, returning legacy profile.",
448-
UserWarning,
449-
stacklevel=2,
450-
)
451-
url = wqp_url("ResultDetectionQuantitationLimit")
431+
if not legacy:
432+
_warn_wqx3_unavailable()
433+
url = wqp_url("ResultDetectionQuantitationLimit")
452434

453435
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
454436

@@ -499,15 +481,9 @@ def what_habitat_metrics(
499481

500482
kwargs = _check_kwargs(kwargs)
501483

502-
if legacy is True:
503-
url = wqp_url("BiologicalMetric")
504-
else:
505-
warnings.warn(
506-
"WQX3.0 profile not available, returning legacy profile.",
507-
UserWarning,
508-
stacklevel=2,
509-
)
510-
url = wqp_url("BiologicalMetric")
484+
if not legacy:
485+
_warn_wqx3_unavailable()
486+
url = wqp_url("BiologicalMetric")
511487

512488
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
513489

@@ -559,15 +535,9 @@ def what_project_weights(ssl_check=True, legacy=True, **kwargs):
559535

560536
kwargs = _check_kwargs(kwargs)
561537

562-
if legacy is True:
563-
url = wqp_url("ProjectMonitoringLocationWeighting")
564-
else:
565-
warnings.warn(
566-
"WQX3.0 profile not available, returning legacy profile.",
567-
UserWarning,
568-
stacklevel=2,
569-
)
570-
url = wqp_url("ProjectMonitoringLocationWeighting")
538+
if not legacy:
539+
_warn_wqx3_unavailable()
540+
url = wqp_url("ProjectMonitoringLocationWeighting")
571541

572542
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
573543

@@ -619,15 +589,9 @@ def what_activity_metrics(ssl_check=True, legacy=True, **kwargs):
619589

620590
kwargs = _check_kwargs(kwargs)
621591

622-
if legacy is True:
623-
url = wqp_url("ActivityMetric")
624-
else:
625-
warnings.warn(
626-
"WQX3.0 profile not available, returning legacy profile.",
627-
UserWarning,
628-
stacklevel=2,
629-
)
630-
url = wqp_url("ActivityMetric")
592+
if not legacy:
593+
_warn_wqx3_unavailable()
594+
url = wqp_url("ActivityMetric")
631595

632596
response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
633597

@@ -744,3 +708,11 @@ def _warn_legacy_use():
744708
"will remove this warning."
745709
)
746710
warnings.warn(message, DeprecationWarning, stacklevel=2)
711+
712+
713+
def _warn_wqx3_unavailable():
714+
warnings.warn(
715+
"WQX3.0 profile not available, returning legacy profile.",
716+
UserWarning,
717+
stacklevel=3,
718+
)

tests/wqp_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,15 @@ def test_what_organizations_legacy_false_warns(requests_mock):
243243
what_organizations(
244244
statecode="US:34", characteristicName="Chloride", legacy=False
245245
)
246+
247+
248+
def test_get_results_invalid_legacy_dataprofile_raises_value_error():
249+
"""Invalid legacy dataProfile -> ValueError with single readable message."""
250+
with pytest.raises(ValueError, match="is not a legacy profile"):
251+
get_results(dataProfile="not_a_real_profile")
252+
253+
254+
def test_get_results_invalid_wqx3_dataprofile_raises_value_error():
255+
"""Invalid WQX3.0 dataProfile -> ValueError with single readable message."""
256+
with pytest.raises(ValueError, match="is not a valid WQX3.0 profile"):
257+
get_results(legacy=False, dataProfile="not_a_real_profile")

0 commit comments

Comments
 (0)