Keep the multiline layout of a sorted literal that has a trailing comment - #2686
Merged
DanielNoord merged 1 commit intoOct 1, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2686 +/- ##
=======================================
Coverage 99.41% 99.41%
=======================================
Files 41 41
Lines 3231 3231
Branches 690 690
=======================================
Hits 3212 3212
Misses 12 12
Partials 7 7 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
AST byte offsets are incorrectly used as character indices, potentially corrupting comments after non-ASCII literals.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Fixes multiline literal layout preservation when a trailing comment follows the closing bracket.
Changes:
- Detects trailing commas only within the parsed literal.
- Adds regression coverage for commented multiline reexports.
| File | Description |
|---|---|
isort/literal.py |
Uses the literal boundary for trailing-comma detection. |
tests/unit/test_regressions.py |
Tests multiline layout with a trailing comment. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
DanielNoord
approved these changes
Oct 1, 2026
…ment The trailing-comma check from PyCQA#2605 looked at the whole literal text, and since the bracket hid the trailing comma, so __all__ = [ "b", "a", ] # noqa: F405 was collapsed to `__all__ = ["a", "b"] # noqa: F405` under the black profile with --sort-reexports. Check the trailing comma on the literal itself, using the end position PyCQA#2647 already computes.
DanielNoord
force-pushed
the
reexports-trailing-comma-comment
branch
from
October 1, 2026 19:56
8cb2fc1 to
f467420
Compare
DanielNoord
enabled auto-merge
October 1, 2026 19:56
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.

#2605 and #2647 (both merged 2026-09-21) interact. #2605 keeps a multiline, trailing-comma literal in its layout when
--sort-reexportsruns under a trailing-comma profile, deciding with_has_trailing_comma(literal). Since #2647,literalalso holds whatever follows the closing bracket, so a comment there hides the comma:The trailing comma goes too, so Black does not expand it back. This moves #2647's end-position computation above the check and passes
literal[:value_end]; the block and its comment are moved, not rewritten.Verification
Base
13758cf5. New test next to #2605's intests/unit/test_regressions.py.isort/literal.pymd5cecdbba24c4f147208bf4bf5literal[:value_end - 1]1087b8373008_issue_2578tests and four of #2669's testsFollowing
continuous-integration.yml:uv sync --all-extras --frozen, thenuv run --with tox-uv tox -e py,coverage_report-ci: 660 passed, 1 skipped,isort/literal.pyat 100%.tox -e lintpasses mypy, isort, flake8 and ruff; it reports one bandit B101 atisort/parse.py:411, identical onmain, in a file this does not touch. Not run: the integration job, other Python versions, Windows and macOS.Written with AI assistance (Claude); the measurements above were run locally and I have reviewed the change.