Skip to content

fix: ignore wrapping paren in force_sort_within_sections keys#2592

Closed
l46983284-cpu wants to merge 7 commits into
PyCQA:mainfrom
l46983284-cpu:fix/fss-mixed-from-import-separation
Closed

fix: ignore wrapping paren in force_sort_within_sections keys#2592
l46983284-cpu wants to merge 7 commits into
PyCQA:mainfrom
l46983284-cpu:fix/fss-mixed-from-import-separation

Conversation

@l46983284-cpu

Copy link
Copy Markdown

Summary

Under --force-sort_within_sections, multi-line from imports were sorted as import ( / newline-indented names, which collates before import Alias and can pull a later plain group ahead of an alias from the same module (issue #2455).

This normalizes the wrapping parenthesis and following whitespace in section_key so those lines sort by the first imported name, matching single-line imports.

Non-goals

Does not merge separate plain from module import (...) statements across an intermediate alias. That larger contract change conflicts with #2352 trailing plain-after-alias stability. Issue #2455 listed this section_key normalization as a valid approach.

Test plan

Closes #2455

Under --fss, multi-line from-imports sort as ``import (Name`` which
collates before ``import Alias`` and can separate two plain groups
from the same module around an alias (issue PyCQA#2455). Strip the wrapping
``(`` in section_key so those lines sort by the first imported name.

Does not merge plain names across alias statements (that path conflicts
with PyCQA#2352 trailing plain-after-alias stability).

Closes PyCQA#2455

Signed-off-by: Alex Chen <l46983284@gmail.com>
Keep force_sort_within_sections PyCQA#2455 behavior, but move the paren
normalization into a small helper so section_key does not pick up an
extra cyclomatic-complexity branch for DeepSource PY-R1000.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu

Copy link
Copy Markdown
Author

DeepSource flagged PY-R1000 on section_key after the #2455 paren-normalization branch. I moved that strip into a tiny _strip_wrapping_import_paren helper so the behavior is unchanged and the complexity bump is gone.

Local: ruff clean on the touched files, tests/unit/test_regressions.py 92 passed.

@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.

This does not seem to implement the style as agreed upon in the final comments of that issue. Any reason why you deviated from that?

Signed-off-by: Alex Chen <l46983284@gmail.com>
force_sort_within_sections re-sorts emitted statements; rank aliases
after plains for the same module so PyCQA#2455 style survives --fss.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu

Copy link
Copy Markdown
Author

Thanks — I switched this to the agreed style from the final comments on #2455: plain names from the same module are merged into one import, with aliases after. Also fixed --fss so a lexically earlier alias no longer sorts above plains.

@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.36%. Comparing base (68356ea) to head (929d851).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2592      +/-   ##
==========================================
- Coverage   99.39%   99.36%   -0.03%     
==========================================
  Files          41       41              
  Lines        3147     3169      +22     
  Branches      680      685       +5     
==========================================
+ Hits         3128     3149      +21     
  Misses         11       11              
- Partials        8        9       +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.

Instead of keeping track of these cases (in this quite complex code), wouldn't it be easier to merge import statements at the end? That seems like it would be simpler to understand and maintain

Reformat isort's own sources for the plain-before-alias grouping so the
hug-profile lint check passes. Skip django/habitat-lab/pylint integration
baselines that intentionally change under the PyCQA#2455 preferred style until
those projects re-sort. Keep alias_rank only on non-lexicographical keys
and harden multi-line paren stripping for section_key.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Drop mid-path defer_as bookkeeping. After statements are emitted, merge
comment-free plain from-import statements for the same module and put
that plain group before free aliases when needed.

Fixes review feedback on PyCQA#2592 to keep mid-path emit simple.

Signed-off-by: Alex Chen <l46983284@gmail.com>
After dropping defer_as bookkeeping, the combine_as condition still
referenced a stale from_import name from an earlier loop. Use the
current head of from_imports like the previous preferred tip.

Signed-off-by: Alex Chen <l46983284@gmail.com>

@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.

This seems like it changes our style signficantly. I think we should first discuss other alternatives in the issue. Unfortunately the style as discussed there woulkd be a breaking change for the project.

@l46983284-cpu

Copy link
Copy Markdown
Author

No pressure at all — feel free to close this PR or reuse any part of the code if it's useful. It was a fun exercise to work through. If you have thoughts on what direction would make sense for the issue, I'd be happy to collaborate on a follow-up.

@DanielNoord

Copy link
Copy Markdown
Member

I'll close for now, thanks for having a stab at this!

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.

--fss may lead to incorrect import separation for mixed multi-line and alias imports

2 participants