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:2236–2330  ·  view source on GitHub ↗

Parses a list of with items. See:

(&mut self)

Source from the content-addressed store, hash-verified

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