From d934e933812e2feb02f0f07f3d18278f12f559e1 Mon Sep 17 00:00:00 2001 From: Steven P Date: Tue, 28 Jul 2026 13:45:08 -0700 Subject: [PATCH 1/2] Handle missing package version in module_available --- xarray/namedarray/utils.py | 3 +++ xarray/tests/test_utils.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/xarray/namedarray/utils.py b/xarray/namedarray/utils.py index 3490a76aa8d..e4267637e17 100644 --- a/xarray/namedarray/utils.py +++ b/xarray/namedarray/utils.py @@ -60,6 +60,9 @@ def module_available(module: str, minversion: str | None = None) -> bool: if minversion is not None: version = importlib.metadata.version(module) + if version is None: + return False + return Version(version) >= Version(minversion) return True diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 90fdcb7d561..00a4997a9c3 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -2,6 +2,7 @@ from collections.abc import Hashable from types import EllipsisType +from unittest.mock import patch import numpy as np import pandas as pd @@ -15,6 +16,7 @@ infix_dims, iterate_nested, ) +from xarray.namedarray.utils import module_available from xarray.tests import assert_array_equal, requires_dask @@ -390,6 +392,21 @@ def f(): assert f() == 3 +# regression test +def test_module_available_with_none_version() -> None: + module_available.cache_clear() + + try: + with ( + patch("importlib.util.find_spec", return_value=object()), + patch("importlib.metadata.version", return_value=None), + ): + assert not module_available( + "package_with_missing_version", + minversion="1.0", + ) + finally: + module_available.cache_clear() def test_attempt_import() -> None: """Test optional dependency handling.""" From 1d21a255f30ada7a05a2e529bda7487a027a9e03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Jul 2026 20:48:30 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 00a4997a9c3..5bc2240ac8d 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -392,6 +392,7 @@ def f(): assert f() == 3 + # regression test def test_module_available_with_none_version() -> None: module_available.cache_clear() @@ -408,6 +409,7 @@ def test_module_available_with_none_version() -> None: finally: module_available.cache_clear() + def test_attempt_import() -> None: """Test optional dependency handling.""" np = attempt_import("numpy")