Non-callable called¶
ID: py/call-to-non-callableKind: problemSecurity severity: Severity: errorPrecision: highTags: - quality - reliability - correctnessQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
If an object is called,obj(), then that object must be a callable or aTypeError will be raised. A callable object is any object whose class defines the__call__ special method. Callable objects include functions, methods, classes.
Thecallable(object) builtin function determines if an object is callable or not.
When the Python interpreter attempts to evaluate a call such asfunc(arg) it will invoke the__call__ special method onfunc. Thus,func(arg) is roughly equivalent totype(func).__call__(func,arg) which means that theclass must define the attribute__call__, merely adding it to the instance is not sufficient.
Recommendation¶
Since this problem usually indicates a logical error, it is not possible to give a general recipe for fixing it.
Example¶
lists are not callable. In this example, an attempt is made to call alist which will fail with aTypeError.
a_list=[]a_list()
References¶
Python Standard Library:callable.
Python Language Reference:object.call.