Skip to content

elif raises SyntaxError when preceded by a string statement #108272

@Stock-Owl

Description

@Stock-Owl

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker,
    and am confident this bug has not been reported before

CPython versions tested on:

3.11

Operating systems tested on:

Windows

Output from running 'python -VV' on the command line:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]

A clear and concise description of the bug:

Minor bug
The following piece of code produces a syntax error. PyLance says it's an unexpected indetnation at the elif clause.

# please note that this example has been taken out of context,
# but still poses the same problem as the implementation
from multiprocessing import Value

shared_break = Value('b', True)

print("started")

"""
IF the shared value is false, then set it to true while breaking
And wait until it is false again. then continue
"""
if shared_break.value == False:
    shared_break.value = True
    while True:
        if shared_break.value == False:
            break
        pass
                            
"""
IF the shared value is true, then wait for
an input from the user then continue
"""
elif shared_break.value == True:
    print(input("Waiting for input"))

print("ended")

Moving the multiline-comment (IF shared values it true...) seems to fix the problem:

# please note that this example has been taken out of context,
# but still poses the same problem as the implementation
from multiprocessing import Value

shared_break = Value('b', True)

print("started")

"""
IF the shared value is false, then set it to true while breaking
And wait until it is false again. then continue
"""
if shared_break.value == False:
    shared_break.value = True
    while True:
        if shared_break.value == False:
            break
        pass
                            
elif shared_break.value == True:
    print(input("Waiting for input"))

print("ended")
"""
IF the shared value is true, then wait for
an input from the user then continue
"""

Also, the problem doesn't arise when multiple # comments are used:

# please note that this example has been taken out of context,
# but still poses the same problem as the implementation
from multiprocessing import Value

shared_break = Value('b', True)

print("started")

"""
IF the shared value is false, then set it to true while breaking
And wait until it is false again. then continue
"""
if shared_break.value == False:
    shared_break.value = True
    while True:
        if shared_break.value == False:
            break
        pass
                            
# IF the shared value is true, then wait for
# an input from the user then continue
elif shared_break.value == True:
    print(input("Waiting for input"))

print("ended")

PEP8 says "Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment)."

However the comment is indented at the same level as the following code.

Same issue from 8 years ago

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions