Skip to content

Preserve trailing-comma layout when sorting reexports - #2605

Open
PSR94 wants to merge 1 commit into
PyCQA:mainfrom
PSR94:issue/2578-preserve-reexport-trailing-comma
Open

Preserve trailing-comma layout when sorting reexports#2605
PSR94 wants to merge 1 commit into
PyCQA:mainfrom
PSR94:issue/2578-preserve-reexport-trailing-comma

Conversation

@PSR94

@PSR94 PSR94 commented Aug 2, 2026

Copy link
Copy Markdown

Summary

Preserve explicitly multiline __all__ collections with trailing commas when --sort-reexports runs under trailing-comma-aware formatting such as the Black profile.

Why this helps

This keeps sorted reexports stable with Black's magic trailing comma behavior, so short __all__ declarations that are intentionally one item per line are not collapsed after sorting.

Changes made

  • Detect when a source literal is multiline and ends with a trailing comma.
  • Preserve that multiline layout when the active config includes trailing commas.
  • Add regression coverage for short tuple-style and list-style __all__ exports.

Testing

  • uv run pytest tests/unit/test_regressions.py -k '2578 or 2280'
  • uv run pytest tests/unit/test_isort.py -k 'reexport_multiline'
  • uv run ruff format --check isort/literal.py tests/unit/test_regressions.py
  • uv run ruff check isort/literal.py tests/unit/test_regressions.py
  • uv run mypy isort tests
  • ./scripts/done.sh

Closes #2578

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.49%. Comparing base (0145f77) to head (04780d4).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2605      +/-   ##
==========================================
+ Coverage   99.43%   99.49%   +0.06%     
==========================================
  Files          41       41              
  Lines        3159     3177      +18     
  Branches      685      688       +3     
==========================================
+ Hits         3141     3161      +20     
+ Misses         11       10       -1     
+ Partials        7        6       -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think you can get full coverage on this? I have not looked at the code yet but we prefer all new code to be covered by tests :)

@PSR94
PSR94 force-pushed the issue/2578-preserve-reexport-trailing-comma branch from 4fda57b to 04780d4 Compare August 4, 2026 02:32
@PSR94

PSR94 commented Aug 4, 2026

Copy link
Copy Markdown
Author

Thanks for the review. I updated the PR to add coverage for the new literal formatting paths, including the formatting-function branch and the trailing-comma detection guard. I also reran uv run ruff format --check isort/literal.py tests/unit/test_literal.py tests/unit/test_regressions.py, uv run ruff check isort/literal.py tests/unit/test_literal.py tests/unit/test_regressions.py, uv run mypy isort tests, and ./scripts/done.sh; they are passing, and isort/literal.py reports 100% coverage locally.

@DanielNoord

DanielNoord commented Aug 11, 2026

Copy link
Copy Markdown
Member

I'm a bit pressed for time currently. To keep moving this forward I'll ask Copilot for an initial review. Please disregard if it is bogus, it tends to help me by doing an initial pass but of course it isn't always the quality we hope for.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Preserves multiline trailing-comma layouts when sorting reexports under compatible profiles such as Black.

Changes:

  • Detects multiline literals with trailing commas.
  • Prevents eligible collections from collapsing to one line.
  • Adds tuple/list regression and helper coverage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
isort/literal.py Detects and preserves trailing-comma layouts.
tests/unit/test_literal.py Tests formatting callbacks and bracket detection.
tests/unit/test_regressions.py Covers short tuple/list __all__ reexports.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

--sort-reexports does not retain trailing commas in short __all__ exports

3 participants