Skip to content

Commit

Permalink
fix: spaces around walrus operator are not required (#368)
Browse files Browse the repository at this point in the history
Fixes #367
  • Loading branch information
sk- authored Aug 12, 2020
1 parent 771d5e1 commit b4e04ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
13 changes: 0 additions & 13 deletions libcst/_nodes/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3680,19 +3680,6 @@ class NamedExpr(BaseExpression):
#: Whitespace after the walrus operator, but before the value.
whitespace_after_walrus: BaseParenthesizableWhitespace = SimpleWhitespace.field(" ")

def _validate(self) -> None:
super(NamedExpr, self)._validate()
if (
self.whitespace_before_walrus.empty
and not self.target._safe_to_use_with_word_operator(ExpressionPosition.LEFT)
):
raise CSTValidationError("Must have at least one space after target.")
if (
self.whitespace_after_walrus.empty
and not self.value._safe_to_use_with_word_operator(ExpressionPosition.RIGHT)
):
raise CSTValidationError("Must have at least one space before value.")

def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "NamedExpr":
return NamedExpr(
lpar=visit_sequence(self, "lpar", self.lpar, visitor),
Expand Down
14 changes: 14 additions & 0 deletions libcst/_nodes/tests/test_namedexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ class NamedExprTest(CSTNodeTest):
"parser": _parse_statement_force_38,
"expected_position": None,
},
{
"node": cst.If(
test=cst.NamedExpr(
target=cst.Name(value="x"),
value=cst.Integer(value="1"),
whitespace_before_walrus=cst.SimpleWhitespace(""),
whitespace_after_walrus=cst.SimpleWhitespace(""),
),
body=cst.SimpleStatementSuite(body=[cst.Pass()]),
),
"code": "if x:=1: pass\n",
"parser": _parse_statement_force_38,
"expected_position": None,
},
)
)
def test_valid(self, **kwargs: Any) -> None:
Expand Down

0 comments on commit b4e04ea

Please sign in to comment.