Skip to content

Fix unindented comments being corrupted in indented blocks#2459

Merged
DanielNoord merged 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-unindented-comment-corruption
Feb 19, 2026
Merged

Fix unindented comments being corrupted in indented blocks#2459
DanielNoord merged 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-unindented-comment-corruption

Conversation

@worksbyfriday
Copy link
Contributor

Summary

When an unindented comment appears before an indented import block, isort corrupts the comment by stripping len(indent) characters from the start of every line—even lines that don't begin with that indent.

Before fix:

# Input
if True:
# this will get cut off
    import os

# Output (corrupted)
if True:
is will get cut off
    import os

After fix: The comment is preserved correctly.

Root cause

In isort/core.py line 422, the indent-stripping logic:

line[len(indent):] for line in import_section.splitlines(keepends=True)

blindly slices every line without checking if it actually starts with the indent prefix.

Fix

Added a line.startswith(indent) guard so only properly indented lines get stripped:

line[len(indent):] if line.startswith(indent) else line

Test plan

  • Added test_unindented_comment_in_indented_block_issue_1899 to tests/unit/test_ticketed_features.py
  • Verified existing unit tests still pass (283 passed, 1 pre-existing failure unrelated to this change)

Fixes #1899

🤖 Generated with Claude Code

When an unindented comment appears before an indented import block,
isort blindly strips `len(indent)` characters from the start of every
line. This corrupts comments that don't start with the expected indent.

For example:
```python
if True:
# this will get cut off
    import os
```
became:
```python
if True:
is will get cut off
    import os
```

The fix checks whether each line actually starts with the indent
before stripping it.

Fixes PyCQA#1899

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (b0f2dab) to head (fd2514b).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2459   +/-   ##
=======================================
  Coverage   99.24%   99.24%           
=======================================
  Files          39       39           
  Lines        3057     3057           
  Branches      675      675           
=======================================
  Hits         3034     3034           
  Misses         13       13           
  Partials       10       10           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Thanks!

@DanielNoord DanielNoord added this pull request to the merge queue Feb 19, 2026
Merged via the queue into PyCQA:main with commit b5f06a7 Feb 19, 2026
22 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.

Unindented comments get malformed

2 participants