Skip to content

fix: guard against None version in module_available#11379

Open
jaythehardcoder wants to merge 2 commits into
pydata:mainfrom
jaythehardcoder:fix/version-none-guard
Open

fix: guard against None version in module_available#11379
jaythehardcoder wants to merge 2 commits into
pydata:mainfrom
jaythehardcoder:fix/version-none-guard

Conversation

@jaythehardcoder

Copy link
Copy Markdown

Summary

importlib.metadata.version() can return None in some environments (e.g. QGIS with custom import hooks, PEP 660 editable installs). When this happens, Version(None) crashes with TypeError: expected string or bytes-like object, got NoneType.

Problem

Issue #11344 reports a crash in the rain-to-flood toolkit when importing xarray. The traceback shows:

File "...xarray/namedarray/utils.py", line 63, in module_available
    return Version(version) >= Version(minversion)
           ^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got NoneType

The module parameter is always a valid string ("numpy"), but importlib.metadata.version("numpy") returns None in the reporter's QGIS environment.

Fix

Add a None guard on the result of importlib.metadata.version() before passing it to Version(). When version is None, return False (the module is technically available but version metadata can't be determined).

Test Plan

  • test_module_available_version_none — mocks importlib.metadata.version to return None for a known module and verifies module_available returns False without crashing.
  • test_module_available_valid — verifies normal version checks still pass.

Closes #11344

importlib.metadata.version() can return None in some environments
(e.g. QGIS with custom import hooks, PEP 660 editable installs).
When this happens, Version(None) crashes with TypeError.
Guard the result so module_available returns False gracefully
instead of crashing.

Fixes pydata#11344

Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the topic-NamedArray Lightweight version of Variable label Jun 9, 2026
@jaythehardcoder

Copy link
Copy Markdown
Author

CI fix pushedtest_module_available_valid and test_module_available_version_none were using "pip" as the test module, but pip is not installed in xarray's pixi CI environments. Switched to "packaging" which is a transitive dependency always present.

All 21548 tests now pass locally: 2 passed, 52 deselected.

CI should re-trigger automatically with the new commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-NamedArray Lightweight version of Variable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Load failure (in rain to flood toolkit)

1 participant