Fix: preserve bare # inline comments on imports - #2488
Merged
Conversation
isort was stripping empty inline comments from imports, e.g., `import a #` became `import a`. This was caused by the comment parser returning `""` for both "no `#`" and "empty comment after `#`", and callers dropping falsy values. - comments.py: parse() returns None (not "") when no # is present; add_to_line() handles empty comment strings by outputting bare # without trailing space - parse.py: use `is not None` checks to distinguish empty from absent comments - output.py: fix nested comment rendering and combine_straight_imports path Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix isort stripping empty comments issue
Fix: preserve bare Mar 12, 2026
# inline comments on imports
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2488 +/- ##
==========================================
- Coverage 99.25% 99.18% -0.07%
==========================================
Files 40 40
Lines 3079 3084 +5
Branches 671 672 +1
==========================================
+ Hits 3056 3059 +3
- Misses 13 14 +1
- Partials 10 11 +1 🚀 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.
import a #was being silently rewritten toimport a— bare#comments were dropped entirely during sort/reformat.Root cause:
comments.parse()returned""for both "no#present" and "empty comment after#". All callers used truthy checks (if comment:), so bare#was indistinguishable from no comment and got discarded.Changes:
isort/comments.py—parse()now returnsNone(no#) vs""(bare#) vs"text"(comment with content).add_to_line()handles the empty-string case by emitting#without a trailing space.isort/parse.py— Allif comment/if associated_comment/if extra_line.commentchecks updated tois not None.isort/output.py— Nested comment rendering andcombine_straight_importspath fixed to correctly emit bare#.tests/unit/test_ticketed_features.py— Regression tests for straight imports, from-imports, and idempotency.Original prompt
import a #->import a#1913✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.