Skip to content

Fix: preserve bare # inline comments on imports - #2488

Merged
DanielNoord merged 2 commits into
mainfrom
copilot/fix-isort-empty-comments
Mar 12, 2026
Merged

Fix: preserve bare # inline comments on imports#2488
DanielNoord merged 2 commits into
mainfrom
copilot/fix-isort-empty-comments

Conversation

Copilot AI commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

import a # was being silently rewritten to import 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.pyparse() now returns None (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 — All if comment / if associated_comment / if extra_line.comment checks updated to is not None.
  • isort/output.py — Nested comment rendering and combine_straight_imports path fixed to correctly emit bare #.
  • tests/unit/test_ticketed_features.py — Regression tests for straight imports, from-imports, and idempotency.
# Before
isort.code("import a  #\n")          # → "import a\n"   ❌
isort.code("from foo import bar  #\n") # → "from foo import bar\n"  ❌

# After
isort.code("import a  #\n")          # → "import a  #\n"   ✅
isort.code("from foo import bar  #\n") # → "from foo import bar  #\n"  ✅
Original prompt

This section details on the original issue you should resolve

<issue_title>isort strips empty comments, e.g. import a # -> import a</issue_title>
<issue_description>This is merely weird in isolation, but more practically makes it difficult for downstream tools like shed to performantly compose isort with other refactoring passes.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

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 # inline comments on imports Mar 12, 2026
Copilot AI requested a review from DanielNoord March 12, 2026 20:58
@codecov

codecov Bot commented Mar 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.18%. Comparing base (aade4b3) to head (2646b88).
⚠️ Report is 7 commits behind head on main.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord
DanielNoord marked this pull request as ready for review March 12, 2026 21:14
@DanielNoord
DanielNoord added this pull request to the merge queue Mar 12, 2026
Merged via the queue into main with commit 59472c9 Mar 12, 2026
21 of 23 checks passed
@DanielNoord
DanielNoord deleted the copilot/fix-isort-empty-comments branch March 12, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isort strips empty comments, e.g. import a # -> import a

2 participants