Skip to content

Keep star imports when inlining a function or method - #940

Merged
lieryan merged 3 commits into
python-rope:masterfrom
chinghar:fix-826-inline-star-import
Nov 13, 2026
Merged

lieryan merged 3 commits into
python-rope:masterfrom
chinghar:fix-826-inline-star-import

Conversation

@chinghar

@chinghar chinghar commented Nov 7, 2026

Copy link
Copy Markdown

Description

Inlining a function or method removed any star import (from x import *) in the
modules where the call sites were inlined, breaking other names those imports provided.

Root cause: after inlining, _remove_from() runs RemovePyNameVisitor over the
module's imports to drop the now-unused import of the inlined name.
visitFromImport rebuilds each from-import from the names it keeps, but for star
imports it collected none, so the import was replaced with an empty FromImport
and dropped. The visitor now leaves star imports unchanged.

Reproduction (before this change):

# mod.py
class A:
    def f(self):
        return 1

# mod2.py
from os import *
from mod import A

print(A().f(), getcwd())

Inlining A.f produced mod2.py without from os import *. It is now kept.

Test: added test_inlining_method_keeps_star_imports_in_the_dest_module to
ropetest/refactor/inlinetest.py; it fails before the change and passes after.
Full suite: 2167 passed, 7 skipped, 5 xfailed. pre-commit (black, isort) passes.

Fixes #826

Checklist (delete if not relevant):

  • I have added tests that prove my fix is effective or that my feature works
  • I have updated CHANGELOG.md

cgharpure627 and others added 3 commits November 7, 2026 14:21
RemovePyNameVisitor rebuilt every from-import from the names it kept.
For a star import no names were collected, so the import was replaced
by an empty one and dropped, breaking other names it provided.

Leave star imports unchanged.

Fixes python-rope#826
@lieryan
lieryan enabled auto-merge November 13, 2026 20:09
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.44%. Comparing base (7260b41) to head (106b518).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
rope/refactor/importutils/actions.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #940   +/-   ##
=======================================
  Coverage   95.44%   95.44%           
=======================================
  Files         134      134           
  Lines       26896    26902    +6     
=======================================
+ Hits        25671    25677    +6     
  Misses       1225     1225           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lieryan
lieryan merged commit 162d60b into python-rope:master Nov 13, 2026
23 of 24 checks passed
@lieryan

lieryan commented Nov 13, 2026

Copy link
Copy Markdown
Member

Thanks for looking into this issue.

@allcontributors please add @chinghar for code contribution

@allcontributors

Copy link
Copy Markdown
Contributor

@lieryan

I've put up a pull request to add @chinghar! 🎉

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.

The Inline Method refactoring removed an import

3 participants