Skip to content

gh-115177: Clarify impact of -O and optimization in assert statement documentation #119305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Doc/reference/simple_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,20 @@ The extended form, ``assert expression1, expression2``, is equivalent to ::
These equivalences assume that :const:`__debug__` and :exc:`AssertionError` refer to
the built-in variables with those names. In the current implementation, the
built-in variable :const:`__debug__` is ``True`` under normal circumstances,
``False`` when optimization is requested (command line option :option:`-O`). The current
``False`` when optimization is requested (command line option :option:`-O`
or environment variable :envvar:`PYTHONOPTIMIZE`). The current
code generator emits no code for an assert statement when optimization is
requested at compile time. Note that it is unnecessary to include the source
code for the expression that failed in the error message; it will be displayed
as part of the stack trace.

.. note::

It is considered good practice to not use :keyword:`assert` statements in
critical code logic while writing library code as enabling optimization will
cause the statements to be ignored. Evaluating function variables using
:keyword:`assert` statements can also lead to unexpected behavior.

Assignments to :const:`__debug__` are illegal. The value for the built-in variable
is determined when the interpreter starts.

Expand Down
Loading