Repository navigation
Keep a plain import that shares its line with an alias - #2795
Merged
DanielNoord merged 1 commit intoDec 3, 2026
Merged
DanielNoord merged 1 commit into
DanielNoord merged 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approved
No active review issues are recorded for the change to preserve plain imports alongside aliases.
No open issues 🎉
What changed in this PR
Fixes isort dropping plain imports when they share a line with an alias and the same module is also aliased.
Changes:
- Tracks alias-only status per module rather than per line.
- Adds regression coverage for mixed imports, lazy imports, and redundant alias removal.
| File | Description |
|---|---|
| tests/unit/test_regressions.py | Tests that plain imports survive alongside aliases. |
| isort/parse.py | Distinguishes plain and aliased modules within each import line. |
Review effort: Balanced (adaptive)
DanielNoord
approved these changes
Dec 2, 2026
DanielNoord
reviewed
Dec 2, 2026
DanielNoord
enabled auto-merge
December 2, 2026 20:32
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2795 +/- ##
=======================================
Coverage 99.40% 99.40%
=======================================
Files 40 40
Lines 3376 3381 +5
Branches 728 729 +1
=======================================
+ Hits 3356 3361 +5
Misses 12 12
Partials 8 8 🚀 New features to boost your workflow:
|
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Dec 3, 2026
`import x as y, a` recorded `a` as alias-only because the whole line was marked as an as-import. When `a` was also aliased elsewhere (`import a as b`), the output then dropped the plain `import a`. Track which modules on the line were actually aliased instead.
DanielNoord
force-pushed
the
fix-straight-import-dropped-next-to-alias
branch
from
December 3, 2026 07:31
70c9f7d to
ffb3b36
Compare
DanielNoord
enabled auto-merge
December 3, 2026 07:32
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.
import x as y, amakes isort treat every module on that line as alias-only. That is harmless until the same module is also aliased somewhere else, and then the plain import disappears:becomes
so
ais no longer bound and the file fails with a NameError. Smaller forms of the same thing:import a as b, aandimport a, a as bboth become justimport a as b, and withremove_redundant_aliases=True,import a as anext toimport a as bloses the plainimport atoo. Lazy imports behave the same way.Cause: in
parse.file_contents,straight_importis a single flag for the whole line. It is set toFalseas soon as the line contains anas, and then every module left on the line is stored with that flag, including the ones that were not aliased. The output only writes a plainimport afor a module that also has aliases when that flag isTrue, so the plain import is lost.Fix: record which modules on the line were actually given an alias, and mark only those as alias-only. A module that is also imported bare on the same line (or whose redundant alias is removed) stays a plain import. From-imports are not affected; they already keep names and aliases separately.
Tests: added
test_plain_import_next_to_an_alias_is_kept_when_the_module_is_also_aliasedintests/unit/test_regressions.py(fails on main, passes with the fix).pytest tests/unit: 776 passed, 2 skipped; the only failures (test_settings_path_skip_issue_909,test_skip_paths_issue_938) fail the same way on main in my environment (FileNotFoundError from the sandbox paths).ruff check,ruff format --checkandisort --profile hug --checkpass.