Repository navigation
Keep a bare "#" when wrapping an import line - #2757
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approved
No active review issues are recorded for the change preserving bare # comments during import formatting.
No open issues 🎉
What changed in this PR
Fixes #2756 by preserving bare # comments during import formatting, distinguishing empty comments from absent comments.
Changes:
- Updates wrapping and comma handling to check for
Nonerather than comment truthiness. - Preserves empty comments on star imports.
- Adds regression coverage for wrapped imports, star imports, and wrapped-import idempotence.
| File | Description |
|---|---|
tests/unit/test_regressions.py |
Adds regression tests for preserving bare # comments. |
isort/wrap.py |
Treats empty comments as present when wrapping imports. |
isort/output.py |
Retains empty comments attached to star imports. |
Review effort: Balanced
DanielNoord
approved these changes
Oct 29, 2026
DanielNoord
enabled auto-merge
October 29, 2026 15:34
A bare "#" parses to an empty comment string, and the checks in wrap.line()
and _with_star_comments() used truthiness, so the empty string was treated as
"no comment". The comment was dropped and the whitespace split off by
content.split("#", 1) stayed on the line, leaving trailing spaces that the
next run then stripped.
Test for None instead, which is what an absent comment actually is.
DanielNoord
force-pushed
the
issue/2756
branch
from
October 29, 2026 16:05
a9341cc to
b451e8c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2757 +/- ##
=======================================
Coverage 99.38% 99.38%
=======================================
Files 40 40
Lines 3275 3275
Branches 707 707
=======================================
Hits 3255 3255
Misses 12 12
Partials 8 8 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2756.
A bare
#parses to an empty comment string, and the checks used truthiness, so""was treated as "no comment". The comment was dropped and the whitespace thatcontent.split("#", 1)left behind stayed on the line:The second line ends with two spaces, and the next run strips them, so the output is not a fixpoint. A non-empty comment on the same input was already handled correctly.
_with_star_comments()had the same check, which dropped the#from star imports even when nothing needed wrapping:Test for
Noneinstead, which is what an absent comment actually is, inwrap.line()and_with_star_comments().comment_suffixand_commahad the sameif comment/not commentshape and are fixed alongside so a bare#behaves like any other comment there.Two regression tests in
test_regressions.pycover the wrapped line and the star import, and both assert the output is a fixpoint. They fail without the change.tests/unitis 718 passed, 1 skipped, and flake8 is clean.