This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: SyntaxError: New message "expected 'else' after 'if' expression" wrongly shown
Type: Stage: resolved
Components: Parser Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2021-08-05 10:45 by aroberge, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27615 merged pablogsal, 2021-08-05 16:43
PR 27616 merged pablogsal, 2021-08-05 17:31
Messages (9)
msg398989 - (view) Author: Andre Roberge (aroberge) * Date: 2021-08-05 10:45
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
msg399002 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2021-08-05 13:00
Oh, this is a difficult one.

It's caused by GH-27506 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?
msg399010 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-05 16:16
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.
msg399012 - (view) Author: Andre Roberge (aroberge) * Date: 2021-08-05 16:29
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.
msg399014 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-05 16:40
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
msg399016 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-05 16:47
> 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.
msg399017 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-05 16:48
Opened https://github.com/python/cpython/pull/27615
msg399018 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-08-05 17:29
New changeset f5cbea6b1b5fc39cca377c6cc93f222916015fc4 by Pablo Galindo Salgado in branch 'main':
bpo-44838: Refine the custom syntax errors for invalid 'if' expressions (GH-27615)
https://github.com/python/cpython/commit/f5cbea6b1b5fc39cca377c6cc93f222916015fc4
msg399021 - (view) Author: miss-islington (miss-islington) Date: 2021-08-05 18:00
New changeset b1bd16c2528295b8b28395827f6b589dccea0624 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)
https://github.com/python/cpython/commit/b1bd16c2528295b8b28395827f6b589dccea0624
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89001
2021-08-05 18:19:06pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-05 18:00:30miss-islingtonsetnosy: + miss-islington
messages: + msg399021
2021-08-05 17:31:10pablogsalsetstage: patch review
pull_requests: + pull_request26110
2021-08-05 17:29:06pablogsalsetmessages: + msg399018
2021-08-05 16:48:21pablogsalsetmessages: + msg399017
2021-08-05 16:47:45pablogsalsetmessages: + msg399016
stage: patch review -> (no value)
2021-08-05 16:43:27pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26109
2021-08-05 16:40:57pablogsalsetmessages: + msg399014
2021-08-05 16:29:39arobergesetmessages: + msg399012
2021-08-05 16:16:31pablogsalsetmessages: + msg399010
2021-08-05 13:00:20lys.nikolaousetmessages: + msg399002
2021-08-05 10:45:07arobergecreate