Skip to content

Preserve imports shadowed by inherited class attributes - #958

Open
ViktorBystrov72 wants to merge 1 commit into
python-rope:masterfrom
ViktorBystrov72:fix/imports-shadowed-by-inherited-attributes
Open

ViktorBystrov72 wants to merge 1 commit into
python-rope:masterfrom
ViktorBystrov72:fix/imports-shadowed-by-inherited-attributes

Conversation

@ViktorBystrov72

Copy link
Copy Markdown

Description

Import cleanup removes a required import when a base class defines an attribute with the same name. In the example from #244, from foo import bar is needed by baz = bar in a subclass body. Python resolves that bare name outside the class; it does not read the inherited attribute. The generated module currently raises NameError.

Use the names defined in the class when checking whether an import is shadowed there. Propagated-name lookup remains unchanged. Regression tests cover from-imports, dotted module imports, and an actual class-local binding that still makes an import unused.

Fixes #244

Validation

  • Before the fix: two new regression tests fail; the class-local shadowing control passes.
  • After the fix: all 155 import utility tests pass.
  • Full suite on macOS arm64, Homebrew Python 3.14.6: 2,183 passed, 7 skipped, 5 xfailed.
  • All five configured pre-commit hooks and git diff --check pass.
  • A separate probe using the public ImportOrganizer API executes the issue's example in temporary directories: the original program prints 10, the unmodified refactoring result raises NameError, and the corrected result prints 10.

The first full run used uv's Python 3.13.13, whose _sqlite3 module is built in and has no __file__. That run had 2,182 passed, 7 skipped, 5 xfailed and one error in the existing compiled-library fixture. The same fixture error reproduces on untouched master. The complete passing run above uses Homebrew's dynamic _sqlite3 extension; no tests were disabled or modified to bypass it.

Checklist

  • I have added tests that prove my fix is effective.
  • I have updated CHANGELOG.md using the existing issue number.

AI assistance

OpenAI Codex assisted with issue and source analysis, implementation, regression tests, local verification, and this description. The change was checked against the source and validated with the tests and public API reproducer listed above.

This branch has not been deployed

No deployments
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.

ImportOrganizer incorrectly determines unbound names in class contexts that inherit from a super class with attributes that have the same name

1 participant