An assert statement has a side-effect¶
ID: py/side-effect-in-assert
Kind: problem
Security severity:
Severity: error
Precision: high
Tags:
- reliability
- maintainability
Query suites:
- python-security-and-quality.qls
Click to see the query in the CodeQL repository
All code defined in assert statements is ignored when optimization is requested, that is, the program is run with the -O
flag. If an assert statement has any side-effects then the behavior of the program changes when optimization is requested.
Recommendation¶
Move all expressions with side-effects out of assert statements.
Example¶
In the example, the exit code from subprocess.call()
is checked against 0, but the entire expression is called from within an assert
statement. If the code is ever run, then the not only the assertion itself, but also the external call, will be discarded. It is better to save the result of subprocess.call()
to a temporary variable, and to assert that variable to be 0.
assert subprocess.call(['run-backup']) == 0
References¶
Python Language Reference: The assert statement.
TutorialsPoint, Python Programming: Assertions in Python.