Skip to content

Commit 1618f6b

Browse files
thodson-usgsclaude
andcommitted
Raise on empty targets instead of issuing a no-op request
Calling ``get_nearest_continuous`` with an empty ``targets`` is almost always a caller bug (an unfiltered frame, a typo, a mis-named column). The previous code papered over it by firing a trivial-range HTTP request (``time=1900-01-01/1900-01-01``) purely so the caller received a real ``BaseMetadata`` object. That pattern wastes a round-trip on a nonsensical input and hides the bug. Raise ``ValueError`` on empty ``targets`` instead. Shrinks the body and makes a caller mistake loud. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2613792 commit 1618f6b

2 files changed

Lines changed: 8 additions & 20 deletions

File tree

dataretrieval/waterdata/api.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,7 @@ def get_nearest_continuous(
565565
window_td = pd.Timedelta(window)
566566

567567
if len(targets) == 0:
568-
# Issue a trivial-range request so the caller still receives a
569-
# real ``BaseMetadata``; return an empty frame with the same
570-
# shape a real response would have.
571-
df, md = get_continuous(
572-
monitoring_location_id=monitoring_location_id,
573-
parameter_code=parameter_code,
574-
time="1900-01-01T00:00:00Z/1900-01-01T00:00:00Z",
575-
**kwargs,
576-
)
577-
return _empty_nearest_result(df), md
568+
raise ValueError("targets must contain at least one timestamp")
578569

579570
filter_expr = _build_window_or_filter(targets, window_td)
580571
df, md = get_continuous(

tests/waterdata_nearest_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,13 @@ def test_multi_site_returns_row_per_target_per_site(patch_get_continuous):
171171
assert set(result["monitoring_location_id"]) == {"USGS-1", "USGS-2"}
172172

173173

174-
def test_empty_targets_returns_empty_frame_without_building_filter(
175-
patch_get_continuous,
176-
):
177-
patch_get_continuous.return_value = (_fake_df([]), mock.Mock())
178-
result, _ = get_nearest_continuous([], monitoring_location_id="USGS-02238500")
179-
assert result.empty
180-
# The one call that happens uses a trivial time= window, not a filter.
181-
_, kwargs = patch_get_continuous.call_args
182-
assert "filter" not in kwargs
183-
assert "time" in kwargs
174+
def test_empty_targets_raises(patch_get_continuous):
175+
"""An empty ``targets`` is a call with no useful work to do and
176+
almost always a caller bug — raise rather than silently issuing a
177+
no-op HTTP request."""
178+
with pytest.raises(ValueError, match="targets"):
179+
get_nearest_continuous([], monitoring_location_id="USGS-02238500")
180+
patch_get_continuous.assert_not_called()
184181

185182

186183
def test_rejects_time_kwarg(patch_get_continuous):

0 commit comments

Comments
 (0)