Skip to content

Fix variables that are returned and/or turned into argument when extracting method in a loop#380

Merged
lieryan merged 6 commits into
masterfrom
fix-112-extract-in-a-loop
Sep 17, 2021
Merged

Fix variables that are returned and/or turned into argument when extracting method in a loop#380
lieryan merged 6 commits into
masterfrom
fix-112-extract-in-a-loop

Conversation

@lieryan

@lieryan lieryan commented Sep 12, 2021

Copy link
Copy Markdown
Member

@lieryan lieryan self-assigned this Sep 12, 2021
@lieryan

lieryan commented Sep 12, 2021

Copy link
Copy Markdown
Member Author

Created #393 for this comment.


One maybe-bug with this refactoring:

def my_func():
    for dummy in range(10):
        i += 1

Current extract refactoring implementation produces:

def my_func():
    for dummy in range(10):
        i = aaa()

def aaa():
    i += 1
    return i

Arguably, the original code was non-sense to begin with, as i was undefined. When run, that code would've produced a NameError, so strictly speaking, this refactoring is garbage-in, garbage-out.

If i was assigned before, such as the below code, the current implementation would have produced correct refactoring:

def my_func():
    i = 0
    for dummy in range(10):
        i += 1

Maybe rope could catch this and refuse to refactor such code.

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.

Unexpected behavior when extracting loop body

1 participant