格式化调用中未使用参数¶
ID: py/str-format/surplus-argument
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- maintainability
- useless-code
Query suites:
- python-security-and-quality.qls
格式化表达式,即形式为the_format.format(args)
或format(the_format, args)
的表达式,可以具有任意数量的参数,前提是参数数量足以匹配格式。但是,多余的参数是多余的,会使代码混乱,难以阅读。
多余的参数也可能表明格式字符串中存在错误。
建议¶
检查格式字符串是否正确,然后删除所有多余的参数。
示例¶
在以下示例中,对str.format()
方法的调用有三个参数,但格式字符串仅需要两个参数。应该删除第三个参数。
def surplus_argument():
the_format = "{} {}" # Used to be "{} {} {}"
return the_format.format(1, 2, 3)
参考¶
Python 库参考:字符串格式化。