From 526c80fa7f1472c1aaf618e8edcd3bbaa1bc205e Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Thu, 19 Oct 2023 08:59:13 -0700 Subject: [PATCH] internal: Improve version handling for numbagg (#8325) * 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> --- xarray/core/rolling_exp.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/xarray/core/rolling_exp.py b/xarray/core/rolling_exp.py index d1e613e98a2..c8160cefef3 100644 --- a/xarray/core/rolling_exp.py +++ b/xarray/core/rolling_exp.py @@ -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( @@ -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 @@ -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 @@ -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 @@ -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 @@ -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