auto-walrus icon indicating copy to clipboard operation
auto-walrus copied to clipboard

Only function-body-level walrusables are walrused

Open akx opened this issue 2 months ago • 0 comments

def foo(data):
    if True:
        foo = data.get("blah")
        if foo:
            return foo.copy()
    return data.copy()

does not get walrused, even if foo is definitely only used in the if block.

This was apparently changed in 9444157b825c85d2e5b (only looking for top-level assignments by virtue of for _node in node.body:), and indeed changing back to for _node in ast.walk(node): makes the above walrusable, but I suppose it might also make some transformations more unsafe unless auto-walrus starts checking whether a name is only used in a given block.

akx avatar Nov 03 '25 08:11 akx