← 返回首页
Asserting a tuple — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Asserting a tuple

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

Click to see the query in the CodeQL repository

When you define an assert statement to test a tuple the test will either always succeed (if the tuple is non-empty) or always fail (if the tuple is empty).

This error usually occurs when the programmer writes assert (condition, message) instead of the correct formassert condition, message

Recommendation

Review the code and determine the purpose of the assert statement:

Example

The statement assert (xxx, yyy) attempts to test a “tuple” (xxx, yyy). The original intention may be any of the alternatives listed below:

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

If you want to define a validity check on the values of a tuple then these must be tested individually.

References