Reputation: 748
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.
Upvotes: 2
Views: 3964
Reputation: 1125388
You cannot see from a call expression what arguments the callable expects. Python leaves this entirely to the callable at runtime.
For static code analysis your only option is to resolve the callable (hoping that it is not too dynamic in nature, e.g. will change at the runtime), and for C-defined objects have your own database of signatures. This is the route the Komodo CodeIntel library takes (OSS, used in other projects such as SublimeCodeIntel). C-based extensions are supported with CIX files, which essentially contain callable signatures.
At runtime, you can use inspect.signature()
to introspect call signatures. C functions are only partially supported at the moment, only those that use the Argument Clinic have signature info stored, see What are __signature__ and __text_signature__ used for in Python 3.4
Upvotes: 1