Skip to content

Keep a plain import that shares its line with an alias - #2795

Merged
DanielNoord merged 1 commit into
PyCQA:mainfrom
waw666waw666:fix-straight-import-dropped-next-to-alias
Dec 3, 2026
Merged

DanielNoord merged 1 commit into
PyCQA:mainfrom
waw666waw666:fix-straight-import-dropped-next-to-alias

Conversation

@waw666waw666

Copy link
Copy Markdown
Contributor

import x as y, a makes 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:

import x as y, a
import a as b

becomes

import a as b
import x as y

so a is no longer bound and the file fails with a NameError. Smaller forms of the same thing: import a as b, a and import a, a as b both become just import a as b, and with remove_redundant_aliases=True, import a as a next to import a as b loses the plain import a too. Lazy imports behave the same way.

Cause: in parse.file_contents, straight_import is a single flag for the whole line. It is set to False as soon as the line contains an as, 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 plain import a for a module that also has aliases when that flag is True, 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_aliased in tests/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 --check and isort --profile hug --check pass.

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.

🟢 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)

Comment thread tests/unit/test_regressions.py Resolved
@DanielNoord
DanielNoord enabled auto-merge December 2, 2026 20:32
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.40%. Comparing base (8f088ca) to head (ffb3b36).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord
DanielNoord added this pull request to the merge queue Dec 3, 2026
@github-merge-queue
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
DanielNoord force-pushed the fix-straight-import-dropped-next-to-alias branch from 70c9f7d to ffb3b36 Compare December 3, 2026 07:31
@DanielNoord
DanielNoord enabled auto-merge December 3, 2026 07:32
@DanielNoord
DanielNoord added this pull request to the merge queue Dec 3, 2026
Merged via the queue into PyCQA:main with commit e45081c Dec 3, 2026
27 checks passed
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.

3 participants