refactor icon indicating copy to clipboard operation
refactor copied to clipboard

Unparsed code missing parents around BinOp

Open muirdm opened this issue 11 months ago • 0 comments

With a simplified runner like:

import ast

import refactor


class Replace(refactor.Rule):
    def match(self, node):
        if type(node) == ast.Call:
            return refactor.Replace(
                node,
                ast.Call(
                    ast.Attribute(node.args[0], node.func.id),
                    node.args[1:],
                    node.keywords,
                ),
            )
        return None


if __name__ == "__main__":
    refactor.run(rules=[Replace])

Running it against:

foo(1 + 2)

I expect the output:

(1 + 2).foo()

But it produces the invalid:

1 + 2.foo()

The stdlib ast.unparse() seems to do this correctly, so I suspect it is a problem in refactor.

Also note that the issue isn't limited to BinOp.

muirdm avatar Jan 23 '25 23:01 muirdm