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

Syntax errors in else blocks are reported at else #119724

Closed
encukou opened this issue May 29, 2024 · 8 comments
Closed

Syntax errors in else blocks are reported at else #119724

encukou opened this issue May 29, 2024 · 8 comments
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker

Comments

@encukou
Copy link
Member

encukou commented May 29, 2024

Since #29513, syntax errors in else and elif blocks, like:

if 1:
    pass
else:
    This is invalid syntax (sic)

are reported at the else:

  File "/tmp/repro.py", line 3
    else:
    ^^^^^^
SyntaxError: 'else' must match a valid statement here

This is quite unhelpful when a small typo is hiding in a large block.

@lysnikolaou, could you take a look?

Linked PRs

@encukou encukou changed the title Syntax errors in else blocks are ignored Syntax errors in else blocks are reported at else May 29, 2024
@encukou
Copy link
Member Author

encukou commented May 31, 2024

@lysnikolaou Do you think this can be solved soon?

Beta 2 is coming up; if this can't be fixed quickly it might be better to revert #29513 and try again.

Marking as a potential release blocker.

@AlexWaygood AlexWaygood added 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels May 31, 2024
@lysnikolaou
Copy link
Contributor

I gave this a go yesterday, but I couldn't come up with something that both fixes this and keeps the current behavior when there's an unmatched else/elif. I'll give another try later and report back.

@erlend-aasland
Copy link
Contributor

Would you consider this an improvement?

Python 3.14.0a0 (heads/autoconf/curses-dirty:739af749138, Jun  3 2024, 13:59:59) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1:
...     pass
... else:
...     This is invalid syntax (sic)
  File "<unknown>", line 4
    This is invalid syntax (sic)
                                ^
SyntaxError: 'else' must match a valid statement here
>>> 

@encukou
Copy link
Member Author

encukou commented Jun 3, 2024

Hard to say with that example. Here is a less minimal one; IMO the error should be “near” the bad line:

if 1:
    pass
else:
    while True:
        do_necessary_things()
        if should_end:
            retrun True  # sic
        do_correct_things()
        do_good_things()

@erlend-aasland
Copy link
Contributor

In this case, yes it shows the offending line, but not the exact column.

@pablogsal
Copy link
Member

Yeah, I think the correct thing to do here is reverting

@lysnikolaou
Copy link
Contributor

I couldn't make it work well. Let's revert and reevaluate.

@pablogsal
Copy link
Member

yeah, the ratio benefit/complexity it's quite important for these error messages and I prefer to have something dummer than showing incorrect things to the user or having to maintain some gigantic rule so it's fine to just not do it

Yhg1s pushed a commit that referenced this issue Jun 4, 2024
Yhg1s pushed a commit to Yhg1s/cpython that referenced this issue Jun 4, 2024
…non-matching 'elif'/'else' statements (pythonGH-29513)" (pythonGH-119974)

This reverts commit 1c8f912.
(cherry picked from commit 31a4fb3)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Yhg1s pushed a commit to Yhg1s/cpython that referenced this issue Jun 4, 2024
…non-matching 'elif'/'else' statements (pythonGH-29513)" (pythonGH-119974)

This reverts commit 1c8f912.
(cherry picked from commit 31a4fb3)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
encukou pushed a commit that referenced this issue Jun 4, 2024
…tching 'elif'/'else' statements (GH-29513)" (GH-119974) (GH-120013)


This reverts commit 1c8f912.
(cherry picked from commit 31a4fb3)
@encukou encukou closed this as completed Jun 4, 2024
barneygale pushed a commit to barneygale/cpython that referenced this issue Jun 5, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker
Projects
Development

No branches or pull requests

5 participants