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

Parses a list of with items. See:

(&mut self)

Source from the content-addressed store, hash-verified

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