Repository navigation
Fix unexpected ExpressionContext.LOAD for keyword arguments - #1478
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
In Python's ast module, keyword argument names in Call nodes are identifiers (`ast.keyword.arg`), not expression nodes, and do not carry an expression context (`ast.expr_context`). In LibCST, `Arg.keyword` is an `Optional[Name]`. Previously, `ExpressionContextVisitor` did not implement `visit_Arg`, causing the AST traversal to walk into `Arg.keyword` and mistakenly assign `ExpressionContext.LOAD` to the argument's name. Add `visit_Arg` to `ExpressionContextVisitor` so that only `Arg.value` is visited for expression contexts while leaving `Arg.keyword` uncontexted, matching the behavior documented in `ExpressionContextProvider` and analogous to `Attribute.attr`. Add a unit regression test verifying keyword arguments in Call expressions. Closes Instagram#1217. Signed-off-by: reginaldalfret <reginaldalfret@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.
Summary
Fixes #1217.
In Python's
astmodule, keyword argument names inCall(func, args, keywords=[keyword(arg="kwarg", value=...)])are plain identifiers (str), not expression nodes, and do not carry anexpr_context(ast.Load,ast.Store, etc.).In LibCST,
Arg.keywordis anOptional[Name]. Previously,ExpressionContextVisitordid not implementvisit_Arg. Because default traversal visits all child fields,visit_Namewas invoked onArg.keywordand mistakenly assignedExpressionContext.LOADto the argument name. This produced unexpected results for codemods and linters inspecting unqualified variable loads (e.g. interpretingkwarg=1as a load ofkwarg).Changes
libcst/metadata/expression_context_provider.py:visit_Arg(self, node: cst.Arg) -> booltoExpressionContextVisitor, ensuring onlynode.valueis visited for expression context resolution while leavingnode.keyworduncontexted (matching the rationale forAttribute.attralready documented inExpressionContextProvider).libcst/metadata/tests/test_expression_context_provider.py:test_call_keyword_argumentverifying that infn(kwarg=x, other=1),fnandxreceiveExpressionContext.LOAD, whereaskwargandotherdo not receive an expression context.Verification
test_expression_context_provider, 177 tests inlibcst.metadata.tests, and 144 tests inlibcst.matchers.testspassed cleanly.ufmt check.