Allow no spaces after as in a contextmanager in certain cases

Like in `with foo()as():pass`

Raised in #930.
This commit is contained in:
Zsolt Dollenstein 2023-05-28 10:47:51 +01:00
parent aa5167189c
commit 4ce3660127
No known key found for this signature in database
GPG key ID: 09F75E762C27AAD9
2 changed files with 21 additions and 1 deletions

View file

@ -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."
)

View file

@ -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(