Fix KeyError in _is_package_available for packages with dotted names#42050
Merged
Rocketknight1 merged 1 commit intohuggingface:mainfrom Nov 6, 2025
Conversation
Fixes huggingface#41981 When checking for packages with dotted names like 'optimum.quanto', the function was crashing with KeyError because these packages aren't always in PACKAGE_DISTRIBUTION_MAPPING (it might only have 'optimum' as a key, not 'optimum.quanto'). The function already had fallback logic to handle cases where package metadata can't be found - it tries to import the package directly. But the exception handler only caught PackageNotFoundError, not KeyError. This adds KeyError to the exception handler so the fallback works for both cases.
9af9090 to
c7512b1
Compare
Member
|
Yes, this makes sense to me! Since it should only have an impact in situations that were already causing a crash, I think it's safe to merge this. |
Rocketknight1
approved these changes
Nov 6, 2025
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Abdennacer-Badaoui
pushed a commit
to Abdennacer-Badaoui/transformers
that referenced
this pull request
Nov 10, 2025
…uggingface#42050) Fixes huggingface#41981 When checking for packages with dotted names like 'optimum.quanto', the function was crashing with KeyError because these packages aren't always in PACKAGE_DISTRIBUTION_MAPPING (it might only have 'optimum' as a key, not 'optimum.quanto'). The function already had fallback logic to handle cases where package metadata can't be found - it tries to import the package directly. But the exception handler only caught PackageNotFoundError, not KeyError. This adds KeyError to the exception handler so the fallback works for both cases. Co-authored-by: Yashwant Bezawada <yashwantbezawada@gmail.com>
SangbumChoi
pushed a commit
to SangbumChoi/transformers
that referenced
this pull request
Jan 23, 2026
…uggingface#42050) Fixes huggingface#41981 When checking for packages with dotted names like 'optimum.quanto', the function was crashing with KeyError because these packages aren't always in PACKAGE_DISTRIBUTION_MAPPING (it might only have 'optimum' as a key, not 'optimum.quanto'). The function already had fallback logic to handle cases where package metadata can't be found - it tries to import the package directly. But the exception handler only caught PackageNotFoundError, not KeyError. This adds KeyError to the exception handler so the fallback works for both cases. Co-authored-by: Yashwant Bezawada <yashwantbezawada@gmail.com>
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.
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.