CodeQL 文档

断言元组

ID: py/asserts-tuple
Kind: problem
Security severity: 
Severity: error
Precision: very-high
Tags:
   - reliability
   - maintainability
   - external/cwe/cwe-670
Query suites:
   - python-security-and-quality.qls

点击查看 CodeQL 存储库中的查询

当您定义assert语句来测试元组时,测试将始终成功(如果元组非空)或始终失败(如果元组为空)。

此错误通常发生在程序员编写assert (condition, message)而不是正确形式assert condition, message

建议

检查代码并确定assert语句的用途

  • 如果“元组”是在错误中创建的,则删除括号并更正语句

  • 如果打算验证元组,则应为元组的每个元素定义assert语句。

示例

语句assert (xxx, yyy)尝试测试“元组”(xxx, yyy)。原始意图可能是下面列出的任何替代方案

assert xxx and yyy   # Alternative 1a. Check both expressions are true

assert xxx, yyy      # Alternative 1b. Check 'xxx' is true, 'yyy' is the failure message.

tuple = (xxx, yyy)   # Alternative 2. Check both elements of the tuple match expectations.
assert tuple[0]==xxx
assert tuple[1]==yyy

如果您想对元组的值定义有效性检查,那么这些值必须单独进行测试。

参考

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