Fix KeyError in _is_package_available for packages with dotted names #42050
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.
What does this PR do?
Fixes #41981
I ran into this issue while testing with
optimum.quanto. The function was crashing withKeyErrorwhen checking for packages with dotted names.The problem is in
import_utils.pyline 56 - when it tries to look up the package inPACKAGE_DISTRIBUTION_MAPPING, packages with dots in the name (likeoptimum.quanto) aren't always there. The mapping might only haveoptimumas a key, notoptimum.quanto.The function already has fallback logic to handle cases where package metadata can't be found - it imports the package directly and checks for
__version__. But the exception handler only caughtPackageNotFoundError, notKeyError.I added
KeyErrorto the exception handler so the fallback works for both cases. Now when a package name isn't in the mapping, it'll use the same fallback path instead of crashing.This is a one-line change that handles the edge case without affecting normal operation. The existing tests should still pass since the behavior for normal packages is unchanged.