You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve error message for missing : before suites #87163
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=Noneclosed_at=<Date2021-02-02.19:57:31.655>created_at=<Date2021-01-22.01:20:05.352>labels= ['3.10']
title='Improve error message for missing : before suites'updated_at=<Date2021-02-02.19:57:31.654>user='https://github.com/pablogsal'
Python 3.8.6 (default, Oct 10 2020, 18:31:21)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for x inrange
File "<stdin>", line 1
for x in range
^
SyntaxError: invalid syntax
>>>
we could display:
>>> for x inrange
File "<stdin>", line 1
for x in range
^
SyntaxError: expected ':'
The same idea applies for every suite that has a missing ':'
In PR24292 I implemented this idea by adding a new element to the grammar: '&&'. This allows to hard-expect a token: if the token is not there the parsing hard-fails immediately without trying anything else and displays an appropriate error message
The other possibility if we think that PR24292 is overkill is manually adding an "invalid_wathever" for every possible compound statement option that parses the statement with a !':' and then raises appropriately. This option is more verbose in the grammar but requires no new machinery.
I propose to go with the first approach. I really like that and I can see us using it in various place in the future. It's basically a positive lookahead with a cut, but the extra feature that it raises a SyntaxError, right?
The problem with bpo-1634034 is that is not anymore possible without manual changes to the grammar with the PEG parser (or new infrastructure). The new parser does not have a defined concept of "i expected this token and now I am going to hard fail because is not there", but instead it will backtrack and try something else. We also did some work on something similar and we deemed it not useful as it was:
Activity
pablogsal commentedon Jan 22, 2021
Instead of displaying a generic syntax error:
we could display:
The same idea applies for every suite that has a missing ':'
pablogsal commentedon Jan 22, 2021
There are several ways to implement this:
In PR24292 I implemented this idea by adding a new element to the grammar: '&&'. This allows to hard-expect a token: if the token is not there the parsing hard-fails immediately without trying anything else and displays an appropriate error message
The other possibility if we think that PR24292 is overkill is manually adding an "invalid_wathever" for every possible compound statement option that parses the statement with a !':' and then raises appropriately. This option is more verbose in the grammar but requires no new machinery.
pablogsal commentedon Jan 22, 2021
PR 24293 implements the second idea (only new rules and no new machinery)
serhiy-storchaka commentedon Jan 22, 2021
See also bpo-1634034.
lysnikolaou commentedon Jan 26, 2021
I propose to go with the first approach. I really like that and I can see us using it in various place in the future. It's basically a positive lookahead with a cut, but the extra feature that it raises a SyntaxError, right?
I'll have a look at the PR now.
pablogsal commentedon Jan 26, 2021
The problem with bpo-1634034 is that is not anymore possible without manual changes to the grammar with the PEG parser (or new infrastructure). The new parser does not have a defined concept of "i expected this token and now I am going to hard fail because is not there", but instead it will backtrack and try something else. We also did some work on something similar and we deemed it not useful as it was:
bugs.python.org/issue40599
pablogsal commentedon Jan 26, 2021
Yeah, but the cut is "absolute" (because of the syntax Error). IIRC the normal cut only affects that rule, no?
lysnikolaou commentedon Jan 26, 2021
Right.
pablogsal commentedon Feb 2, 2021
New changeset 58fb156 by Pablo Galindo in branch 'master':
bpo-42997: Improve error message for missing : before suites (GH-24292)
58fb156