Define functions extracted from a class body before the class - #878
Open
ethanstoner wants to merge 2 commits into
Open
ethanstoner wants to merge 2 commits into
ethanstoner wants to merge 2 commits into
Conversation
Extract Method on code in a class body inserted the new function after the class, but class bodies run at definition time, so the class raised NameError. Insert the function before the (outermost) class instead, and collect class-body variables so they are passed as arguments. Fixes python-rope#825
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.
Description
Extract Method on an expression or statements in a class body put the new function after the class. Class bodies run when the class is defined, so the class raised
NameError.Root cause: in
_DefinitionLocationFinder.find_lineno(), a class scope at module level is neitherglobal_normethod, so it fell through to_get_after_scope(), which inserts after the class. Withglobal_=Trueit went after the top-level class for the same reason.Changes in
rope/refactor/extract.py:_ExtractInfo.class_to_define_before: when the scope is a class, the function is defined before the outermost class in the chain of enclosing classes (before any decorators), at that class's indentation. Nested classes can't see names in an enclosing class body, so the outermost class is used. Withglobal_=True, this applies only when that class is at module level. If the class is inside a function, the existing "after the top-level function" placement is kept, since that code runs later._ExtractInfo.methodnow requires the scope to be a function. Before, extracting from a class nested in a class crashed withAttributeError: 'PyClass' object has no attribute 'get_kind'._FunctionInformationCollector._ClassDefnow visits the body when the class is the host scope. Class variables read by the extracted code (e.g.b = a + 2) are passed as arguments. Otherwise, moving the function before the class would still raiseNameError.Fixes #825
Checklist (delete if not relevant):
Tests: 7 new tests in
ropetest/refactor/extracttest.pycover the issue case, class variables, a decorated class, a class inside a function, a nested class, andglobal_=True. 6 fail without the fix. The seventh (global_=Trueon a class inside a function) already passed and checks that case isn't changed. Ranpytest ropetest(2155 passed, 7 skipped, 5 xfailed), plus black 26.5.1 and isort.Generated with Claude Code on behalf of @ethanstoner.