From 4ce3660127c39b565460a48d6b9cd2504a588c7d Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Sun, 28 May 2023 10:47:51 +0100 Subject: [PATCH] Allow no spaces after `as` in a contextmanager in certain cases Like in `with foo()as():pass` Raised in #930. --- libcst/_nodes/statement.py | 5 ++++- libcst/_nodes/tests/test_with.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libcst/_nodes/statement.py b/libcst/_nodes/statement.py index 8cd171e0..de5161fa 100644 --- a/libcst/_nodes/statement.py +++ b/libcst/_nodes/statement.py @@ -745,7 +745,10 @@ class AsName(CSTNode): whitespace_after_as: BaseParenthesizableWhitespace = SimpleWhitespace.field(" ") def _validate(self) -> None: - if self.whitespace_after_as.empty: + if ( + self.whitespace_after_as.empty + and not self.name._safe_to_use_with_word_operator(ExpressionPosition.RIGHT) + ): raise CSTValidationError( "There must be at least one space between 'as' and name." ) diff --git a/libcst/_nodes/tests/test_with.py b/libcst/_nodes/tests/test_with.py index 1310b3f8..517ce357 100644 --- a/libcst/_nodes/tests/test_with.py +++ b/libcst/_nodes/tests/test_with.py @@ -102,6 +102,23 @@ class WithTest(CSTNodeTest): "code": "with context_mgr() as ctx: pass\n", "parser": parse_statement, }, + { + "node": cst.With( + ( + cst.WithItem( + cst.Call(cst.Name("context_mgr")), + cst.AsName( + cst.Tuple(()), + whitespace_after_as=cst.SimpleWhitespace(""), + whitespace_before_as=cst.SimpleWhitespace(""), + ), + ), + ), + cst.SimpleStatementSuite((cst.Pass(),)), + ), + "code": "with context_mgr()as(): pass\n", + "parser": parse_statement, + }, # indentation { "node": DummyIndentedBlock(