Skip to content

refactor(wqp): collapse the 8 what_* bodies into a shared _what helper#320

Merged
thodson-usgs merged 1 commit into
DOI-USGS:mainfrom
thodson-usgs:refactor/wqp-what-dedup
Jun 8, 2026
Merged

refactor(wqp): collapse the 8 what_* bodies into a shared _what helper#320
thodson-usgs merged 1 commit into
DOI-USGS:mainfrom
thodson-usgs:refactor/wqp-what-dedup

Conversation

@thodson-usgs

Copy link
Copy Markdown
Collaborator

What

The eight what_* metadata-search functions in dataretrieval/wqp.py
(what_sites, what_organizations, what_projects, what_activities,
what_detection_limits, what_habitat_metrics, what_project_weights,
what_activity_metrics) had byte-identical bodies differing only by the
service name string and which URL builder they used. This factors that shared
body into one private _what() helper; each public function becomes a one-line
delegator.

def _what(service, *, ssl_check, legacy, **kwargs):
    kwargs = _check_kwargs(kwargs)
    if service in services_wqx3:          # single source of truth
        url = wqp_url(service) if legacy else wqx3_url(service)
    else:
        url = _legacy_only_url(service, legacy=legacy)
    response = query(url, payload=kwargs, delimiter=";", ssl_check=ssl_check)
    return _read_wqp_csv(response.text), WQP_Metadata(response, **kwargs)

The WQX3.0-capable services (Station, Activity) are derived from the
existing services_wqx3 constant rather than hardcoded per function, so the
list of WQX3-capable services lives in exactly one place.

Why

Pure structural duplication: ~9 identical lines × 8 functions. Consolidating
removes the copy-paste and makes future changes to the request/parse flow a
one-line edit instead of an eight-place edit.

Scope / non-goals

  • Public signatures unchanged — every what_* keeps its name, parameters,
    and full docstring (these render into the Sphinx API reference).
  • No behavior change. The only deliberate behavioral detail: the extra
    _what call frame shifts _warn_wqx3_unavailable's stacklevel by one, so
    it's bumped 3 → 4 to keep attributing the "WQX3.0 profile not available"
    warning to the public what_* call site.
  • Docstring-boilerplate dedup (the repeated API-reference preamble / Parameters
    / Returns blocks) is intentionally left out of this PR since it touches
    rendered public docs — happy to do it as a follow-up if wanted.

Net

dataretrieval/wqp.py: +48 / −74 (−26 LOC).

Verification

  • ruff format + ruff check (preview rules): clean
  • mypy --strict: clean
  • pytest tests/wqp_test.py: 15 passed (all what_* covered)
  • pytest tests/nwis_test.py tests/utils_test.py: 77 passed (NWIS_Metadata.site_infowqp.what_sites path exercised)

🤖 Generated with Claude Code

The what_sites/what_organizations/what_projects/what_activities/
what_detection_limits/what_habitat_metrics/what_project_weights/
what_activity_metrics functions had byte-identical bodies differing only
by the service name and which URL builder they used. Factor the shared
body (kwarg check -> URL resolution -> query -> CSV parse -> metadata)
into a private _what() helper; each public function becomes a one-line
delegator.

The WQX3.0-capable services are derived from the existing services_wqx3
constant (single source of truth) rather than hardcoded per function.

Public signatures, docstrings, and observable behavior are unchanged.
_warn_wqx3_unavailable's stacklevel is bumped 3 -> 4 to account for the
extra _what frame so the warning is still attributed to the public
what_* call site.

Net -26 LOC; no behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@thodson-usgs thodson-usgs marked this pull request as ready for review June 8, 2026 15:11
@thodson-usgs thodson-usgs merged commit be0f072 into DOI-USGS:main Jun 8, 2026
9 checks passed
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