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

Unhelpful error message in ParserSyntaxError #1028

Open
Zac-HD opened this issue Oct 1, 2023 · 1 comment
Open

Unhelpful error message in ParserSyntaxError #1028

Zac-HD opened this issue Oct 1, 2023 · 1 comment
Labels
enhancement New feature or request parsing Converting source code into CST nodes

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Oct 1, 2023

Passing this on from HypothesisWorks/hypothesis#3759:

# repro.py
import libcst

source = "def f(*):\n pass\n"
libcst.parse_module(source)

compile(source, "", "exec")  # for comparison
$ python repro.py 
Traceback (most recent call last):
    libcst.parse_module("def f(*):\n pass\n")
  ...
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:5.
parser error: error at 1:8: expected one of ,, NAME

 pass
     ^

# and if we comment out the `parse_module()` call, we see Python gives us:
Traceback (most recent call last):
    compile(source, "", "exec")
  ...

    def f(*):
          ^
SyntaxError: named arguments must follow bare *

The error message we get from libcst points to 1:8, i.e. the close-paren of the function definition, but the exception metadata instead points to the end of the function body (for def f(*): ... it points to the start of the def). Additionally, we see expected one of ,, NAME, which suggests that the message is constructed from three alternatives of which two have an empty string representation.

I've included the error raised by Python 3.10 for comparison; it points directly to the * and has a presumably-hand-written message explaining what's going on. Unclear how much of that is worth adopting or copying over, but testing that the location of syntax errors is compatible might be worthwhile?

@zsol
Copy link
Member

zsol commented Oct 6, 2023

I completely agree the parser error messages are often downright misleading as opposed to helpful. It would be great to improve them, but currently the parser generator library we use make it difficult to emit high-quality error messages.
See kevinmehall/rust-peg#276 and kevinmehall/rust-peg#289 for improving that situation.

@zsol zsol added enhancement New feature or request parsing Converting source code into CST nodes labels Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request parsing Converting source code into CST nodes
Projects
None yet
Development

No branches or pull requests

2 participants