格式化调用参数不足¶
ID: py/str-format/missing-argument
Kind: problem
Security severity:
Severity: error
Precision: high
Tags:
- reliability
- correctness
Query suites:
- python-security-and-quality.qls
格式化表达式(即 the_format.format(args)
或 format(the_format, args)
形式的表达式)必须具有足够的参数以匹配格式。否则,将引发 IndexError
。
建议¶
更改格式以匹配参数,或确保参数数量足够。
示例¶
在以下示例中,对 str.format
方法的调用只能提供 2 个参数,这对于使用的格式字符串来说是不够的。要解决此问题,应在第 4 行提供第三个参数。
def unsafe_format():
the_format = "{} {} {}"
if unlikely_condition():
return the_format.format(1, 2)
else:
return the_format.format(1, 2, 3)
参考资料¶
Python 库参考:字符串格式化。