格式化调用中未使用命名参数¶
ID: py/str-format/surplus-named-argument
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- maintainability
- useless-code
Query suites:
- python-security-and-quality.qls
格式化表达式,即形式为 the_format.format(args)
或 format(the_format, args)
的表达式,可以具有任意名称的关键字参数,只要提供了所有必需的名称。但是,多余的关键字参数(名称不在格式中的参数)是冗余的。这些多余的参数会使代码混乱,难以阅读。
多余的关键字参数也可能表明格式字符串中的错误。
建议¶
检查格式字符串是否正确,然后删除所有多余的关键字参数。
示例¶
在以下示例中,注释表明 chips
关键字参数不再需要,应该删除。
def surplus_argument():
the_format = "{spam} {eggs}" # Used to be "{spam} {eggs} {chips}"
return the_format.format(spam = "spam", eggs="eggs", chips="chips")
参考¶
Python 库参考:字符串格式化。