Skip to content

Syntax errors in else blocks are reported at else #119724

Closed

Description

@encukou

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

Activity

  1. changed the title [-]Syntax errors in `else` blocks are ignored[/-] [+]Syntax errors in `else` blocks are reported at `else`[/+] on May 29, 2024
  2. encukou commented on May 31, 2024

    @encukou
    MemberAuthor

    @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.

  3. lysnikolaou commented on May 31, 2024

    @lysnikolaou
    Member

    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.

  4. added a commit that references this issue on Jun 3, 2024
  5. erlend-aasland commented on Jun 3, 2024

    @erlend-aasland
    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
    >>> 
  6. encukou commented on Jun 3, 2024

    @encukou
    MemberAuthor

    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()
  7. erlend-aasland commented on Jun 3, 2024

    @erlend-aasland
    Contributor

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

  8. pablogsal commented on Jun 3, 2024

    @pablogsal
    Member

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

  9. lysnikolaou commented on Jun 3, 2024

    @lysnikolaou
    Member

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

  10. pablogsal commented on Jun 3, 2024

    @pablogsal
    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

  11. added 3 commits that reference this issue on Jun 4, 2024
  12. added a commit that references this issue on Jun 4, 2024
  13. added a commit that references this issue on Jun 5, 2024
  14. added a commit that references this issue on Jul 11, 2024
  15. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions