Skip to content

Commit

Permalink
Fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Jun 30, 2024
1 parent d3ce5d2 commit 79790f7
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions flox/xrutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,40 @@

import numpy as np
import pandas as pd
from numpy.core.multiarray import normalize_axis_index # type: ignore[attr-defined]
from packaging.version import Version


def module_available(module: str, minversion: Optional[str] = None) -> bool:
"""Checks whether a module is installed without importing it.
Use this for a lightweight check and lazy imports.
Parameters
----------
module : str
Name of the module.
Returns
-------
available : bool
Whether the module is installed.
"""
has = importlib.util.find_spec(module) is not None
if has:
mod = importlib.import_module(module)
return Version(mod.__version__) >= Version(minversion) if minversion is not None else True
else:
return False


if module_available("numpy", minversion="2.0.0"):
from numpy.lib.array_utils import ( # type: ignore[import-not-found]
normalize_axis_index,
)
else:
from numpy.core.numeric import normalize_axis_index # type: ignore[attr-defined]


try:
import cftime
except ImportError:
Expand Down Expand Up @@ -349,26 +380,3 @@ def nanlast(values, axis, keepdims=False):
return np.expand_dims(result, axis=axis)
else:
return result


def module_available(module: str, minversion: Optional[str] = None) -> bool:
"""Checks whether a module is installed without importing it.
Use this for a lightweight check and lazy imports.
Parameters
----------
module : str
Name of the module.
Returns
-------
available : bool
Whether the module is installed.
"""
has = importlib.util.find_spec(module) is not None
if has:
mod = importlib.import_module(module)
return Version(mod.__version__) >= Version(minversion) if minversion is not None else True
else:
return False

0 comments on commit 79790f7

Please sign in to comment.