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

Parses a list of with items. See:

(&mut self)

Source from the content-addressed store, hash-verified

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