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

Walrus operator's left hand side now has STORE expression context #433

Merged
merged 6 commits into from
Dec 16, 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/metadata/expression_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ def visit_AugAssign(self, node: cst.AugAssign) -> bool:
node.value.visit(self)
return False

def visit_NamedExpr(self, node: cst.NamedExpr) -> bool:
node.target.visit(
ExpressionContextVisitor(self.provider, ExpressionContext.STORE)
)
node.value.visit(self)
return False

def visit_Name(self, node: cst.Name) -> bool:
self.provider.set_metadata(node, self.context)
return False
Expand Down
20 changes: 20 additions & 0 deletions libcst/metadata/tests/test_expression_context_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,23 @@ def test_function(self) -> None:
},
)
)

def test_walrus(self) -> None:
code = """
if x := y:
pass
"""
wrapper = MetadataWrapper(
parse_module(
dedent(code), config=cst.PartialParserConfig(python_version="3.8")
)
)
wrapper.visit(
DependentVisitor(
test=self,
name_to_context={
"x": ExpressionContext.STORE,
"y": ExpressionContext.LOAD,
},
)
)
22 changes: 22 additions & 0 deletions libcst/metadata/tests/test_scope_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# LICENSE file in the root directory of this source tree.


import sys
from textwrap import dedent
from typing import Mapping, Tuple, cast

Expand Down Expand Up @@ -1609,6 +1610,27 @@ def test_no_out_of_order_references_in_global_scope(self) -> None:
),
)

def test_walrus_accesses(self) -> None:
if sys.version_info < (3, 8):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not as easy, I'd have to plumb the parser config through some helper functions, so I thought it's fine to keep it as is

self.skipTest("This python version doesn't support :=")
m, scopes = get_scope_metadata_provider(
"""
if x := y:
y = 1
x
"""
)
for scope in scopes.values():
for acc in scope.accesses:
self.assertEqual(
len(acc.referents),
1 if getattr(acc.node, "value") == "x" else 0,
msg=(
"Access for node has incorrect number of referents: "
+ f"{acc.node}"
),
)

def test_cast(self) -> None:
with self.assertRaises(cst.ParserSyntaxError):
m, scopes = get_scope_metadata_provider(
Expand Down