CodeQL 文档

格式化字符串混合使用隐式和显式编号的字段

ID: py/str-format/mixed-fields
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

点击查看 CodeQL 仓库中的查询

格式化表达式,即 the_format.format(args)format(the_format, args) 形式的表达式,可以使用显式编号的字段,例如 {1},或隐式编号的字段,例如 {},但不能同时使用两者。这样做会导致抛出 ValueError

建议

使用显式编号的字段或隐式编号的字段,但保持一致。

示例

在以下示例中,格式化使用隐式,{},和显式,{1},字段编号,这是非法的。

def illegal_format():
    "{} {1}".format("spam", "eggs")

参考资料

  • ©2025GitHub, Inc.
  • 条款
  • 隐私