Wrong number of arguments for formatยถ
ID: py/percent-format/wrong-arguments
Kind: problem
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
- external/cwe/cwe-685
Query suites:
- python-security-and-quality.qls
Click to see the query in the CodeQL repository
A formatting expression, that is an expression of the format fmt % arguments must have the correct number of arguments on the right hand side of the expression. Otherwise, a TypeError will be raised.
Recommendationยถ
Change the format to match the arguments and ensure that the right hand argument always has the correct number of elements.
Exampleยถ
In the following example the right hand side of the formatting operation can be of length 2, which does not match the format string.
def unsafe_format():
if unlikely_condition():
args = (1,2)
else:
args = (1, 2, 3)
return "%s %s %s" % args
Referencesยถ
Python Library Reference: String Formatting.
Common Weakness Enumeration: CWE-685.