Skip to content

Improve the assert statement documentation to clarify impact of -O and optimization #115177

@davidlbaird

Description

@davidlbaird

Documentation

In https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement,

The existing documentation states, "The current code generator emits no code for an assert statement when optimization is requested at compile time." This could include more details that setting the -O flag on the Python executable, or setting the PYTHONOPTIMIZE environment variable, will disable all asserts statements in code. Another improvement is a reminder, especially for library code authors, that if their code logic depends on assert statements as part of their critical code logic, that logic could break when optimization is enabled from the command line or in the environment. Another risk is evaluating function variables with assert in order to avoid typing errors on the return value, like declaring the return type as -> int, and adding an assert x is not None before a return x statement.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc direasy

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions