Skip to content

Keep a bare "#" when wrapping an import line - #2757

Merged
DanielNoord merged 2 commits into
PyCQA:mainfrom
beduldul:issue/2756
Oct 29, 2026
Merged

DanielNoord merged 2 commits into
PyCQA:mainfrom
beduldul:issue/2756

Conversation

@beduldul

Copy link
Copy Markdown
Contributor

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 that content.split("#", 1) left behind stayed on the line:

>>> isort.code("from sys import super_long_name  #\n", force_grid_wrap=3, line_length=30)
'from sys import \\\n    super_long_name  \n'

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:

>>> isort.code("from sys import *  #\n")
'from sys import *\n'

Test for None instead, which is what an absent comment actually is, in wrap.line() and _with_star_comments(). comment_suffix and _comma had the same if comment / not comment shape and are fixed alongside so a bare # behaves like any other comment there.

>>> isort.code("from sys import super_long_name  #\n", force_grid_wrap=3, line_length=30)
'from sys import \\\n    super_long_name  #\n'
>>> isort.code("from sys import *  #\n")
'from sys import *  #\n'

Two regression tests in test_regressions.py cover the wrapped line and the star import, and both assert the output is a fixpoint. They fail without the change. tests/unit is 718 passed, 1 skipped, and flake8 is clean.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 None rather 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
DanielNoord enabled auto-merge October 29, 2026 15:34
beduldul and others added 2 commits October 29, 2026 17:05
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.
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (1d60803) to head (b451e8c).
⚠️ Report is 1 commits behind head on main.

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

@DanielNoord
DanielNoord added this pull request to the merge queue Oct 29, 2026
Merged via the queue into PyCQA:main with commit 2c45b2b Oct 29, 2026
45 of 49 checks passed
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.

A bare # is dropped and leaves trailing whitespace when the import line is wrapped

3 participants