Skip to content
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

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

Closed
mokichih opened this issue Mar 20, 2025 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@mokichih
Copy link

mokichih commented Mar 20, 2025

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

@mokichih mokichih added the type-bug An unexpected behavior, bug, or error label Mar 20, 2025
@brianschubert
Copy link
Contributor

I can’t reproduce this in 3.11.11 or 3.13.2 (both highlight the entire expression). This has likely already been fixed.

@ambv ambv closed this as completed Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants