Skip to content

Commit e4c84bf

Browse files
thodson-usgsclaude
andcommitted
Apply /simplify pass on duckdb connectors
* Narrow `_flatten_geometry` exception from `Exception` to `ValueError` (the actual error geopandas raises on `.x`/`.y` for non-Point geometries) so genuine bugs aren't swallowed. * Drop the `if TYPE_CHECKING: import duckdb as _duckdb` block in `_base.py`. The runtime `try: import duckdb` is enough for type checkers; the alias was dead code. * Parametrize `test_what_endpoint_invokes_correct_underlying` so each WQP helper gets its own test case (matches the parametrize pattern already used in `tests/waterdata_utils_test.py`). 20 tests pass; ruff clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d4c1f29 commit e4c84bf

2 files changed

Lines changed: 20 additions & 21 deletions

File tree

dataretrieval/duckdb_connectors/_base.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import logging
1919
from collections.abc import Callable
20-
from typing import TYPE_CHECKING, Any
20+
from typing import Any
2121

2222
import pandas as pd
2323

@@ -35,9 +35,6 @@
3535
except ImportError:
3636
GEOPANDAS = False
3737

38-
if TYPE_CHECKING:
39-
import duckdb as _duckdb # noqa: F401 (resolved by type-checker only)
40-
4138
logger = logging.getLogger(__name__)
4239

4340
_INSTALL_HINT = (
@@ -96,8 +93,8 @@ def _flatten_geometry(df: pd.DataFrame) -> pd.DataFrame:
9693
try:
9794
out["longitude"] = geom.x
9895
out["latitude"] = geom.y
99-
except Exception:
100-
# Non-point geometries (lines, polygons): skip lon/lat shortcut.
96+
except ValueError:
97+
# geopandas raises ValueError on .x/.y for non-Point geometries.
10198
pass
10299

103100
out[geom_name] = geom.to_wkt()

tests/duckdb_connectors_wqp_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,21 @@ def test_join_results_to_sites(results_df, sites_df):
123123
]
124124

125125

126-
def test_what_endpoints_invoke_correct_underlying(results_df):
126+
@pytest.mark.parametrize(
127+
"helper",
128+
[
129+
"what_organizations",
130+
"what_projects",
131+
"what_activities",
132+
"what_detection_limits",
133+
"what_habitat_metrics",
134+
"what_activity_metrics",
135+
],
136+
)
137+
def test_what_endpoint_invokes_correct_underlying(results_df, helper):
127138
"""Each helper should invoke its corresponding wqp function."""
128-
helper_to_target = {
129-
"what_organizations": "what_organizations",
130-
"what_projects": "what_projects",
131-
"what_activities": "what_activities",
132-
"what_detection_limits": "what_detection_limits",
133-
"what_habitat_metrics": "what_habitat_metrics",
134-
"what_activity_metrics": "what_activity_metrics",
135-
}
136-
for helper_name, target in helper_to_target.items():
137-
with mock.patch(f"dataretrieval.wqp.{target}") as m:
138-
m.return_value = (results_df, mock.Mock())
139-
with wqp_connector.connect() as con:
140-
getattr(con, helper_name)(statecode="US:17")
141-
assert m.call_count == 1, helper_name
139+
with mock.patch(f"dataretrieval.wqp.{helper}") as m:
140+
m.return_value = (results_df, mock.Mock())
141+
with wqp_connector.connect() as con:
142+
getattr(con, helper)(statecode="US:17")
143+
assert m.call_count == 1

0 commit comments

Comments
 (0)