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

SyntaxError: New message "expected 'else' after 'if' expression" wrongly shown #89001

Closed
aroberge mannequin opened this issue Aug 5, 2021 · 9 comments
Closed

SyntaxError: New message "expected 'else' after 'if' expression" wrongly shown #89001

aroberge mannequin opened this issue Aug 5, 2021 · 9 comments
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@aroberge
Copy link
Mannequin

aroberge mannequin commented Aug 5, 2021

BPO 44838
Nosy @aroberge, @lysnikolaou, @pablogsal, @miss-islington
PRs
  • bpo-44838: Refine the custom syntax errors for invalid 'if' expressions #27615
  • [3.10] bpo-44838: Refine the custom syntax errors for invalid 'if' expressions (GH-27615). #27616
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-08-05.18:19:06.988>
    created_at = <Date 2021-08-05.10:45:07.528>
    labels = ['interpreter-core', '3.10']
    title = 'SyntaxError: New message "expected \'else\' after \'if\' expression" wrongly shown'
    updated_at = <Date 2021-08-05.18:19:06.987>
    user = 'https://github.com/aroberge'

    bugs.python.org fields:

    activity = <Date 2021-08-05.18:19:06.987>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-05.18:19:06.988>
    closer = 'pablogsal'
    components = ['Parser']
    creation = <Date 2021-08-05.10:45:07.528>
    creator = 'aroberge'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44838
    keywords = ['patch']
    message_count = 9.0
    messages = ['398989', '399002', '399010', '399012', '399014', '399016', '399017', '399018', '399021']
    nosy_count = 4.0
    nosy_names = ['aroberge', 'lys.nikolaou', 'pablogsal', 'miss-islington']
    pr_nums = ['27615', '27616']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue44838'
    versions = ['Python 3.10']

    @aroberge
    Copy link
    Mannequin Author

    aroberge mannequin commented Aug 5, 2021

    Given the following code containing no if expression (only if statements):

    if True:
        print('hello'
    
    if 2:
        print(123))

    The following traceback is generated in Python 3.10.0RC1

    File "...\example.py", line 2
    print('hello'
    ^^^^^^^
    SyntaxError: expected 'else' after 'if' expression

    @aroberge aroberge mannequin added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Aug 5, 2021
    @lysnikolaou
    Copy link
    Contributor

    Oh, this is a difficult one.

    It's caused by #71693 that was recently added to produce better errors for things like print('hello' if something), where the correct syntax is `print('hello' if something else 'hi').

    This is going to have a lot of false positives though, especially in cases like this, where an if statement immediately follows a line with unclosed parentheses.

    What do you think, Pablo? Should we maybe revert this?

    @pablogsal
    Copy link
    Member

    This is a tricky one because the tokenizer doesn't see the enclosed parentheses because is actually closed (the second print has two
    ')' parentheses).

    This is tricky indeed because the parser is parsing the ( after the print as a group, not as a call, and indentation doesn't matter in the group.

    We can indeed revert the PR but I'm afraid that you can use this pattern to actually trigger a bunch of other custom error messages where they should not.

    @aroberge
    Copy link
    Mannequin Author

    aroberge mannequin commented Aug 5, 2021

    I have no idea how the parser works ... but when you do the test to identify if an 'else' is missing, could the fact that there is a colon instead of the expected 'else' be used to avoid misidentifying this case?

    Note: I am keenly aware of the difficulties in identifying possible cause of syntax errors and do appreciate the many improvements that have been done.

    @pablogsal
    Copy link
    Member

    Technically this example si more or less equivalent to:

    [ something, blech

    if 42:
       True

    [Bluch]]

    I wonder if the better fix here is just not to raise the rule if we find a ':', which is the only token that actually makes sense after

    'if' expression

    @pablogsal
    Copy link
    Member

    but when you do the test to identify if an 'else' is missing, could the fact that there is a colon instead of the expected 'else' be used to avoid misidentifying this case?

    Yeah! That is what I was thinking. The key here is that the ':' is the only token that is valid after the construct, so anything else is a wrong 'if' expression, no matter what it is.

    @pablogsal
    Copy link
    Member

    Opened #27615

    @pablogsal
    Copy link
    Member

    New changeset f5cbea6 by Pablo Galindo Salgado in branch 'main':
    bpo-44838: Refine the custom syntax errors for invalid 'if' expressions (GH-27615)
    f5cbea6

    @miss-islington
    Copy link
    Contributor

    New changeset b1bd16c by Pablo Galindo Salgado in branch '3.10':
    [3.10] bpo-44838: Refine the custom syntax errors for invalid 'if' expressions (GH-27615). (GH-27616)
    b1bd16c

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants