fix: importlib-metadata as a dependency on Python>=3.10 #839
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.
Issue Summary
The zulip_bots package attempts to import importlib_metadata regardless of the Python version, even though importlib-metadata is not listed as a dependency in the package. This leads to an ImportError on Python versions >= 3.10, where importlib.metadata is included in the standard library, but the fallback to importlib_metadata is still attempted.
Root Cause
The error occurs because the code in zulip_bots does not correctly handle the conditional import for importlib-metadata vs. the standard library's importlib.metadata. The setup.py configuration ensures the dependency is installed only for Python < 3.10, but the runtime code doesn't account for the difference in module naming.
Solution
To fix the issue, I modified the code in zulip_bots/finder.py to conditionally import the appropriate module based on the Python version.
Testing
Verified behavior on Python 3.9 and Python 3.10+ environments:
Python >= 3.10: Uses importlib.metadata.
Python < 3.10: Falls back to importlib_metadata.
Added unit tests to validate both paths with mock objects.
Impact
This fix ensures that:
The package correctly resolves metadata imports regardless of Python version.
Dependencies are installed appropriately based on the Python version.