Skip to content

Keep aliased import when the plain name carries a comment#2567

Open
sarathfrancis90 wants to merge 1 commit into
PyCQA:mainfrom
sarathfrancis90:fix/aliased-import-dropped-when-name-has-comment
Open

Keep aliased import when the plain name carries a comment#2567
sarathfrancis90 wants to merge 1 commit into
PyCQA:mainfrom
sarathfrancis90:fix/aliased-import-dropped-when-name-has-comment

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

I hit a case where isort silently drops an import. With default settings:

from x import aaa
from x import m  # c
from x import m as z

becomes

from x import m  # c
from x import aaa

m as z is gone, and the output no longer re-sorts to itself.

The trigger is a name imported both plainly and with an alias, where the plain import has a trailing comment and another member of the module sorts ahead of it. The loop that emits aliased names only does so while the name is at the front of the group, so it never reaches one a sibling sorted before. The later "this name has its own comment" pass then prints just the plain name and removes it from the group, dropping the alias with it.

Fix is to leave a commented name that also has an alias for the alias loop rather than consuming it there. Added a regression test; full test suite, mypy, ruff/flake8 and the isort self-check are green.

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

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (0740d0c) to head (34c6e31).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2567   +/-   ##
=======================================
  Coverage   99.16%   99.16%           
=======================================
  Files          41       41           
  Lines        3101     3104    +3     
  Branches      671      672    +1     
=======================================
+ Hits         3075     3078    +3     
  Misses         14       14           
  Partials       12       12           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


first_pass = isort.code(to_sort)
assert first_pass == expected
assert "m as z" in first_pass

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.

Suggested change
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

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.

We can just use this and remove expected

Comment thread isort/output.py
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

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 is a very verbose, AI-ey, comment which also refers to stuff that isn't happening (printing). Can you make it more to the point please?

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.

2 participants