Skip to content

Commit

Permalink
internal: Improve version handling for numbagg (#8325)
Browse files Browse the repository at this point in the history
* internal: Improve version handling for numbagg

Uses the approach in #8316, a bit nicer. Only internal.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update xarray/core/rolling_exp.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
max-sixty and pre-commit-ci[bot] authored Oct 19, 2023
1 parent e1bad52 commit 526c80f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions xarray/core/rolling_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import numbagg
from numbagg import move_exp_nanmean, move_exp_nansum

has_numbagg: Version | None = Version(numbagg.__version__)
_NUMBAGG_VERSION: Version | None = Version(numbagg.__version__)
except ImportError:
has_numbagg = None
_NUMBAGG_VERSION = None


def _get_alpha(
Expand Down Expand Up @@ -100,17 +100,17 @@ def __init__(
window_type: str = "span",
min_weight: float = 0.0,
):
if has_numbagg is None:
if _NUMBAGG_VERSION is None:
raise ImportError(
"numbagg >= 0.2.1 is required for rolling_exp but currently numbagg is not installed"
)
elif has_numbagg < Version("0.2.1"):
elif _NUMBAGG_VERSION < Version("0.2.1"):
raise ImportError(
f"numbagg >= 0.2.1 is required for `rolling_exp` but currently version {has_numbagg} is installed"
f"numbagg >= 0.2.1 is required for rolling_exp but currently version {_NUMBAGG_VERSION} is installed"
)
elif has_numbagg < Version("0.3.1") and min_weight > 0:
elif _NUMBAGG_VERSION < Version("0.3.1") and min_weight > 0:
raise ImportError(
f"numbagg >= 0.3.1 is required for `min_weight > 0` within `.rolling_exp` but currently version {has_numbagg} is installed"
f"numbagg >= 0.3.1 is required for `min_weight > 0` within `.rolling_exp` but currently version {_NUMBAGG_VERSION} is installed"
)

self.obj: T_DataWithCoords = obj
Expand Down Expand Up @@ -211,9 +211,9 @@ def std(self) -> T_DataWithCoords:
Dimensions without coordinates: x
"""

if has_numbagg is None or has_numbagg < Version("0.4.0"):
if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version("0.4.0"):
raise ImportError(
f"numbagg >= 0.4.0 is required for rolling_exp().std(), currently {has_numbagg} is installed"
f"numbagg >= 0.4.0 is required for rolling_exp().std(), currently {_NUMBAGG_VERSION} is installed"
)
dim_order = self.obj.dims

Expand Down Expand Up @@ -243,9 +243,9 @@ def var(self) -> T_DataWithCoords:
Dimensions without coordinates: x
"""

if has_numbagg is None or has_numbagg < Version("0.4.0"):
if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version("0.4.0"):
raise ImportError(
f"numbagg >= 0.4.0 is required for rolling_exp().var(), currently {has_numbagg} is installed"
f"numbagg >= 0.4.0 is required for rolling_exp().var(), currently {_NUMBAGG_VERSION} is installed"
)
dim_order = self.obj.dims

Expand Down Expand Up @@ -275,9 +275,9 @@ def cov(self, other: T_DataWithCoords) -> T_DataWithCoords:
Dimensions without coordinates: x
"""

if has_numbagg is None or has_numbagg < Version("0.4.0"):
if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version("0.4.0"):
raise ImportError(
f"numbagg >= 0.4.0 is required for rolling_exp().cov(), currently {has_numbagg} is installed"
f"numbagg >= 0.4.0 is required for rolling_exp().cov(), currently {_NUMBAGG_VERSION} is installed"
)
dim_order = self.obj.dims

Expand Down Expand Up @@ -308,9 +308,9 @@ def corr(self, other: T_DataWithCoords) -> T_DataWithCoords:
Dimensions without coordinates: x
"""

if has_numbagg is None or has_numbagg < Version("0.4.0"):
if _NUMBAGG_VERSION is None or _NUMBAGG_VERSION < Version("0.4.0"):
raise ImportError(
f"numbagg >= 0.4.0 is required for rolling_exp().cov(), currently {has_numbagg} is installed"
f"numbagg >= 0.4.0 is required for rolling_exp().cov(), currently {_NUMBAGG_VERSION} is installed"
)
dim_order = self.obj.dims

Expand Down

0 comments on commit 526c80f

Please sign in to comment.