Skip to content

Commit 94a004e

Browse files
Avoid collapsing elif and else branches during import sorting (#5964)
## Summary I ran into this in the wild. It looks like Ruff will collapse the `else` and `elif` branches here (i.e., it doesn't recognize that they're too independent import blocks): ```python if "sdist" in cmds: _sdist = cmds["sdist"] elif "setuptools" in sys.modules: from setuptools.command.sdist import sdist as _sdist else: from setuptools.command.sdist import sdist as _sdist from distutils.command.sdist import sdist as _sdist ``` Likely fallout from the `elif_else_branches` refactor.
1 parent aaf7f36 commit 94a004e

File tree

6 files changed

+90
-2
lines changed

6 files changed

+90
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if "sdist" in cmds:
2+
_sdist = cmds["sdist"]
3+
elif "setuptools" in sys.modules:
4+
from setuptools.command.sdist import sdist as _sdist
5+
else:
6+
from setuptools.command.sdist import sdist as _sdist
7+
from distutils.command.sdist import sdist as _sdist
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
match 1:
2+
case 1:
3+
import sys
4+
import os
5+
case 2:
6+
import collections
7+
import abc

crates/ruff/src/rules/isort/block.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ruff_text_size::{TextRange, TextSize};
2-
use rustpython_parser::ast::{self, ExceptHandler, MatchCase, Ranged, Stmt};
2+
use rustpython_parser::ast::{self, ElifElseClause, ExceptHandler, MatchCase, Ranged, Stmt};
33
use std::iter::Peekable;
44
use std::slice;
55

@@ -254,7 +254,6 @@ where
254254
for clause in elif_else_clauses {
255255
self.visit_elif_else_clause(clause);
256256
}
257-
self.finalize(None);
258257
}
259258
Stmt::With(ast::StmtWith { body, .. }) => {
260259
for stmt in body {
@@ -331,4 +330,11 @@ where
331330
}
332331
self.finalize(None);
333332
}
333+
334+
fn visit_elif_else_clause(&mut self, elif_else_clause: &'b ElifElseClause) {
335+
for stmt in &elif_else_clause.body {
336+
self.visit_stmt(stmt);
337+
}
338+
self.finalize(None);
339+
}
334340
}

crates/ruff/src/rules/isort/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,15 @@ mod tests {
322322
#[test_case(Path::new("force_sort_within_sections.py"))]
323323
#[test_case(Path::new("force_to_top.py"))]
324324
#[test_case(Path::new("force_wrap_aliases.py"))]
325+
#[test_case(Path::new("if_elif_else.py"))]
325326
#[test_case(Path::new("import_from_after_import.py"))]
326327
#[test_case(Path::new("inline_comments.py"))]
327328
#[test_case(Path::new("insert_empty_lines.py"))]
328329
#[test_case(Path::new("insert_empty_lines.pyi"))]
329330
#[test_case(Path::new("isort_skip_file.py"))]
330331
#[test_case(Path::new("leading_prefix.py"))]
331332
#[test_case(Path::new("magic_trailing_comma.py"))]
333+
#[test_case(Path::new("match_case.py"))]
332334
#[test_case(Path::new("natural_order.py"))]
333335
#[test_case(Path::new("no_lines_before.py"))]
334336
#[test_case(Path::new("no_reorder_within_section.py"))]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff/src/rules/isort/mod.rs
3+
---
4+
if_elif_else.py:6:1: I001 [*] Import block is un-sorted or un-formatted
5+
|
6+
4 | from setuptools.command.sdist import sdist as _sdist
7+
5 | else:
8+
6 | / from setuptools.command.sdist import sdist as _sdist
9+
7 | | from distutils.command.sdist import sdist as _sdist
10+
|
11+
= help: Organize imports
12+
13+
Fix
14+
3 3 | elif "setuptools" in sys.modules:
15+
4 4 | from setuptools.command.sdist import sdist as _sdist
16+
5 5 | else:
17+
6 |- from setuptools.command.sdist import sdist as _sdist
18+
7 6 | from distutils.command.sdist import sdist as _sdist
19+
7 |+
20+
8 |+ from setuptools.command.sdist import sdist as _sdist
21+
22+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
source: crates/ruff/src/rules/isort/mod.rs
3+
---
4+
match_case.py:3:1: I001 [*] Import block is un-sorted or un-formatted
5+
|
6+
1 | match 1:
7+
2 | case 1:
8+
3 | / import sys
9+
4 | | import os
10+
5 | | case 2:
11+
| |_^ I001
12+
6 | import collections
13+
7 | import abc
14+
|
15+
= help: Organize imports
16+
17+
Fix
18+
1 1 | match 1:
19+
2 2 | case 1:
20+
3 |+ import os
21+
3 4 | import sys
22+
4 |- import os
23+
5 5 | case 2:
24+
6 6 | import collections
25+
7 7 | import abc
26+
27+
match_case.py:6:1: I001 [*] Import block is un-sorted or un-formatted
28+
|
29+
4 | import os
30+
5 | case 2:
31+
6 | / import collections
32+
7 | | import abc
33+
|
34+
= help: Organize imports
35+
36+
Fix
37+
3 3 | import sys
38+
4 4 | import os
39+
5 5 | case 2:
40+
6 |+ import abc
41+
6 7 | import collections
42+
7 |- import abc
43+
44+

0 commit comments

Comments
 (0)