Skip to content

Commit f416956

Browse files
thodson-usgsclaude
andcommitted
refactor(api)!: expose service submodules, not a flattened top-level namespace
`dataretrieval/__init__.py` star-imported every service module, flattening all their public functions into the top-level package namespace. That: - leaked one large, ambiguous namespace (`dataretrieval.get_dv`, `dataretrieval.get_results`, ...), and - silently collided on names defined by more than one service: `get_ratings` (nwis vs waterdata) and `what_sites` (nwis vs wqp) resolved to whichever module was star-imported last; the other was shadowed. Replace the star-imports with submodule imports, so callers reach each service through its own module -- the pattern the README, the docs (per-module autodoc), and the tests already use: from dataretrieval import waterdata df, meta = waterdata.get_ratings(...) from dataretrieval import nwis df, meta = nwis.get_ratings(...) `dataretrieval.<name>` and `from dataretrieval import <name>` still work for every service module, and `__version__` is unchanged. `nldi` remains import-on-demand (it pulls in the optional geopandas dependency). The collision is now impossible -- each `get_ratings` / `what_sites` lives only under its module. BREAKING CHANGE: top-level function access (e.g. `dataretrieval.get_dv`) is removed; use the service module (`dataretrieval.nwis.get_dv`). Exception and helper classes likewise move under their modules (e.g. `dataretrieval.utils.NoSitesError`). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1adf174 commit f416956

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

dataretrieval/__init__.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
1+
"""Discover and retrieve water data from U.S. federal hydrologic web services.
2+
3+
Access each service through its submodule rather than the top-level package::
4+
5+
from dataretrieval import waterdata # modern USGS Water Data API
6+
7+
df, meta = waterdata.get_daily(monitoring_location_id="USGS-05427718")
8+
9+
from dataretrieval import nwis # legacy NWIS services
10+
11+
df, meta = nwis.get_dv(sites="05427718")
12+
13+
Available service modules: ``waterdata``, ``wqp`` (Water Quality Portal),
14+
``nldi``, ``samples``, ``streamstats``, ``nadp``, and the deprecated ``nwis``.
15+
16+
``nldi`` requires geopandas (``pip install dataretrieval[nldi]``) and is not
17+
imported automatically; import it explicitly with ``from dataretrieval import
18+
nldi``.
19+
"""
20+
121
from importlib.metadata import PackageNotFoundError, version
222

323
try:
424
__version__ = version("dataretrieval")
525
except PackageNotFoundError:
626
__version__ = "version-unknown"
727

8-
from dataretrieval.nadp import *
9-
from dataretrieval.nwis import *
10-
from dataretrieval.samples import *
11-
from dataretrieval.streamstats import *
12-
from dataretrieval.utils import *
13-
from dataretrieval.waterdata import *
14-
from dataretrieval.wqp import *
28+
# Bind the service submodules as ``dataretrieval.<name>`` -- so
29+
# ``from dataretrieval import waterdata`` works and the modules load on
30+
# ``import dataretrieval`` -- WITHOUT flattening their functions into the
31+
# top-level namespace. Callers go through the owning module, e.g.
32+
# ``waterdata.get_ratings`` / ``nwis.get_ratings``, which also removes the
33+
# ambiguity of names defined in more than one service.
34+
#
35+
# ``nldi`` is omitted on purpose: it imports geopandas at module load, an
36+
# optional dependency, so importing it here would break ``import dataretrieval``
37+
# for installs without the ``[nldi]`` extra.
38+
from . import (
39+
nadp,
40+
nwis,
41+
samples,
42+
streamstats,
43+
utils,
44+
waterdata,
45+
wqp,
46+
)
47+
48+
__all__ = [
49+
"nadp",
50+
"nwis",
51+
"samples",
52+
"streamstats",
53+
"utils",
54+
"waterdata",
55+
"wqp",
56+
"__version__",
57+
]

0 commit comments

Comments
 (0)