Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a10b67

Browse files
Add support for lazy import parsing (#23755)
## Summary This PR adds `is_lazy` to the AST and parser, and supports `lazy` import formatting in the formatter. The name `is_lazy` matches the [CPython AST](https://github.com/python/cpython/blob/c3fb0d9d96902774c08b199dda0479a8d31398a5/Parser/Python.asdl#L48), though we could modify it if there's demand. There are no further changes to Ruff rules or even to Ruff import sorting, since those deserve separate design discussions. I've also omitted support for flagging semantic syntax errors (e.g., `lazy import` inside `def`). See: #21305.
1 parent d9daf6d commit 8a10b67

67 files changed

Lines changed: 839 additions & 46 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/ruff_graph/src/collector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
4646
names,
4747
module,
4848
level,
49+
is_lazy: _,
4950
range: _,
5051
node_index: _,
5152
}) => {
@@ -89,6 +90,7 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
8990
}
9091
Stmt::Import(ast::StmtImport {
9192
names,
93+
is_lazy: _,
9294
range: _,
9395
node_index: _,
9496
}) => {

crates/ruff_linter/src/checkers/ast/analyze/statement.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
532532
}
533533
Stmt::Import(ast::StmtImport {
534534
names,
535+
is_lazy: _,
535536
range: _,
536537
node_index: _,
537538
}) => {
@@ -690,6 +691,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
690691
names,
691692
module,
692693
level,
694+
is_lazy: _,
693695
range: _,
694696
node_index: _,
695697
},

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
10021002
}
10031003
Stmt::Import(ast::StmtImport {
10041004
names,
1005+
is_lazy: _,
10051006
range: _,
10061007
node_index: _,
10071008
}) => {
@@ -1057,6 +1058,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
10571058
names,
10581059
module,
10591060
level,
1061+
is_lazy: _,
10601062
range: _,
10611063
node_index: _,
10621064
}) => {

crates/ruff_linter/src/importer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ impl<'a> Importer<'a> {
457457
module: name,
458458
names,
459459
level,
460+
is_lazy: _,
460461
range: _,
461462
node_index: _,
462463
}) = stmt

crates/ruff_linter/src/rules/flake8_tidy_imports/rules/relative_imports.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn fix_banned_relative_import(
9191
return None;
9292
}
9393

94-
let Stmt::ImportFrom(ast::StmtImportFrom { names, .. }) = stmt else {
94+
let Stmt::ImportFrom(ast::StmtImportFrom { names, is_lazy, .. }) = stmt else {
9595
panic!("Expected Stmt::ImportFrom");
9696
};
9797
let node = ast::StmtImportFrom {
@@ -101,6 +101,7 @@ fn fix_banned_relative_import(
101101
)),
102102
names: names.clone(),
103103
level: 0,
104+
is_lazy: *is_lazy,
104105
range: TextRange::default(),
105106
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
106107
};

crates/ruff_linter/src/rules/isort/annotate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) fn annotate_imports<'a>(
2626
Stmt::Import(ast::StmtImport {
2727
names,
2828
range,
29+
is_lazy: _,
2930
node_index: _,
3031
}) => {
3132
// Find comments above.
@@ -62,6 +63,7 @@ pub(crate) fn annotate_imports<'a>(
6263
module,
6364
names,
6465
level,
66+
is_lazy: _,
6567
range: _,
6668
node_index: _,
6769
}) => {

crates/ruff_linter/src/rules/isort/rules/add_required_imports.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn includes_import(stmt: &Stmt, target: &NameImport) -> bool {
6161
NameImport::Import(target) => {
6262
let Stmt::Import(ast::StmtImport {
6363
names,
64+
is_lazy: _,
6465
range: _,
6566
node_index: _,
6667
}) = &stmt
@@ -77,6 +78,7 @@ fn includes_import(stmt: &Stmt, target: &NameImport) -> bool {
7778
module,
7879
names,
7980
level,
81+
is_lazy: _,
8082
range: _,
8183
node_index: _,
8284
}) = &stmt

crates/ruff_linter/src/rules/pylint/rules/manual_import_from.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub(crate) fn manual_from_import(checker: &Checker, stmt: &Stmt, alias: &Alias,
8383
alias.range(),
8484
);
8585
if names.len() == 1 {
86+
let is_lazy = stmt
87+
.as_import_stmt()
88+
.is_some_and(|import_stmt| import_stmt.is_lazy);
8689
let node = ast::StmtImportFrom {
8790
module: Some(Identifier::new(module.to_string(), TextRange::default())),
8891
names: vec![Alias {
@@ -92,6 +95,7 @@ pub(crate) fn manual_from_import(checker: &Checker, stmt: &Stmt, alias: &Alias,
9295
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
9396
}],
9497
level: 0,
98+
is_lazy,
9599
range: TextRange::default(),
96100
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
97101
};

crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_c_element_tree.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub(crate) fn deprecated_c_element_tree(checker: &Checker, stmt: &Stmt) {
5757
match stmt {
5858
Stmt::Import(ast::StmtImport {
5959
names,
60+
is_lazy: _,
6061
range: _,
6162
node_index: _,
6263
}) => {
@@ -71,6 +72,7 @@ pub(crate) fn deprecated_c_element_tree(checker: &Checker, stmt: &Stmt) {
7172
module,
7273
names,
7374
level,
75+
is_lazy: _,
7476
range: _,
7577
node_index: _,
7678
}) => {

crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_mock_import.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ pub(crate) fn deprecated_mock_import(checker: &Checker, stmt: &Stmt) {
285285
match stmt {
286286
Stmt::Import(ast::StmtImport {
287287
names,
288+
is_lazy: _,
288289
range: _,
289290
node_index: _,
290291
}) => {

0 commit comments

Comments
 (0)