Skip to content

Reuse of variable in comprehensions confuses method extraction #315

Description

@chrisbarber

Extract method for the a = and b = lines individually.

Actual

def f():
    y = [1,2,3,4]

    a = sum((x for x in y))

    b = sum([x for x in y])

    print(a, b)

f() 
def f():
    y = [1,2,3,4]

    x, a = _a(y)

    b = _b(x, y)

    print(a, b)

def _b(x, y):
    b = sum([x for x in y])
    return b

def _a(y):
    a = sum((x for x in y))
    return x, a

f()

Expected

def f():
    y = [1,2,3,4]

    a = sum((x1 for x1 in y))

    b = sum([x2 for x2 in y])

    print(a, b)

f() 
def f():
    y = [1,2,3,4]

    a = _a(y)

    b = _b(y)

    print(a, b)

def _b(y):
    b = sum([x2 for x2 in y])
    return b

def _a(y):
    a = sum((x1 for x1 in y))
    return a

f()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions