MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_with_items

Method parse_with_items

crates/ruff_python_parser/src/parser/statement.rs:2231–2325  ·  view source on GitHub ↗

Parses a list of with items. See:

(&mut self)

Source from the content-addressed store, hash-verified

2229 ///
2230 /// See: <https://docs.python.org/3/reference/compound_stmts.html#the-with-statement>
2231 fn parse_with_items(&mut self) -> Vec<WithItem> {
2232 if !self.at_expr() {
2233 self.add_error(
2234 ParseErrorType::OtherError(
2235 "Expected the start of an expression after `with` keyword".to_string(),
2236 ),
2237 self.current_token_range(),
2238 );
2239 return vec![];
2240 }
2241
2242 let open_paren_range = self.current_token_range();
2243
2244 if self.at(TokenKind::Lpar) {
2245 if let (Some(items), has_trailing_comma) = self.try_parse_parenthesized_with_items() {
2246 // test_ok tuple_context_manager_py38
2247 // # parse_options: {"target-version": "3.8"}
2248 // with (
2249 // foo,
2250 // bar,
2251 // baz,
2252 // ) as tup: ...
2253
2254 // test_ok single_parenthesized_item_context_manager_py38
2255 // # parse_options: {"target-version": "3.8"}
2256 // with (
2257 // open('foo.txt')) as foo: ...
2258 // with (
2259 // open('foo.txt')): ...
2260
2261 // test_err tuple_context_manager_py38
2262 // # parse_options: {"target-version": "3.8"}
2263 // # these cases are _syntactically_ valid before Python 3.9 because the `with` item
2264 // # is parsed as a tuple, but this will always cause a runtime error, so we flag it
2265 // # anyway
2266 // with (foo, bar): ...
2267 // with (
2268 // foo,
2269 // bar,
2270 // baz,
2271 // ): ...
2272 // with (foo,): ...
2273
2274 // test_ok parenthesized_context_manager_py39
2275 // # parse_options: {"target-version": "3.9"}
2276 // with (foo as x, bar as y): ...
2277 // with (foo, bar as y): ...
2278 // with (foo as x, bar): ...
2279
2280 // test_err parenthesized_context_manager_py38
2281 // # parse_options: {"target-version": "3.8"}
2282 // with (foo as x, bar as y): ...
2283 // with (foo, bar as y): ...
2284 // with (foo as x, bar): ...
2285 if items.len() > 1 || has_trailing_comma {
2286 self.add_unsupported_syntax_error(
2287 UnsupportedSyntaxErrorKind::ParenthesizedContextManager,
2288 open_paren_range,

Callers 1

parse_with_statementMethod · 0.80

Calls 11

at_exprMethod · 0.80
current_token_rangeMethod · 0.80
expectMethod · 0.80
parse_with_itemMethod · 0.80
add_errorMethod · 0.45
to_stringMethod · 0.45
atMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected