rope icon indicating copy to clipboard operation
rope copied to clipboard

unsafe refactoring: when extracting method, should detect and raise an error if variable scope cannot be determined

Open lieryan opened this issue 4 years ago • 0 comments

Comment copied from https://github.com/python-rope/rope/pull/380#issuecomment-917687734

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.

lieryan avatar Sep 17 '21 19:09 lieryan