1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from typing import Any
import libcst as cst
from libcst import parse_statement, PartialParserConfig
from libcst._nodes.tests.base import CSTNodeTest, DummyIndentedBlock, parse_statement_as
from libcst.metadata import CodeRange
from libcst.testing.utils import data_provider
class WithTest(CSTNodeTest):
maxDiff: int = 2000
@data_provider(
(
# Simple with block
{
"node": cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
),
"code": "with context_mgr(): pass\n",
"parser": parse_statement,
"expected_position": CodeRange((1, 0), (1, 24)),
},
# Simple async with block
{
"node": cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
asynchronous=cst.Asynchronous(),
),
"code": "async with context_mgr(): pass\n",
"parser": lambda code: parse_statement(
code, config=PartialParserConfig(python_version="3.7")
),
},
# Python 3.6 async with block
{
"node": cst.FunctionDef(
cst.Name("foo"),
cst.Parameters(),
cst.IndentedBlock(
(
cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
asynchronous=cst.Asynchronous(),
),
)
),
asynchronous=cst.Asynchronous(),
),
"code": "async def foo():\n async with context_mgr(): pass\n",
"parser": lambda code: parse_statement(
code, config=PartialParserConfig(python_version="3.6")
),
},
# Multiple context managers
{
"node": cst.With(
(
cst.WithItem(cst.Call(cst.Name("foo"))),
cst.WithItem(cst.Call(cst.Name("bar"))),
),
cst.SimpleStatementSuite((cst.Pass(),)),
),
"code": "with foo(), bar(): pass\n",
"parser": None,
},
{
"node": cst.With(
(
cst.WithItem(
cst.Call(cst.Name("foo")),
comma=cst.Comma(whitespace_after=cst.SimpleWhitespace(" ")),
),
cst.WithItem(cst.Call(cst.Name("bar"))),
),
cst.SimpleStatementSuite((cst.Pass(),)),
),
"code": "with foo(), bar(): pass\n",
"parser": parse_statement,
},
# With block containing variable for context manager.
{
"node": cst.With(
(
cst.WithItem(
cst.Call(cst.Name("context_mgr")),
cst.AsName(cst.Name("ctx")),
),
),
cst.SimpleStatementSuite((cst.Pass(),)),
),
"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(
" ",
cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
),
),
"code": " with context_mgr(): pass\n",
"parser": None,
"expected_position": CodeRange((1, 4), (1, 28)),
},
# with an indented body
{
"node": DummyIndentedBlock(
" ",
cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.IndentedBlock((cst.SimpleStatementLine((cst.Pass(),)),)),
),
),
"code": " with context_mgr():\n pass\n",
"parser": None,
"expected_position": CodeRange((1, 4), (2, 12)),
},
# leading_lines
{
"node": cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
leading_lines=(
cst.EmptyLine(comment=cst.Comment("# leading comment")),
),
),
"code": "# leading comment\nwith context_mgr(): pass\n",
"parser": parse_statement,
"expected_position": CodeRange((2, 0), (2, 24)),
},
# Whitespace
{
"node": cst.With(
(
cst.WithItem(
cst.Call(cst.Name("context_mgr")),
cst.AsName(
cst.Name("ctx"),
whitespace_before_as=cst.SimpleWhitespace(" "),
whitespace_after_as=cst.SimpleWhitespace(" "),
),
),
),
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_with=cst.SimpleWhitespace(" "),
whitespace_before_colon=cst.SimpleWhitespace(" "),
),
"code": "with context_mgr() as ctx : pass\n",
"parser": parse_statement,
"expected_position": CodeRange((1, 0), (1, 36)),
},
# Weird spacing rules, that parse differently depending on whether
# we are using a grammar that included parenthesized with statements.
{
"node": cst.With(
(
cst.WithItem(
cst.Call(
cst.Name("context_mgr"),
lpar=(),
rpar=(),
)
),
),
cst.SimpleStatementSuite((cst.Pass(),)),
lpar=(cst.LeftParen()),
rpar=(cst.RightParen()),
whitespace_after_with=cst.SimpleWhitespace(""),
),
"code": "with(context_mgr()): pass\n",
"parser": parse_statement,
"expected_position": CodeRange((1, 0), (1, 25)),
},
# Multi-line parenthesized with.
{
"node": cst.With(
(
cst.WithItem(
cst.Call(cst.Name("foo")),
comma=cst.Comma(
whitespace_after=cst.ParenthesizedWhitespace(
first_line=cst.TrailingWhitespace(
whitespace=cst.SimpleWhitespace(
value="",
),
comment=None,
newline=cst.Newline(
value=None,
),
),
empty_lines=[],
indent=True,
last_line=cst.SimpleWhitespace(
value=" ",
),
)
),
),
cst.WithItem(cst.Call(cst.Name("bar")), comma=cst.Comma()),
),
cst.SimpleStatementSuite((cst.Pass(),)),
lpar=cst.LeftParen(whitespace_after=cst.SimpleWhitespace(" ")),
rpar=cst.RightParen(whitespace_before=cst.SimpleWhitespace(" ")),
),
"code": ("with ( foo(),\n" " bar(), ): pass\n"), # noqa
"parser": parse_statement,
"expected_position": CodeRange((1, 0), (2, 21)),
},
)
)
def test_valid(self, **kwargs: Any) -> None:
self.validate_node(**kwargs)
@data_provider(
(
{
"get_node": lambda: cst.With(
(), cst.IndentedBlock((cst.SimpleStatementLine((cst.Pass(),)),))
),
"expected_re": "A With statement must have at least one WithItem",
},
{
"get_node": lambda: cst.With(
(
cst.WithItem(
cst.Call(cst.Name("foo")),
comma=cst.Comma(whitespace_after=cst.SimpleWhitespace(" ")),
),
),
cst.IndentedBlock((cst.SimpleStatementLine((cst.Pass(),)),)),
),
"expected_re": "The last WithItem in an unparenthesized With cannot "
+ "have a trailing comma.",
},
{
"get_node": lambda: cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_with=cst.SimpleWhitespace(""),
),
"expected_re": "Must have at least one space after with keyword",
},
{
"get_node": lambda: cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_with=cst.SimpleWhitespace(""),
lpar=cst.LeftParen(),
),
"expected_re": "Do not mix concrete LeftParen/RightParen with "
+ "MaybeSentinel",
},
{
"get_node": lambda: cst.With(
(cst.WithItem(cst.Call(cst.Name("context_mgr"))),),
cst.SimpleStatementSuite((cst.Pass(),)),
whitespace_after_with=cst.SimpleWhitespace(""),
rpar=cst.RightParen(),
),
"expected_re": "Do not mix concrete LeftParen/RightParen with "
+ "MaybeSentinel",
},
)
)
def test_invalid(self, **kwargs: Any) -> None:
self.assert_invalid(**kwargs)
@data_provider(
(
{
"code": "with a, b: pass",
"parser": parse_statement_as(python_version="3.1"),
"expect_success": True,
},
{
"code": "with a, b: pass",
"parser": parse_statement_as(python_version="3.0"),
"expect_success": False,
},
)
)
def test_versions(self, **kwargs: Any) -> None:
if not kwargs.get("expect_success", True):
self.skipTest("parse errors are disabled for native parser")
self.assert_parses(**kwargs)
def test_adding_parens(self) -> None:
node = cst.With(
(
cst.WithItem(
cst.Call(cst.Name("foo")),
comma=cst.Comma(
whitespace_after=cst.ParenthesizedWhitespace(),
),
),
cst.WithItem(cst.Call(cst.Name("bar")), comma=cst.Comma()),
),
cst.SimpleStatementSuite((cst.Pass(),)),
lpar=cst.LeftParen(whitespace_after=cst.SimpleWhitespace(" ")),
rpar=cst.RightParen(whitespace_before=cst.SimpleWhitespace(" ")),
)
module = cst.Module([])
self.assertEqual(
module.code_for_node(node), ("with ( foo(),\n" "bar(), ): pass\n") # noqa
)
|