Skip to content

Commit

Permalink
MNT: Remove individual imports of exceptions from the high-level API …
Browse files Browse the repository at this point in the history
…and add exceptions modules instead. [skip ci]
  • Loading branch information
Taher Chegini committed Oct 6, 2024
1 parent 55e2942 commit 13f215c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 0 additions & 5 deletions src/pynldas2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from importlib.metadata import PackageNotFoundError, version

from pynldas2 import exceptions
from pynldas2.exceptions import InputRangeError, InputTypeError, InputValueError, NLDASServiceError
from pynldas2.print_versions import show_versions
from pynldas2.pynldas2 import get_bycoords, get_bygeom, get_grid_mask

Expand All @@ -19,10 +18,6 @@
"get_grid_mask",
"get_bygeom",
"show_versions",
"InputRangeError",
"InputTypeError",
"InputValueError",
"NLDASServiceError",
"exceptions",
"__version__",
]
5 changes: 4 additions & 1 deletion src/pynldas2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from __future__ import annotations

from typing import Generator, Sequence
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Generator, Sequence


class NLDASServiceError(Exception):
Expand Down
21 changes: 13 additions & 8 deletions src/pynldas2/pynldas2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import warnings
from io import BytesIO, StringIO
from typing import TYPE_CHECKING, Any, Callable, Literal, Sequence, TypeVar, Union
from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, Union

import numpy as np
import numpy.typing as npt
Expand All @@ -21,7 +21,7 @@
from pynldas2.exceptions import InputRangeError, InputTypeError, InputValueError, NLDASServiceError

try:
from numpy.core._exceptions import UFuncTypeError # pyright: ignore[reportMissingImports]
from numpy._core._exceptions import UFuncTypeError
except ImportError:
UFuncTypeError = TypeError

Expand All @@ -30,7 +30,7 @@
from numba import njit, prange

ngjit = functools.partial(njit, nogil=True)
numba_config.THREADING_LAYER = "workqueue" # pyright: ignore[reportAttributeAccessIssue]
numba_config.THREADING_LAYER = "workqueue"
has_numba = True
except ImportError:
has_numba = False
Expand All @@ -39,7 +39,7 @@
T = TypeVar("T")
Func = Callable[..., T]

def ngjit(signature_or_function: str | Func[T]) -> Callable[[Func[T]], Func[T]]:
def ngjit(_: str | Func[T]) -> Callable[[Func[T]], Func[T]]:
def decorator_njit(func: Func[T]) -> Func[T]:
@functools.wraps(func)
def wrapper_decorator(*args: tuple[Any, ...], **kwargs: dict[str, Any]) -> T:
Expand All @@ -51,6 +51,8 @@ def wrapper_decorator(*args: tuple[Any, ...], **kwargs: dict[str, Any]) -> T:


if TYPE_CHECKING:
from collections.abc import Sequence

from shapely import MultiPolygon, Polygon

DF = TypeVar("DF", pd.DataFrame, xr.Dataset)
Expand Down Expand Up @@ -292,10 +294,10 @@ def _get_dates(
) -> list[pd.Timestamp]:
"""Get dates."""
start = pd.to_datetime(start_date)
end = pd.to_datetime(end_date) + pd.Timedelta("1D") # pyright: ignore[reportGeneralTypeIssues]
end = pd.to_datetime(end_date) + pd.Timedelta("1D")
if start < pd.to_datetime("1979-01-01T13"):
raise InputRangeError("start_date", "1979-01-01 to yesterday")
if end > pd.Timestamp.now() - pd.Timedelta("1D"): # pyright: ignore[reportGeneralTypeIssues]
if end > pd.Timestamp.now() - pd.Timedelta("1D"): # pyright: ignore[reportOperatorIssue]
raise InputRangeError("end_date", "1979-01-01 to yesterday")
if end <= start:
raise InputRangeError("end_date", "after start_date")
Expand Down Expand Up @@ -362,7 +364,7 @@ def _get_lon_lat(
"""Get longitude and latitude from a list of coordinates."""
try:
coords_list = hgu.coords_list(coords)
except hgu.InputTypeError as ex:
except hgu.exceptions.InputTypeError as ex:
raise InputTypeError("coords", "tuple of length 2 or list of tuples") from ex

xx, yy = zip(*coords_list)
Expand Down Expand Up @@ -435,11 +437,14 @@ def get_bycoords(
raise InputRangeError("coords", str(bounds))

idx = list(coords_id) if coords_id is not None else [f"P{i}" for i in range(n_pts)]
if len(idx) != n_pts:
raise InputValueError("coords_id", "same length as coords")

nldas = functools.partial(
_byloc,
variables=variables,
start_date=start_date,
end_date=end_date,
variables=variables,
n_conn=n_conn,
snow=snow,
snow_params=snow_params,
Expand Down

0 comments on commit 13f215c

Please sign in to comment.