From a3d47907c47771accbe2cea2e28e12678b903756 Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Fri, 26 Jun 2026 02:58:39 -0400 Subject: [PATCH 1/5] Keep aliased import when the plain name carries a comment When a name is imported both plainly and with an alias, and the plain import has a trailing comment, the aliased form was silently dropped if another member of the same module sorted ahead of it. The leading-alias loop only emits an aliased name while it sits at the front of the module group, so it never reached a name that a sibling sorted before. The later "this name has its own comment" pass then printed only the plain name and removed it from the group, taking the alias with it -- and the result no longer re-sorted cleanly. Leave a commented name that also has an alias for the alias loop instead of consuming it here. --- isort/output.py | 15 +++++++++++++++ tests/unit/test_regressions.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/isort/output.py b/isort/output.py index 7fa6e3e6f..8c4ba3a7e 100644 --- a/isort/output.py +++ b/isort/output.py @@ -556,6 +556,21 @@ def _with_from_imports( comment = ( parsed.categorized_comments["nested"].get(module, {}).pop(from_import, None) ) + if ( + comment is not None + and from_import in as_imports + and config.multi_line_output != wrap.Modes.NOQA # type: ignore[attr-defined] # noqa: E501 + ): + # This name also carries an alias. The leading-alias loop above emits + # the plain name (with this comment) together with its alias on a later + # pass of the outer ``while``. Consuming it here would print only the + # plain name and silently drop the alias, so put the comment back and + # leave the name for that loop. (The NOQA wrap mode appends its own + # per-line suppression and keeps its existing handling.) + parsed.categorized_comments["nested"].setdefault(module, {})[ + from_import + ] = comment + continue if comment is not None: # If the comment is a noqa and hanging indent wrapping is used, # keep the name in the main list and hoist the comment to the statement. diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index da00a5dd3..5a0732d0d 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2386,3 +2386,34 @@ def test_isort_skip_is_honored_with_future_import_issue_2092(): skip_index = next(i for i, line in enumerate(lines) if "# isort: skip" in line) assert lines.index("import ccc") > skip_index # skip not relocated below the block assert isort.code(sorted_interleaved) == sorted_interleaved + + +def test_isort_does_not_drop_aliased_import_when_plain_name_has_a_comment(): + """A name imported both plainly and with an alias must keep its alias when the plain + import carries a trailing comment and another member sorts ahead of it. + + With default settings ``from x import m`` (commented) and ``from x import m as z`` are + combined into one module group. The aliased ``m as z`` is only emitted by the loop that + walks the *leading* aliased names, so it is reached only while ``m`` sits at the front of + the group. When a sibling such as ``aaa`` sorts before ``m`` that loop never sees ``m``; + the later "name has its own comment" pass then printed ``from x import m # c`` and + removed ``m`` from the group, so ``m as z`` was silently dropped (and the output no longer + re-sorted cleanly). The comment-bearing names that also have an alias must be left for the + alias loop instead of being consumed here. + """ + to_sort = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" + expected = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" + + first_pass = isort.code(to_sort) + assert first_pass == expected + assert "m as z" in first_pass + + # The result must already be a fixpoint - the dropped alias previously only surfaced on + # the second run. + assert isort.code(first_pass) == first_pass + + # The same holds for a relative (local-folder) import. + relative = "from . import bar, one\nfrom . import one as zzz # NOQA\n" + relative_sorted = isort.code(relative) + assert "one as zzz" in relative_sorted + assert isort.code(relative_sorted) == relative_sorted From 30cc618f60f74a32688525935980ea26424fe43e Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Tue, 21 Jul 2026 18:26:46 -0400 Subject: [PATCH 2/5] Simplify aliased-import regression test and tighten guard comment --- isort/output.py | 9 +++------ tests/unit/test_regressions.py | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/isort/output.py b/isort/output.py index 8c4ba3a7e..9c2949189 100644 --- a/isort/output.py +++ b/isort/output.py @@ -561,12 +561,9 @@ def _with_from_imports( and from_import in as_imports and config.multi_line_output != wrap.Modes.NOQA # type: ignore[attr-defined] # noqa: E501 ): - # This name also carries an alias. The leading-alias loop above emits - # the plain name (with this comment) together with its alias on a later - # pass of the outer ``while``. Consuming it here would print only the - # plain name and silently drop the alias, so put the comment back and - # leave the name for that loop. (The NOQA wrap mode appends its own - # per-line suppression and keeps its existing handling.) + # This name also has an alias; the leading-alias loop keeps the two + # together on a later pass. Popping the comment here would drop the + # alias, so put it back and leave the name for that loop. parsed.categorized_comments["nested"].setdefault(module, {})[ from_import ] = comment diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 5a0732d0d..f81469bf1 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2402,10 +2402,9 @@ def test_isort_does_not_drop_aliased_import_when_plain_name_has_a_comment(): alias loop instead of being consumed here. """ to_sort = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" - expected = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" first_pass = isort.code(to_sort) - assert first_pass == expected + assert first_pass == to_sort assert "m as z" in first_pass # The result must already be a fixpoint - the dropped alias previously only surfaced on From 9175ce6eb187147d1bc1e55aa7b1ab8784b2b16b Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Wed, 22 Jul 2026 18:30:33 -0400 Subject: [PATCH 3/5] Address review: drop type-ignore, tighten aliased-import test Use wrap_modes.WrapModes.NOQA to match the rest of the file so the attr-defined type-ignore and E501 noqa are no longer needed. Trim the regression test's docstring, drop the redundant substring assert, and assert the relative-import case against its explicit expected output. --- isort/output.py | 2 +- tests/unit/test_regressions.py | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/isort/output.py b/isort/output.py index 9c2949189..7ca69ed2f 100644 --- a/isort/output.py +++ b/isort/output.py @@ -559,7 +559,7 @@ def _with_from_imports( if ( comment is not None and from_import in as_imports - and config.multi_line_output != wrap.Modes.NOQA # type: ignore[attr-defined] # noqa: E501 + and config.multi_line_output != wrap_modes.WrapModes.NOQA ): # This name also has an alias; the leading-alias loop keeps the two # together on a later pass. Popping the comment here would drop the diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index f81469bf1..70e80ea8d 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2389,30 +2389,26 @@ def test_isort_skip_is_honored_with_future_import_issue_2092(): def test_isort_does_not_drop_aliased_import_when_plain_name_has_a_comment(): - """A name imported both plainly and with an alias must keep its alias when the plain - import carries a trailing comment and another member sorts ahead of it. - - With default settings ``from x import m`` (commented) and ``from x import m as z`` are - combined into one module group. The aliased ``m as z`` is only emitted by the loop that - walks the *leading* aliased names, so it is reached only while ``m`` sits at the front of - the group. When a sibling such as ``aaa`` sorts before ``m`` that loop never sees ``m``; - the later "name has its own comment" pass then printed ``from x import m # c`` and - removed ``m`` from the group, so ``m as z`` was silently dropped (and the output no longer - re-sorted cleanly). The comment-bearing names that also have an alias must be left for the - alias loop instead of being consumed here. + """A name imported both plainly (with a trailing comment) and aliased must keep its + alias when a sibling sorts ahead of it. + + ``from x import m # c`` and ``from x import m as z`` combine into one group, and the + alias is only emitted while ``m`` leads that group. When ``aaa`` sorts before ``m`` the + comment pass used to consume ``m`` and drop ``m as z``, which only surfaced on a second + run once the group was re-sorted. """ to_sort = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" first_pass = isort.code(to_sort) assert first_pass == to_sort - assert "m as z" in first_pass - # The result must already be a fixpoint - the dropped alias previously only surfaced on - # the second run. + # The dropped alias previously only surfaced on the second run, so the result must + # already be a fixpoint. assert isort.code(first_pass) == first_pass # The same holds for a relative (local-folder) import. relative = "from . import bar, one\nfrom . import one as zzz # NOQA\n" relative_sorted = isort.code(relative) - assert "one as zzz" in relative_sorted + expected = "from . import bar\nfrom . import one # NOQA\nfrom . import one as zzz\n" + assert relative_sorted == expected assert isort.code(relative_sorted) == relative_sorted From 764226ea8f9fdce5cd48176337dd810376ec9289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 24 Jul 2026 22:09:13 +0200 Subject: [PATCH 4/5] Small fix --- tests/unit/test_regressions.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 70e80ea8d..f6cc2d8fc 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2398,13 +2398,7 @@ def test_isort_does_not_drop_aliased_import_when_plain_name_has_a_comment(): run once the group was re-sorted. """ to_sort = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" - - first_pass = isort.code(to_sort) - assert first_pass == to_sort - - # The dropped alias previously only surfaced on the second run, so the result must - # already be a fixpoint. - assert isort.code(first_pass) == first_pass + assert isort.code(first_pass) == to_sort # The same holds for a relative (local-folder) import. relative = "from . import bar, one\nfrom . import one as zzz # NOQA\n" From 8782d276d0a160c63f3ecb812d4028f7fc47841d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Fri, 24 Jul 2026 22:12:18 +0200 Subject: [PATCH 5/5] Fix the fix :) --- tests/unit/test_regressions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index f6cc2d8fc..090433525 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2398,7 +2398,7 @@ def test_isort_does_not_drop_aliased_import_when_plain_name_has_a_comment(): run once the group was re-sorted. """ to_sort = "from x import aaa\nfrom x import m # c\nfrom x import m as z\n" - assert isort.code(first_pass) == to_sort + assert isort.code(to_sort) == to_sort # The same holds for a relative (local-folder) import. relative = "from . import bar, one\nfrom . import one as zzz # NOQA\n"