跳到内容

unnecessary-literal-within-tuple-call (C409)

Derived from the flake8-comprehensions linter. (源自 flake8-comprehensions linter。)

修复总是可用的。

作用

检查 tuple 调用中使用了不必要的列表或元组字面量作为参数的情况。在 预览版 中,这也包括了元组调用中不必要的列表推导式。

为什么这不好?

tuple() 调用中使用列表或元组字面量是不必要的,因为这些类型有字面量语法。

如果传递了列表字面量,则应将其重写为 tuple 字面量。否则,如果传递了元组字面量,则应删除对 tuple() 的外部调用。

预览版 中,此规则还会检查 tuple() 调用中的列表推导式。如果找到列表推导式,则应将其重写为生成器表达式。

示例

tuple([1, 2])
tuple((1, 2))
tuple([x for x in range(10)])

建议改为

(1, 2)
(1, 2)
tuple(x for x in range(10))

修复安全性

This rule's fix is marked as unsafe, as it may occasionally drop comments when rewriting the call. In most cases, though, comments will be preserved. (此规则的修复被标记为不安全,因为它在重写调用时有时会删除注释。但在大多数情况下,注释将被保留。)