Skip to content

Incorrect error highlighting in assert statement with short-circuit evaluation in Python 3.11.2 #131497

@mokichih

Description

@mokichih

Bug report

Bug description:

Environment

# python -VV
Python 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0]

# uname -a
Linux flower 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06) x86_64 GNU/Linux

How to reproduce

  1. Create main.py with the following contents:
# main.py
assert 0 <= -1 and 10 < 20
  1. Run main.py. An AssertionError occurs:
mokichi@flower:~/python_bug$ python3 main.py 
Traceback (most recent call last):
  File "/home/mokichi/python_bug/main.py", line 1, in <module>
    assert 0 <= -1 and 10 < 20
                       ^^^^^^^
AssertionError

Problem

Currently, highlighting tells us that the assertion error has occurred in expression 10 < 20.
However, according to short-circuit evaluation, the expression that has really activated assertion error is the first one: 0 <= -1.

Possible fixes

  1. Highlight only the evaluated part
    Evaluate expressions separated by and from left to right. When an expression gets evaluated to False, highlight only that expression. (I think this is more intuitive since this follows short-circuit evaluation rule.)

  2. Highlight the whole expression
    When any part of assert statement gets evaluated to False, highlight whole expression. This fix may be little unfriendly than previous fix, but better than current incorrect behavior.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions