fix: guard against None version in module_available#11379
Open
jaythehardcoder wants to merge 2 commits into
Open
fix: guard against None version in module_available#11379jaythehardcoder wants to merge 2 commits into
jaythehardcoder wants to merge 2 commits into
Conversation
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>
Author
|
CI fix pushed — All 21548 tests now pass locally: CI should re-trigger automatically with the new commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
importlib.metadata.version()can returnNonein some environments (e.g. QGIS with custom import hooks, PEP 660 editable installs). When this happens,Version(None)crashes withTypeError: 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:
The
moduleparameter is always a valid string ("numpy"), butimportlib.metadata.version("numpy")returnsNonein the reporter's QGIS environment.Fix
Add a
Noneguard on the result ofimportlib.metadata.version()before passing it toVersion(). When version isNone, returnFalse(the module is technically available but version metadata can't be determined).Test Plan
test_module_available_version_none— mocksimportlib.metadata.versionto returnNonefor a known module and verifiesmodule_availablereturnsFalsewithout crashing.test_module_available_valid— verifies normal version checks still pass.Closes #11344