Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ExpressionContextProvider] Make subscript values have a LOAD context #319

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libcst/codemod/commands/tests/test_remove_unused_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ def foo() -> None:
"""

self.assertCodemod(before, after)

def test_access_in_assignment(self) -> None:
before = """
from a import b
b(0)[x] = False
"""
self.assertCodemod(before, before)
4 changes: 3 additions & 1 deletion libcst/metadata/expression_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def visit_Attribute(self, node: cst.Attribute) -> bool:

def visit_Subscript(self, node: cst.Subscript) -> bool:
self.provider.set_metadata(node, self.context)
node.value.visit(self)
node.value.visit(
ExpressionContextVisitor(self.provider, ExpressionContext.LOAD)
)
slice = node.slice
if isinstance(slice, Sequence):
for sli in slice:
Expand Down
24 changes: 20 additions & 4 deletions libcst/metadata/tests/test_expression_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def visit_Name(self, node: cst.Name) -> None:
self.test.assertEqual(
self.get_metadata(ExpressionContextProvider, node, None),
self.name_to_context[node.value],
f"Context doesn't match for Name {node.value}",
)

def visit_Attribute(self, node: cst.Attribute) -> None:
Expand Down Expand Up @@ -148,7 +149,7 @@ def test_assign_with_subscript(self) -> None:
DependentVisitor(
test=self,
name_to_context={
"a": ExpressionContext.STORE,
"a": ExpressionContext.LOAD,
"b": ExpressionContext.LOAD,
"c": ExpressionContext.LOAD,
"d": ExpressionContext.LOAD,
Expand Down Expand Up @@ -226,7 +227,7 @@ def test_del_with_subscript(self) -> None:
DependentVisitor(
test=self,
name_to_context={
"a": ExpressionContext.DEL,
"a": ExpressionContext.LOAD,
"b": ExpressionContext.LOAD,
},
subscript_to_context={"a[b]": ExpressionContext.DEL},
Expand Down Expand Up @@ -278,7 +279,7 @@ def test_nested_tuple_with_assign(self) -> None:
)
)

def test_list_with_assing(self) -> None:
def test_list_with_assign(self) -> None:
wrapper = MetadataWrapper(parse_module("[a] = [b]"))
wrapper.visit(
DependentVisitor(
Expand All @@ -294,7 +295,7 @@ def test_list_with_assing(self) -> None:
)
)

def test_nested_list_with_assing(self) -> None:
def test_nested_list_with_assign(self) -> None:
wrapper = MetadataWrapper(parse_module("[[a, b], c] = [[d, e], f]"))
wrapper.visit(
DependentVisitor(
Expand All @@ -316,6 +317,21 @@ def test_nested_list_with_assing(self) -> None:
)
)

def test_expressions_with_assign(self) -> None:
wrapper = MetadataWrapper(parse_module("f(a)[b] = c"))
wrapper.visit(
DependentVisitor(
test=self,
name_to_context={
"a": ExpressionContext.LOAD,
"b": ExpressionContext.LOAD,
"c": ExpressionContext.LOAD,
"f": ExpressionContext.LOAD,
},
subscript_to_context={"f(a)[b]": ExpressionContext.STORE},
)
)

def test_invalid_type_for_context(self) -> None:
wrapper = MetadataWrapper(parse_module("a()"))
wrapper.visit(
Expand Down