Fix metadata dependency cache leaking across the MRO - #1468
Open
Sanjays2402 wants to merge 1 commit into
Open
Conversation
get_inherited_dependencies() memoized into a plain class attribute and read it back with ordinary attribute lookup, which walks the MRO. Running a metadata pass with a bare CSTTransformer/CSTVisitor therefore cached an empty frozenset on the base class, and every subclass that had not yet computed its own cache inherited that empty set, raising a spurious METADATA_DEPENDENCIES KeyError. Read only the class's own __dict__ entry so a base-class cache can never be inherited. Memoization and its per-class semantics are unchanged. Adds a regression test that resolves a bare CSTTransformer before a subclass declaring a provider.
Author
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.
Closes #1459
Summary
MetadataDependent.get_inherited_dependencies()memoized into a plain classattribute and read it back with ordinary attribute lookup, which walks the MRO.
Resolving a bare
CSTTransformer/CSTVisitorduring a metadata pass thereforecached an empty
frozensetonto the base class, and every subclass that had notyet computed its own cache inherited that empty set — raising a spurious
METADATA_DEPENDENCIESKeyErroreven though the subclass declared itsprovider correctly. The poisoning is process-wide and surfaces far from its
cause.
The fix reads only the class's own
__dict__entry, so a base-class cache cannever be inherited. Memoization and its per-class semantics are unchanged.
Test Plan
Added
test_bare_base_class_does_not_poison_inherited_metadatainlibcst/metadata/tests/test_metadata_provider.py, beside the existingtest_inherited_metadata: it resolves a bareCSTTransformerfirst, then asubclass declaring a provider. Stash-verified — the test fails without the
source change (the exact reported
KeyError) and passes with it.libcst/metadata/tests/is 174 passed; the 3 errors are the pre-existing pyretest_type_inference_providercases that need the pyre binary.This change was prepared with AI assistance; the regression test was run locally
and fails without the fix.