Use .get method instead of in operator to get value from inference cache safely
#2609
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.
Linked Issues
databrickslabs/ucx#2908 and databrickslabs/ucx#2919
Type of Changes
Description
Use
.getmethod instead ofinoperator to get value from inference cache safelyMotivation
We ran into a
KeyErrorwhen callingNodeNG.inferred(see stack trace below). Unfortunately, I do not have the code that results in theKeyErrorwhen inferring - yet.Stack trace
Theory
I have the following theory, the code fails with a
KeyErroron theyieldhere:Which is odd, as first the
keyis checked to be in the inference cache. Maybe, thekey in context.inferredchecks if thekeyis in the cache, but invalidates the cache at the same time - somehow? There is an_invalidate_cache()in theTransformVisitor._transform()that is in the stack trace and theruntime_checkableis a builtin for which Astroid has a custom lookup that may invalidate the cache.Or, we thread astroid. The thread is probably not thread safe since it is a plain dict, maybe the
KeyErrorwas unlucky timing.Solution
Use the key once on the inference cache by using
.get