断言元组¶
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
当您定义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
如果您想对元组的值定义有效性检查,那么这些值必须单独进行测试。
参考¶
Python 语言参考:断言语句.
教程点:Python 中的断言.
常见漏洞枚举:CWE-670.