pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

if/elif/else statement need better error handling

Open Rabbit994 opened this issue 6 years ago • 5 comments

Environment data

  • Language Server version: Pylance language server 2020.6.1
  • OS and version: Windows 10 Enterprise
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.7

Expected behaviour

When doing improper if/elif/else statements (see code Snippet), I would expect Pylance to report syntax errors in similar fashion to pylint which returns following for code snippet: invalid syntax (, line 2) (syntax-error)

Actual behaviour

Pylance reports errors around missing : or Type annotation not supported for this type of expression for else statement.

Code Snippet / Additional information

spam = 'eggs'
if spam = 'eggs':
    print('Green Eggs and Spam')
else spam == 'ham':
    print('Green Eggs and Ham')

Rabbit994 avatar Jul 01 '20 23:07 Rabbit994

There is no one right way to report a syntax error; it usually depends on the parser. In pylance's case it knows that '=' is not appropriate at that location so it wants to terminate the if after if spam and reports a missing ':'.

But if I read you correctly, what you are saying is that you would prefer the error message to be "Syntax error" instead of "Expected <token>"; maybe even better would be "Syntax error: expected <token>".

gramster avatar Jul 02 '20 00:07 gramster

pylint behavior is to report "Syntax Error" which is more clear. It's a syntax error for sure and I think calling it out as syntax error and leaving it at that would be acceptable. It would be nicer for pylance to go step beyond and report "Syntax Error, == expected" or "Syntax Error, else may not have evaluation"

It's understand the difficulty of building linter for language that has such a diverse crowd from professional programmers powering backends, data scientists using it to crunch data, Machine Learning and those starting out with their first language in the world of programming. It was friend learning programming who sent me something similar and couldn't figure it out. Just trying to be the voice of a beginner.

Rabbit994 avatar Jul 02 '20 03:07 Rabbit994

There are two issues here.

The first is that we were not doing a better job with parse recovery once a parse error is detected. Your example code resulted in about eight different errors. I've improved the parse recovery in this case to avoid the cascading errors. With my improvements, you will now see only two error messages.

The second issue is with the error message. I disagree that "syntax error" is a better message. This is a very generic term, and many beginning Python users won't even know what "syntax error" means. Good error messages are understandable, specific, and provide the user with information about what was expected or what they can do to fix it. As such, I think the current error message is an appropriate and useful message.

erictraut avatar Jul 02 '20 05:07 erictraut

I think syntax error is learned quickly but again, error messages are such a Bike shed problem that I’m glad error count has been reduced. Thanks for quick turnaround.

Rabbit994 avatar Jul 02 '20 22:07 Rabbit994

This issue has been fixed in version 2020.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202070-9-july-2020

jakebailey avatar Jul 09 '20 21:07 jakebailey