Search code examples
pythonabstract-syntax-treeastroid

Define a `Call` node in `astroid`


I would like to define a node such as this one :

from astroid import parse
print_node = parse("print()")

print(print_node.body[0].value)

# Call(func=<Name.print l.1 at 0x1abe5f02978>,
#      args=[],
#      keywords=None)

The only way I found to define it is:

new_print_node = astroid.Call()
new_print_node.postinit(func=print_node.body[0].value.func)

Is it possible to get directly the functions by instantiating a NodeNG as suggested by the documentation ? I have not found how to define the print function from NodeNG


Solution

  • This Node is simply a Name with name print.

    Such as astroid.Name(name="print")

    print_node = astroid.Call()
    print_node.postinit(
        func=astroid.Name(name="print")
    )
    

    Which gives :

    Call(func=<Name.print l.None at 0x249099ac710>,
         args=[],
         keywords=None)