Commit c755f6b
authored
Validate monitoring_location_id format in waterdata functions (#229)
Validate and normalize monitoring_location_id (and other multi-value filters) at the boundary
Closes #188.
Catches a class of bug that previously produced silent zero-result responses
or confusing JSONDecodeErrors when callers passed malformed
`monitoring_location_id` values to the WaterData OGC API getters.
Behavior changes (every public waterdata function accepting
`monitoring_location_id`):
- `monitoring_location_id` is now validated client-side. Non-string,
non-iterable inputs (`int`, `dict`, …) raise `TypeError`; strings that
don't match the AGENCY-ID hyphen-separated form (e.g. `"USGS-01646500"`)
raise `ValueError`; non-string elements inside an iterable raise
`TypeError`. A clear "Expected 'AGENCY-ID' format, e.g.
'USGS-01646500'" hint is appended in every error.
- Other multi-value string filters (`parameter_code`, `statistic_id`,
`agency_code`, etc.) now accept any non-string iterable of strings —
`list`, `tuple`, `pandas.Series`, `pandas.Index`, `numpy.ndarray`, and
generators are all materialized to `list[str]` before the URL is built.
A bare single string is still accepted unchanged.
- `properties` additionally accepts a single string and wraps it into a
one-element list, since `",".join(...)` would otherwise iterate it as
characters.
- `list[int]` filters on `get_peaks` (`water_year`, `year`, `month`,
`day`, `peak_since`) and `list[float]` on `get_combined_metadata`
(`thresholds`) pass through untouched.
- `_format_api_dates` now rejects `Mapping` inputs (which previously
silently materialized as the keys list) and `None` is short-circuited
up front.
Public API:
- Type annotations widened to `str | Iterable[str] | None` (or
`str | list[str] | None` where only a list is meaningful) across all
affected functions. Numpydoc parameter descriptions updated from "list
of strings" to "iterable of strings" to match.
- Coverage verified on all 15 public functions that accept
`monitoring_location_id`: 12 in `waterdata/api.py` (via centralized
`_get_args`), `get_ratings` in `waterdata/ratings.py` (direct call),
and `get_nearest_continuous` in `waterdata/nearest.py` (transitively
via `get_continuous`).
Internals:
- `_normalize_str_iterable` — one O(N) walk validates element types and
materializes to `list`. Generic, used by every string-filter param.
- `_check_monitoring_location_id` — composes `_normalize_str_iterable`
with per-element `_check_id_format` (regex `[^-\s]+-[^-\s]+`, fullmatch).
- `_get_args` — single dispatch point that runs the right normalizer per
param name. New `get_*` functions inherit validation automatically.
- `_DATE_RANGE_PARAMS` — shared constant covering `time`, `datetime`,
`last_modified`, `begin`, `begin_utc`, `end`, `end_utc`; used by both
`_construct_api_requests`'s date formatting and `_get_args`'s bypass.
Tests:
- Live-verified against the USGS OGC + STAC APIs with ~70 stress cases
covering every iterable shape, every public function with mloc, and
every rejection path.
- 100+ unit tests including regressions for: int-list parameter rejection,
Series-of-IDs materialization, Mapping rejection on date inputs,
AGENCY-ID format edge cases (trailing/leading hyphen, embedded comma,
whitespace, multi-hyphen).1 parent da49749 commit c755f6b
6 files changed
Lines changed: 815 additions & 456 deletions
File tree
- dataretrieval/waterdata
- tests
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
22 | | - | |
| 22 | + | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
48 | 49 | | |
49 | | - | |
| 50 | + | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
26 | 32 | | |
27 | 33 | | |
28 | 34 | | |
| |||
33 | 39 | | |
34 | 40 | | |
35 | 41 | | |
36 | | - | |
| 42 | + | |
37 | 43 | | |
38 | 44 | | |
39 | 45 | | |
| |||
62 | 68 | | |
63 | 69 | | |
64 | 70 | | |
65 | | - | |
| 71 | + | |
66 | 72 | | |
67 | 73 | | |
68 | 74 | | |
| |||
142 | 148 | | |
143 | 149 | | |
144 | 150 | | |
| 151 | + | |
145 | 152 | | |
146 | 153 | | |
147 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
143 | 144 | | |
144 | 145 | | |
145 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
146 | 156 | | |
147 | 157 | | |
148 | 158 | | |
| |||
223 | 233 | | |
224 | 234 | | |
225 | 235 | | |
| 236 | + | |
| 237 | + | |
226 | 238 | | |
227 | 239 | | |
228 | 240 | | |
229 | 241 | | |
230 | 242 | | |
231 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
232 | 254 | | |
233 | 255 | | |
234 | 256 | | |
| |||
429 | 451 | | |
430 | 452 | | |
431 | 453 | | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | 454 | | |
436 | 455 | | |
437 | 456 | | |
438 | 457 | | |
439 | | - | |
| 458 | + | |
440 | 459 | | |
441 | 460 | | |
442 | 461 | | |
| |||
452 | 471 | | |
453 | 472 | | |
454 | 473 | | |
455 | | - | |
456 | | - | |
| 474 | + | |
457 | 475 | | |
458 | 476 | | |
459 | 477 | | |
460 | 478 | | |
461 | | - | |
462 | | - | |
463 | | - | |
| 479 | + | |
| 480 | + | |
464 | 481 | | |
465 | 482 | | |
466 | 483 | | |
| |||
1168 | 1185 | | |
1169 | 1186 | | |
1170 | 1187 | | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
1171 | 1311 | | |
1172 | 1312 | | |
1173 | 1313 | | |
| |||
1194 | 1334 | | |
1195 | 1335 | | |
1196 | 1336 | | |
1197 | | - | |
1198 | | - | |
1199 | | - | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
0 commit comments