Skip to content

Group non-aliased from imports - #2614

Merged
DanielNoord merged 4 commits into
mainfrom
group-plain-from-imports
Oct 18, 2026
Merged

DanielNoord merged 4 commits into
mainfrom
group-plain-from-imports

Conversation

@DanielNoord

@DanielNoord DanielNoord commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Closes #2455

Code is still to be improved. I'm also unsure about some of the changes to expected test output...

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2614   +/-   ##
=======================================
  Coverage   99.37%   99.38%           
=======================================
  Files          40       40           
  Lines        3224     3253   +29     
  Branches      689      700   +11     
=======================================
+ Hits         3204     3233   +29     
  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 force-pushed the split-out-as-imports branch from 873f015 to 300a1dc Compare August 5, 2026 21:40
@DanielNoord
DanielNoord force-pushed the group-plain-from-imports branch from cefc0d0 to 52f4789 Compare August 5, 2026 23:21
@DanielNoord
DanielNoord force-pushed the group-plain-from-imports branch 2 times, most recently from cc796a7 to 109c47b Compare August 7, 2026 14:45
Base automatically changed from split-out-as-imports to main August 7, 2026 20:28
@DanielNoord
DanielNoord force-pushed the group-plain-from-imports branch from f693a88 to a4f5d68 Compare August 7, 2026 21:25
@DanielNoord
DanielNoord force-pushed the group-plain-from-imports branch 4 times, most recently from 3a6f53f to a114126 Compare August 8, 2026 21:13
@DanielNoord DanielNoord added the build-wheels Label to apply to pull requests to trigger a build of the wheels label Aug 18, 2026
@staticdev

Copy link
Copy Markdown
Collaborator

@DanielNoord on a first look I see:

If you want I can try to do commits to help this PR as well.

@DanielNoord

Copy link
Copy Markdown
Member Author

@staticdev

Doesn't this address it? I thought it did but might need to double check. I think the original error is just attributing an underlying problem (not merging/grouping plain from imports) to unrelated configuration options.
By grouping plain imports we necessarily need to handle comments on the import statements we are grouping.
The tests fail because this is a breaking change and both django and habitat don't conform to this style. Note that this is the style that ruff enforces by default.

@staticdev staticdev left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@DanielNoord sorry, I double-checked and the core behavior looks correct: the original multiline reproducer groups all plain imports into one statement, keeps the aliased import separate, and is idempotent with and without --fss.

I would propose to close this:

  • Add the exact long multiline #2455 reproducer as a regression test.
  • Add the reverse alias-before-plain case and assert idempotency.
  • Keep the Django and Habitat skips, but clarify that they reflect incompatibility with the intentional breaking style.

@DanielNoord
DanielNoord force-pushed the group-plain-from-imports branch from a114126 to f5f25ca Compare October 18, 2026 15:52
@DanielNoord DanielNoord changed the title Group plain imports Group non-aliased from imports Oct 18, 2026
@DanielNoord
DanielNoord requested a balanced review from Copilot October 18, 2026 16:06
@DanielNoord
DanielNoord marked this pull request as ready for review October 18, 2026 16:07

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.

🟡 Changes recommended

Formatting stability, alias ordering, and lint-suppression preservation have unresolved regressions.

3 open issues
What changed in this PR

Groups non-aliased from imports from the same module to address #2455, including when sorting within sections.

Changes:

  • Reworks grouping, alias emission, and comment wrapping.
  • Updates formatting expectations and adds regression coverage.
  • Temporarily skips three integration checks affected by the style change.
File Description
tests/​unit/​test_ticketed_features.py Updates commented-import expectations.
tests/​unit/​test_regressions.py Adds grouping and comment regressions.
tests/​unit/​test_isort.py Updates formatting expectations and stability checks.
tests/​integration/​test_projects_using_isort.py Skips three affected project checks.
isort/​output.py Implements grouping and adjusts comment handling.
isort/​__init__.py Applies the new import grouping style.

Review effort: Balanced

Comment thread isort/output.py Resolved
Comment thread isort/output.py Resolved
Comment thread isort/output.py Resolved

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.

🟡 Changes recommended

Unresolved regressions affect alias ordering across repeated runs and preservation of type-ignore directives.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve alias order when combining wildcard imports

isort/​output.py:713

With combine_star=True, from foo import *\nfrom foo import b\nfrom foo import b as c\nfrom foo import e as f\n emits e as f before b as c; a second run reverses them. The plain-and-aliased name b is deferred, while alias-only e is emitted immediately. Include only_show_as_imports in this branch so both aliases are emitted in order when the wildcard replaces the plain imports. Add a two-pass regression for this case.

Medium severity Preserve type-ignore directives when grouping imports

isort/​output.py:746

The compatibility check treats type: ignore as an ordinary explanatory comment. For example, from x import a # explanation\nfrom x import b # type: ignore[attr-defined]\n becomes from x import a, b # explanation; type: ignore[attr-defined]. Python no longer recognizes the type-ignore directive, so the suppression for b is lost. Preserve directive ownership and keep these imports separate when comments are incompatible. Add a regression covering this directive and repeated formatting.

3 resolved since last review

Review effort: Balanced

Comment thread isort/output.py Resolved

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.

🔵 Needs a closer look

Comment handling can broaden diagnostic suppression and produce output that changes on subsequent formatting passes.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Preserve pragma suppression scope when grouping imports

isort/​output.py:760

Pragma checks only run when the incoming import has a comment, so grouping can extend a suppression to previously unannotated imports. For example, from x import a # type: ignore[attr-defined] followed by from x import b becomes from x import a, b # type: ignore[attr-defined]. An unannotated a followed by b # noqa: F401 also merges, suppressing diagnostics for both names. Preserve the original suppression scope when grouping, with regression tests for uncommented imports on either side of a pragma-bearing import.

Medium severity Ignore comment boundaries when removing comments

isort/​output.py:760

These comment boundaries are enforced even with ignore_comments=True, although formatting then removes the comments. For from x import a # explanation followed by from x import b # noqa: F401, the first pass produces two uncommented statements and the second merges them into from x import a, b. Skip comment-based boundaries when removing comments so the first pass produces stable output.

Medium severity Clear opening comments after formatting import groups

isort/​output.py:810

opening_line_comments stays nonempty after the group owning those comments has been formatted. With profile="black", line_length=40, from x import a, b # explanation followed by from x import c # type: ignore[attr-defined] therefore moves the second comment onto an opening-parenthesis line. On the next pass, that comment is parsed as a module-level comment and merged with the explanation, changing its scope and breaking idempotence. Clear the opening-comment state after formatting the group that owns it.

1 resolved since last review

Review effort: Balanced

@DanielNoord
DanielNoord added this pull request to the merge queue Oct 18, 2026
Merged via the queue into main with commit 6b84f1f Oct 18, 2026
37 of 38 checks passed
@DanielNoord
DanielNoord deleted the group-plain-from-imports branch October 18, 2026 18:47
DanielNoord pushed a commit to hansu650/isort that referenced this pull request Oct 31, 2026
…orts

With combine_as_imports, the comments of an import that has an alias
are kept under module.__combined_as__ and added to the comments of the
grouped statement in _with_from_imports_for_module. Since the grouping
change of PyCQA#2614, preserve_comments_on_statement_line only looks at the
module's own opening-line comments, so a wrapped import of a single
aliased name went through wrap.line() and its comment, often a
type: ignore, moved to the alias line, where mypy no longer applies it.
That is the case PyCQA#1839 reported, now only with combine_as_imports.

Count the combined comments as statement comments too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-wheels Label to apply to pull requests to trigger a build of the wheels

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

3 participants