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

Method parse_import_statement

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

Parses an import statement. # Panics If the parser isn't positioned at an `import` token. See:

(&mut self, start: TextSize, is_lazy: bool)

Source from the content-addressed store, hash-verified

606 ///
607 /// See: <https://docs.python.org/3/reference/simple_stmts.html#the-import-statement>
608 fn parse_import_statement(&mut self, start: TextSize, is_lazy: bool) -> ast::StmtImport {
609 self.bump(TokenKind::Import);
610
611 // test_err import_stmt_parenthesized_names
612 // import (a)
613 // import (a, b)
614
615 // test_err import_stmt_star_import
616 // import *
617 // import x, *, y
618
619 // test_err import_stmt_trailing_comma
620 // import ,
621 // import x, y,
622
623 let mut names = self.parse_comma_separated_list_into_vec_with_capacity(
624 RecoveryContextKind::ImportNames,
625 |p| p.parse_alias(ImportStyle::Import),
626 1,
627 );
628
629 if names.is_empty() {
630 // test_err import_stmt_empty
631 // import
632 self.add_error(ParseErrorType::EmptyImportNames, self.current_token_range());
633 }
634
635 names.shrink_to_fit();
636
637 ast::StmtImport {
638 names,
639 is_lazy,
640 range: self.node_range(start),
641 node_index: AtomicNodeIndex::NONE,
642 }
643 }
644
645 /// Parses a `from` import statement.
646 ///

Callers 1

Calls 8

parse_aliasMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
is_emptyMethod · 0.45
add_errorMethod · 0.45
shrink_to_fitMethod · 0.45

Tested by

no test coverage detected