From b461d69087a90f82a9be465449133623f5eca4d2 Mon Sep 17 00:00:00 2001 From: driphtyio Date: Fri, 10 Jul 2026 14:27:26 -0700 Subject: [PATCH] fix: handle None module in module_available() Add guard for module=None in module_available() to avoid TypeError from find_spec(None). Returns False instead. Closes #11344 --- xarray/namedarray/utils.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/namedarray/utils.py b/xarray/namedarray/utils.py index 3490a76aa8d..64cf4784397 100644 --- a/xarray/namedarray/utils.py +++ b/xarray/namedarray/utils.py @@ -37,15 +37,15 @@ @lru_cache -def module_available(module: str, minversion: str | None = None) -> bool: +def module_available(module: str | None, minversion: str | None = 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. + module : str or None + Name of the module. If None, returns False. minversion : str, optional Minimum version of the module @@ -54,6 +54,8 @@ def module_available(module: str, minversion: str | None = None) -> bool: available : bool Whether the module is installed. """ + if module is None: + return False if importlib.util.find_spec(module) is None: return False