Skip to content

Commit

Permalink
MNT: Fix or ignore issues raised by pyright. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Feb 11, 2024
1 parent c1d3e9f commit db00be6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
10 changes: 5 additions & 5 deletions pydaymet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import click
import geopandas as gpd
import pandas as pd
import shapely.geometry as sgeom
import shapely

from pydaymet import pydaymet as daymet
from pydaymet.exceptions import (
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_target_df(tdf: DFType, req_cols: list[str]) -> DFType:
missing = [c for c in req_cols if c not in tdf]
if missing:
raise MissingItemError(missing)
return tdf[req_cols] # pyright: ignore[reportGeneralTypeIssues]
return tdf[req_cols] # pyright: ignore[reportReturnType]


def get_required_cols(geom_type: str, columns: pd.Index) -> list[str]:
Expand All @@ -55,9 +55,9 @@ def get_required_cols(geom_type: str, columns: pd.Index) -> list[str]:
def _get_region(gid: str, geom: Polygon | MultiPolygon | Point) -> str:
"""Get the Daymer region of an input geometry (point or polygon)."""
region_bbox = {
"na": sgeom.box(-136.8989, 6.0761, -6.1376, 69.077),
"hi": sgeom.box(-160.3055, 17.9539, -154.7715, 23.5186),
"pr": sgeom.box(-67.9927, 16.8443, -64.1195, 19.9381),
"na": shapely.box(-136.8989, 6.0761, -6.1376, 69.077),
"hi": shapely.box(-160.3055, 17.9539, -154.7715, 23.5186),
"pr": shapely.box(-67.9927, 16.8443, -64.1195, 19.9381),
}
for region, bbox in region_bbox.items():
if bbox.contains(geom):
Expand Down
14 changes: 7 additions & 7 deletions pydaymet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
import numpy.typing as npt
import pandas as pd
import shapely.geometry as sgeom
import shapely
import xarray as xr

from pydaymet.exceptions import InputRangeError, InputTypeError, InputValueError
Expand All @@ -21,8 +21,8 @@
from numba import config as numba_config
from numba import jit, prange

ngjit = functools.partial(jit, nopython=True, nogil=True)
numba_config.THREADING_LAYER = "workqueue"
ngjit = functools.partial(jit, nopython=True, nogil=True) # pyright: ignore[reportAssignmentType]
numba_config.THREADING_LAYER = "workqueue" # pyright: ignore[reportAttributeAccessIssue]
has_numba = True
except ImportError:
has_numba = False
Expand Down Expand Up @@ -206,9 +206,9 @@ def __init__(
self.snow = validated.snow

self.region_bbox = {
"na": sgeom.box(-136.8989, 6.0761, -6.1376, 69.077),
"hi": sgeom.box(-160.3055, 17.9539, -154.7715, 23.5186),
"pr": sgeom.box(-67.9927, 16.8443, -64.1195, 19.9381),
"na": shapely.box(-136.8989, 6.0761, -6.1376, 69.077),
"hi": shapely.box(-160.3055, 17.9539, -154.7715, 23.5186),
"pr": shapely.box(-67.9927, 16.8443, -64.1195, 19.9381),
}
if self.region == "pr":
self.valid_start = pd.to_datetime("1950-01-01")
Expand Down Expand Up @@ -366,7 +366,7 @@ def dates_tolist(self, dates: tuple[str, str]) -> list[tuple[pd.Timestamp, pd.Ti
lp = period[(period.is_leap_year) & (~period.strftime(DATE_FMT).str.endswith("12-31"))]
_period = period[(period.isin(nl)) | (period.isin(lp))]
years = [_period[_period.year == y] for y in _period.year.unique()]
return [(y[0], y[-1]) for y in years]
return [(y[0], y[-1]) for y in years] # pyright: ignore[reportReturnType]

def years_tolist(self, years: list[int] | int) -> list[tuple[pd.Timestamp, pd.Timestamp]]:
"""Correct dates for Daymet accounting for leap years.
Expand Down
13 changes: 7 additions & 6 deletions pydaymet/pet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Core class for the Daymet functions."""
# pyright: reportGeneralTypeIssues=false
# pyright: reportReturnType=false,reportArgumentType=false
from __future__ import annotations

from dataclasses import dataclass, fields
Expand Down Expand Up @@ -28,6 +28,7 @@
CRSTYPE = Union[int, str, CRS]
DF = TypeVar("DF", pd.DataFrame, xr.Dataset)
DS = TypeVar("DS", pd.Series, xr.DataArray)
PET_METHODS = Literal["penman_monteith", "priestley_taylor", "hargreaves_samani"]

__all__ = ["potential_et"]

Expand Down Expand Up @@ -333,7 +334,7 @@ def __init__(
self,
clm: pd.DataFrame,
coords: tuple[float, float],
method: str,
method: PET_METHODS,
crs: CRSTYPE = 4326,
params: dict[str, float] | None = None,
) -> None:
Expand Down Expand Up @@ -415,11 +416,11 @@ def penman_monteith(self) -> pd.DataFrame:
)

# recommended when no data is not available to estimate wind speed
u_2m = self.clm[self.u2m] if self.u2m in self.clm else 2.0
u_2m = self.clm.get(self.u2m, 2.0)
self.clm["pet (mm/day)"] = (
0.408 * vp_slope * (rad_n - self.params.soil_heat_flux)
+ gamma * 900.0 / (self.tmean + 273.0) * u_2m * (e_s - e_a)
) / (vp_slope + gamma * (1 + 0.34 * u_2m))
) / (vp_slope + gamma * (1 + 0.34 * u_2m)) # pyright: ignore[reportOperatorIssue]

return self.clm

Expand Down Expand Up @@ -525,7 +526,7 @@ class PETGridded:
def __init__(
self,
clm: xr.Dataset,
method: str,
method: PET_METHODS,
params: dict[str, float] | None = None,
) -> None:
self.clm = clm.copy()
Expand Down Expand Up @@ -640,7 +641,7 @@ def penman_monteith(self) -> xr.Dataset:
)

# recommended when no data is not available to estimate wind speed
u_2m = self.clm["u2m"] if "u2m" in self.clm_vars else 2.0
u_2m = self.clm.get("u2m", 2.0)
self.clm["pet"] = (
0.408 * self.clm["vp_slope"] * (self.clm["rad_n"] - self.params.soil_heat_flux)
+ self.clm["gamma"]
Expand Down
11 changes: 6 additions & 5 deletions pydaymet/pydaymet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Access the Daymet database for both single single pixel and gridded queries."""
# pyright: reportGeneralTypeIssues=false
# pyright: reportArgumentType=false,reportCallIssue=false,reportReturnType=false
from __future__ import annotations

import functools
Expand Down Expand Up @@ -28,6 +28,7 @@
from shapely import MultiPolygon, Polygon

CRSTYPE = Union[int, str, pyproj.CRS]
PET_METHODS = Literal["penman_monteith", "priestley_taylor", "hargreaves_samani"]

DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"
MAX_CONN = 10
Expand Down Expand Up @@ -131,7 +132,7 @@ def _by_coord(
daymet: Daymet,
time_scale: str,
dates: list[tuple[pd.Timestamp, pd.Timestamp]],
pet: str | None,
pet: PET_METHODS | None,
pet_params: dict[str, float] | None,
snow: bool,
snow_params: dict[str, float] | None,
Expand Down Expand Up @@ -181,7 +182,7 @@ def get_bycoords(
| None = None,
region: Literal["na", "hi", "pr"] = "na",
time_scale: Literal["daily", "monthly", "annual"] = "daily",
pet: Literal["penman_monteith", "priestley_taylor", "hargreaves_samani"] | None = None,
pet: PET_METHODS | None = None,
pet_params: dict[str, float] | None = None,
snow: bool = False,
snow_params: dict[str, float] | None = None,
Expand Down Expand Up @@ -407,7 +408,7 @@ def get_bygeom(
| None = None,
region: Literal["na", "hi", "pr"] = "na",
time_scale: Literal["daily", "monthly", "annual"] = "daily",
pet: Literal["penman_monteith", "priestley_taylor", "hargreaves_samani"] | None = None,
pet: PET_METHODS | None = None,
pet_params: dict[str, float] | None = None,
snow: bool = False,
snow_params: dict[str, float] | None = None,
Expand Down Expand Up @@ -589,7 +590,7 @@ def get_bystac(
region: Literal["na", "hi", "pr"] = "na",
time_scale: Literal["daily", "monthly", "annual"] = "daily",
res_km: int = 1,
pet: Literal["penman_monteith", "priestley_taylor", "hargreaves_samani"] | None = None,
pet: PET_METHODS | None = None,
pet_params: dict[str, float] | None = None,
snow: bool = False,
snow_params: dict[str, float] | None = None,
Expand Down

0 comments on commit db00be6

Please sign in to comment.