GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — An assert statement has a side-effect — CodeQL query help documentation

An assert statement has a side-effect¶

ID: py/side-effect-in-assert
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-code-quality.qls
   - 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.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.