Keep aliased import when the plain name carries a comment#2567
Open
sarathfrancis90 wants to merge 1 commit into
Open
Keep aliased import when the plain name carries a comment#2567sarathfrancis90 wants to merge 1 commit into
sarathfrancis90 wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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:
|
DanielNoord
reviewed
Jun 28, 2026
|
|
||
| first_pass = isort.code(to_sort) | ||
| assert first_pass == expected | ||
| assert "m as z" in first_pass |
Member
There was a problem hiding this comment.
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 |
Member
There was a problem hiding this comment.
We can just use this and remove expected
| 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 |
Member
There was a problem hiding this comment.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I hit a case where isort silently drops an import. With default settings:
becomes
m as zis 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.