Repository navigation
Keep an import that follows an indented import block with lines_before_imports - #2672
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2672 +/- ##
=======================================
Coverage 99.37% 99.37%
=======================================
Files 40 40
Lines 3223 3223
Branches 689 689
=======================================
Hits 3203 3203
Misses 12 12
Partials 8 8 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Explicit spacing remains off by one between transitioned import sections.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Prevents lines_before_imports from dropping imports queued after indented import blocks.
Changes:
- Flushes the current section when another indentation level is pending.
- Adds regression coverage for minimal and issue #2010 cases.
| File | Description |
|---|---|
isort/core.py |
Prevents incorrect blank-line deferral. |
tests/unit/test_regressions.py |
Tests preservation and idempotence. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
8e62704 to
85ddd4d
Compare
|
Rebased onto |
…e_imports When an import at a shallower indentation starts a new section, line is emptied, and the lines_before_imports handling took it for a blank line and deferred it. The indented section was then only flushed at end of file and the section queued behind it was never written, so the import was lost.
…rts is set The section that follows already gets exactly lines_before_imports blank lines, so the transition separator made it one more than configured. The regression test now checks the full output for 0, 1 and 2.
bb54e8e to
366824d
Compare

Refs #2010 (the part where the trailing import disappears).
With
lines_before_importsset, isort deletes a top-level import that follows an indented import block:import bis gone, with no error. Withoutlines_before_importsthe same input comes back unchanged. The same thing happens to the file from #2010 when its configuration (which setslines_before_imports=1) is used: the trailingimport third_party_librarydisappears. Reproduced onmain(f00e0919) and on the released 9.0.1, 6.0.1 and 5.13.2.Why
When an import at a shallower indentation than the current section arrives,
process()queues it asnext_import_sectionand setsline = "", so that the current section is flushed on this iteration and the queued one becomes the current section.The
lines_before_importshandling that runs next defers blank lines (if line.strip() == "" and not end_of_file: lines_before += line; continue), and it took that emptiedlinefor a blank line. The iteration was skipped, so the indented section was only flushed at the end of the file. The end-of-file pass flushes one section and then promotesnext_import_section, but there is no later iteration to write it, so the queued import was lost.The change
Only defer a blank line when no
next_import_sectionis pending. One condition incore.py, with a comment saying why.Test
test_lines_before_imports_keeps_import_after_indented_blockcovers the minimal case and the one from #2010, and checks that the output is stable when isort runs on it again. Onmainit fails:tests/unitin a clean container: 652 passed, 1 skipped with the change. The same 2 tests fail with and without it (test_isort_supports_shared_profiles_issue_970,test_sort_configurable_sort_issue_1732), because the example profile and sorting plugins are not installed there.One thing I left alone: with
lines_before_imports=1the kept import now comes after two blank lines, because the indented section keeps its trailing blank line and the next section also gets the configured one. The output is stable across runs, so this is cosmetic, but I can trim it in this PR if you would rather have exactlylines_before_importsblank lines there.