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
.get
method instead ofin
operator to get value from inference cache safelyMotivation
We ran into a
KeyError
when callingNodeNG.inferred
(see stack trace below). Unfortunately, I do not have the code that results in theKeyError
when inferring - yet.Stack trace
Theory
I have the following theory, the code fails with a
KeyError
on theyield
here:Which is odd, as first the
key
is checked to be in the inference cache. Maybe, thekey in context.inferred
checks if thekey
is 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_checkable
is 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
KeyError
was unlucky timing.Solution
Use the key once on the inference cache by using
.get