跳到内容

string-dot-format-extra-named-arguments (F522) (字符串格式化多余的命名参数)

派生自 Pyflakes 代码检查器。

有时提供修复。

作用

Checks for str.format calls with unused keyword arguments. (检查 str.format 调用中未使用的关键字参数。)

为什么这不好?

Unused keyword arguments are redundant, and often indicative of a mistake. They should be removed. (未使用的关键字参数是多余的,通常表明存在错误。 应该删除它们。)

示例

"Hello, {name}".format(greeting="Hello", name="World")

建议改为

"Hello, {name}".format(name="World")

修复安全性

This rule's fix is marked as unsafe if the unused keyword argument contains a function call with potential side effects, because removing such arguments could change the behavior of the code. (如果未使用的关键字参数包含具有潜在副作用的函数调用,则此规则的修复将被标记为不安全,因为删除此类参数可能会改变代码的行为。)

例如,在以下情况下,修复将被标记为不安全

"Hello, {name}".format(greeting=print(1), name="World")

参考