Is there a way (preferably using the ast module) to know if a function has a required number of arguments?
For example, if a user interface allows a user to insert a python expression for example "sum(x)" but the user has mistakenly typed "sum()" is there a way to validate that expression before runtime?
I have called ast.dump on every node in the symbol tree but the information on these symbols seems insufficient for this kind of analysis?
For example:
#own debug code added to ast.py
def visit(self, node):
print(dump(node),True, False)
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
return visitor(node)
yields:
'Module(body=[Expr(value=Call(func=Attribute(value=Name(id='Math', ctx=Load()),
attr='cos', ctx=Load()), args=[Num(n=0.5)],
keywords=[], starargs=None, kwargs=None))])'
Which doesn't really differ from a function like print() which does not require a specific number of arguments.