Asserting a tuple¶
ID: py/asserts-tuple Kind: problem Severity: error Precision: very-high Tags: - reliability - maintainability - external/cwe/cwe-670 Query suites: - python-security-and-quality.qls
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 form
assert condition, message
Review the code and determine the purpose of the
If the “tuple” has been created in error, then remove the parentheses and correct the statement
If validation of a tuple is intended, then you should define an
assertstatement for each element of the tuple.
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==xxx assert tuple==yyy
If you want to define a validity check on the values of a tuple then these must be tested individually.