From abda60db45d6f9c4ca7fa8b72dbad0eb03bc3874 Mon Sep 17 00:00:00 2001 From: Taher Chegini Date: Sun, 6 Oct 2024 09:53:53 -0400 Subject: [PATCH] MNT: Remove individual imports of exceptions from the high-level API and add exceptions modules instead. [skip ci] --- src/pynhd/__init__.py | 20 -------------------- src/pynhd/exceptions.py | 11 +++++++---- src/pynhd/network_tools.py | 16 ++++++++-------- src/pynhd/nhdplus_derived.py | 2 +- src/pynhd/pynhd.py | 20 ++++++++++++++------ 5 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/pynhd/__init__.py b/src/pynhd/__init__.py index ae64388..add2f0a 100644 --- a/src/pynhd/__init__.py +++ b/src/pynhd/__init__.py @@ -6,17 +6,6 @@ from pynhd import exceptions from pynhd.core import AGRBase, GeoConnex, ScienceBase -from pynhd.exceptions import ( - DependencyError, - InputRangeError, - InputTypeError, - InputValueError, - MissingColumnError, - MissingCRSError, - MissingItemError, - NoTerminalError, - ZeroMatchedError, -) from pynhd.network_tools import ( NHDTools, enhd_flowlines_nx, @@ -51,15 +40,6 @@ __version__ = "999" __all__ = [ - "DependencyError", - "InputRangeError", - "InputValueError", - "InputTypeError", - "MissingItemError", - "MissingColumnError", - "MissingCRSError", - "ZeroMatchedError", - "NoTerminalError", "prepare_nhdplus", "topoogical_sort", "vector_accumulation", diff --git a/src/pynhd/exceptions.py b/src/pynhd/exceptions.py index 8068208..ceafba8 100644 --- a/src/pynhd/exceptions.py +++ b/src/pynhd/exceptions.py @@ -2,11 +2,14 @@ from __future__ import annotations -from typing import Generator +from typing import TYPE_CHECKING -import async_retriever as ar -import pygeoogc as ogc -import pygeoutils as pgu +import async_retriever.exceptions as ar +import pygeoogc.exceptions as ogc +import pygeoutils.exceptions as pgu + +if TYPE_CHECKING: + from collections.abc import Generator class ZeroMatchedError(ogc.ZeroMatchedError): diff --git a/src/pynhd/network_tools.py b/src/pynhd/network_tools.py index bf2aca0..2a688ff 100644 --- a/src/pynhd/network_tools.py +++ b/src/pynhd/network_tools.py @@ -5,8 +5,9 @@ import io import warnings +from collections.abc import Iterable from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Iterable, Union, cast +from typing import TYPE_CHECKING, Any, Callable, Union, cast import cytoolz.curried as tlz import geopandas as gpd @@ -20,7 +21,7 @@ import async_retriever as ar import pygeoutils as pgu from pygeoogc import streaming_download -from pygeoutils import InputTypeError +from pygeoutils.exceptions import InputTypeError from pynhd import nhdplus_derived as derived from pynhd.core import ScienceBase from pynhd.exceptions import ( @@ -553,7 +554,7 @@ def _get_idx(d_sp: npt.NDArray[np.float64], distance: float) -> npt.NDArray[np.i def _get_spline_params( - line: LineString, n_seg: int, distance: float, crs: CRSTYPE, smoothing: float | None + line: LineString, n_seg: int, distance: float, smoothing: float | None ) -> tuple[ npt.NDArray[np.float64], npt.NDArray[np.float64], @@ -561,11 +562,11 @@ def _get_spline_params( ]: """Get Spline parameters (x, y, phi).""" _n_seg = n_seg - spline = pgu.spline_linestring(line, crs, _n_seg, smoothing=smoothing) + spline = pgu.spline_linestring(line, _n_seg, smoothing=smoothing) idx = _get_idx(spline.distance, distance) while np.isnan(idx).any(): _n_seg *= 2 - spline = pgu.spline_linestring(line, crs, _n_seg, smoothing=smoothing) + spline = pgu.spline_linestring(line, _n_seg, smoothing=smoothing) idx = _get_idx(spline.distance, distance) x, y, phi = spline.x[idx], spline.y[idx], spline.phi[idx] spacing = np.diff(spline.distance[idx]) @@ -587,7 +588,6 @@ def _xs_planar( n_seg: int, half_width: float, distance: float, - crs: CRSTYPE, smoothing: float | None, ) -> npt.NDArray[LineString]: # pyright: ignore """Get cross-sections along a line at a given spacing. @@ -621,7 +621,7 @@ def _xs_planar( if shapely.get_num_points(line) < 4: line = shapely.segmentize(line, line.length / 4) - x, y, phi = _get_spline_params(line, n_seg, distance, crs, smoothing) + x, y, phi = _get_spline_params(line, n_seg, distance, smoothing) xy = np.c_[x, y] # Normal vector of the centerline at each point. @@ -794,7 +794,7 @@ def flowline_xsection( n_segments = (int(np.ceil(ln.length / distance)) * 100 for ln in lines) main_split = tlz.concat( - _xs_planar(ln, n_seg, half_width, distance, flw.crs, smoothing) + _xs_planar(ln, n_seg, half_width, distance, smoothing) for ln, n_seg in zip(lines, n_segments) ) diff --git a/src/pynhd/nhdplus_derived.py b/src/pynhd/nhdplus_derived.py index 8642706..fd8ee13 100644 --- a/src/pynhd/nhdplus_derived.py +++ b/src/pynhd/nhdplus_derived.py @@ -456,7 +456,7 @@ def epa_nhd_catchments( *[ ( "https://qed.epa.gov/hms/rest/api/info/catchment", - {"params": {**f_kwd, "comid": comid}}, + {"params": f_kwd | {"comid": comid}}, ) for comid in clist ] diff --git a/src/pynhd/pynhd.py b/src/pynhd/pynhd.py index cb55194..a94de67 100644 --- a/src/pynhd/pynhd.py +++ b/src/pynhd/pynhd.py @@ -4,7 +4,7 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Any, Generator, Literal, Sequence, Union, cast, overload +from typing import TYPE_CHECKING, Any, Literal, Union, cast, overload import cytoolz.curried as tlz import numpy as np @@ -15,12 +15,20 @@ import async_retriever as ar import pygeoogc as ogc import pygeoutils as geoutils -from pygeoogc import WFS, InputValueError, ServiceURL -from pygeoutils import EmptyResponseError, InputTypeError +from pygeoogc import WFS, ServiceURL +from pygeoutils.exceptions import EmptyResponseError from pynhd.core import AGRBase, PyGeoAPIBase, PyGeoAPIBatch -from pynhd.exceptions import InputRangeError, MissingItemError, ZeroMatchedError +from pynhd.exceptions import ( + InputRangeError, + InputTypeError, + InputValueError, + MissingItemError, + ZeroMatchedError, +) if TYPE_CHECKING: + from collections.abc import Generator, Sequence + import geopandas as gpd import pyproj from shapely import MultiPoint, MultiPolygon, Point, Polygon @@ -907,7 +915,7 @@ def _check_resp(resp: dict[str, Any] | list[dict[str, Any]] | None) -> bool: @overload def _get_urls( - self, url_parts: Generator[tuple[str, ...], None, None] | str, raw: Literal[True] = ... + self, url_parts: Generator[tuple[str, ...], None, None] | str, raw: Literal[True] = True ) -> tuple[list[int], list[dict[str, Any]] | dict[str, Any]]: ... @overload @@ -1150,7 +1158,7 @@ def getcharacteristic_byid( char_type: str, fsource: str = ..., char_ids: str | list[str] = ..., - values_only: Literal[True] = ..., + values_only: Literal[True] = True, ) -> pd.DataFrame: ... @overload