Skip to content

Commit

Permalink
internal: Improve version handling for numbagg
Browse files Browse the repository at this point in the history
Uses the approach in pydata#8316, a bit nicer. Only internal.
  • Loading branch information
max-sixty committed Oct 17, 2023
1 parent f895dc1 commit 36a6ba6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions xarray/core/rolling_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Generic

import numpy as np
from packaging.version import Version

from xarray.core.computation import apply_ufunc
from xarray.core.options import _get_keep_attrs
Expand All @@ -14,9 +15,9 @@
import numbagg
from numbagg import move_exp_nanmean, move_exp_nansum

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


def _get_alpha(
Expand Down Expand Up @@ -99,17 +100,17 @@ def __init__(
window_type: str = "span",
min_weight: float = 0.0,
):
if has_numbagg is False:
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 < "0.2.1":
elif _NUMBAGG_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 < "0.3.1" and min_weight > 0:
elif _NUMBAGG_VERSION < "0.3.1" and min_weight > 0:
raise ImportError(
f"numbagg >= 0.3.1 is required for `min_weight > 0` but currently version {has_numbagg} is installed"
f"numbagg >= 0.3.1 is required for `min_weight > 0` but currently version {_NUMBAGG_VERSION} is installed"
)

self.obj: T_DataWithCoords = obj
Expand Down

0 comments on commit 36a6ba6

Please sign in to comment.