Skip to content

gh-115177: Mention PYTHONOPTIMIZE in assert docs and add warning#151114

Open
nahcmon wants to merge 1 commit into
python:mainfrom
nahcmon:issue-115177-assert-docs-pythonoptimize
Open

gh-115177: Mention PYTHONOPTIMIZE in assert docs and add warning#151114
nahcmon wants to merge 1 commit into
python:mainfrom
nahcmon:issue-115177-assert-docs-pythonoptimize

Conversation

@nahcmon

@nahcmon nahcmon commented Jun 8, 2026

Copy link
Copy Markdown

The assert statement documentation at https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement
mentions the -O command-line flag as the way to request optimization, but omits the
PYTHONOPTIMIZE environment variable, which has the same effect. Users who encounter
assertion failures (or missing assertions) set via the environment variable have no
path to this information from the assert docs.

Additionally, the section contains no guidance warning against using assert for
production-critical logic (input validation, interface enforcement, etc.) — a mistake
that leads to silent failures when optimization is enabled.

Changes

  • Added :envvar:\PYTHONOPTIMIZE`cross-reference alongside the existing:option:`-O``
    mention so both ways to enable optimization are visible together.
  • Added a .. warning:: directive advising against using assert to enforce
    interface constraints or validate data in production code.

Test coverage

Documentation-only change; no test file modifications needed. RST validated
with docutils; :envvar: target confirmed defined at Doc/using/cmdline.rst:833.

NEWS entry

Not required for documentation-only changes per the CPython developer guide.

CLA

Note: the CLA for the GitHub account nahcmon may not be signed. The PR will be
held by the CLA bot until it is signed at https://cla.python.org/.

…add warning

Add a cross-reference to the PYTHONOPTIMIZE environment variable alongside
the existing -O flag mention, and add a warning advising against using assert
for production data validation or interface enforcement.
@python-cla-bot

python-cla-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33048280 | 📁 Comparing 3a57764 against main (29a920e)

  🔍 Preview build  

1 file changed
± reference/simple_stmts.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant