From a28ed77bf2374acdfc7a2f101bd7373349369d73 Mon Sep 17 00:00:00 2001 From: thodson-usgs Date: Thu, 9 Apr 2026 11:04:30 -0500 Subject: [PATCH 1/2] Small cleanups: idiomatic Python and a precedence fix - waterdata/utils.py: replace runtime `assert` in `_check_ogc_requests` with an explicit `ValueError` (assertions can be disabled with `-O`). - waterdata/utils.py: fix operator-precedence bug in `_format_datetime` where `len==1 and re.search(...) or "/" in datetime_input[0]` would short-circuit and return `datetime_input[0]` for 2-element inputs whose first element contained "/". Parenthesize so both branches require `len==1`. - nwis.py: `"dec_lat_va" in list(df)` -> `in df.columns` (drop needless list construction). - nwis.py: replace index-based `for i in range(len(index_list)-1)` loop with `zip(index_list[:-1], index_list[1:])`. Co-Authored-By: Claude Opus 4.6 (1M context) --- dataretrieval/nwis.py | 6 ++---- dataretrieval/waterdata/utils.py | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dataretrieval/nwis.py b/dataretrieval/nwis.py index 6244798..ca477ae 100644 --- a/dataretrieval/nwis.py +++ b/dataretrieval/nwis.py @@ -96,7 +96,7 @@ def format_response( if service == "peaks": df = preformat_peaks_response(df) - if gpd is not None and "dec_lat_va" in list(df): + if gpd is not None and "dec_lat_va" in df.columns: geoms = gpd.points_from_xy(df.dec_long_va.values, df.dec_lat_va.values) df = gpd.GeoDataFrame(df, geometry=geoms, crs=_CRS) @@ -993,9 +993,7 @@ def _read_json(json): ) index_list.append(len(site_list)) - for i in range(len(index_list) - 1): - start = index_list[i] # [0] - end = index_list[i + 1] # [21] + for start, end in zip(index_list[:-1], index_list[1:]): # grab a block containing timeseries 0:21, # which are all from the same site diff --git a/dataretrieval/waterdata/utils.py b/dataretrieval/waterdata/utils.py index 4198e02..c7d5665 100644 --- a/dataretrieval/waterdata/utils.py +++ b/dataretrieval/waterdata/utils.py @@ -184,9 +184,8 @@ def _format_api_dates( if len(datetime_input) <= 2: # If the list is of length 1, first look for things like "P7D" or dates # already formatted in ISO08601. Otherwise, try to coerce to datetime - if ( - len(datetime_input) == 1 - and re.search(r"P", datetime_input[0], re.IGNORECASE) + if len(datetime_input) == 1 and ( + re.search(r"P", datetime_input[0], re.IGNORECASE) or "/" in datetime_input[0] ): return datetime_input[0] @@ -291,12 +290,15 @@ def _check_ogc_requests(endpoint: str = "daily", req_type: str = "queryables"): Raises ------ - AssertionError + ValueError If req_type is not "queryables" or "schema". requests.HTTPError If the HTTP request returns an unsuccessful status code. """ - assert req_type in ["queryables", "schema"] + if req_type not in ("queryables", "schema"): + raise ValueError( + f"req_type must be 'queryables' or 'schema', got {req_type!r}" + ) url = f"{OGC_API_URL}/collections/{endpoint}/{req_type}" resp = requests.get(url, headers=_default_headers()) resp.raise_for_status() From 6ba98c8a9ea716f3a36350fa965a8a2bbc159670 Mon Sep 17 00:00:00 2001 From: thodson-usgs Date: Thu, 9 Apr 2026 11:11:10 -0500 Subject: [PATCH 2/2] Ruff format --- dataretrieval/nwis.py | 1 - dataretrieval/waterdata/utils.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/dataretrieval/nwis.py b/dataretrieval/nwis.py index ca477ae..160c433 100644 --- a/dataretrieval/nwis.py +++ b/dataretrieval/nwis.py @@ -994,7 +994,6 @@ def _read_json(json): index_list.append(len(site_list)) for start, end in zip(index_list[:-1], index_list[1:]): - # grab a block containing timeseries 0:21, # which are all from the same site site_block = json["value"]["timeSeries"][start:end] diff --git a/dataretrieval/waterdata/utils.py b/dataretrieval/waterdata/utils.py index c7d5665..ecf99ba 100644 --- a/dataretrieval/waterdata/utils.py +++ b/dataretrieval/waterdata/utils.py @@ -296,9 +296,7 @@ def _check_ogc_requests(endpoint: str = "daily", req_type: str = "queryables"): If the HTTP request returns an unsuccessful status code. """ if req_type not in ("queryables", "schema"): - raise ValueError( - f"req_type must be 'queryables' or 'schema', got {req_type!r}" - ) + raise ValueError(f"req_type must be 'queryables' or 'schema', got {req_type!r}") url = f"{OGC_API_URL}/collections/{endpoint}/{req_type}" resp = requests.get(url, headers=_default_headers()) resp.raise_for_status()