Instagram / Instagram/LibCST

Unhelpful error message in `ParserSyntaxError`

Open
#1,028 1 comment 0 reactions 0 assignees View on GitHub
enhancement parsing
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

Passing this on from https://github.com/HypothesisWorks/hypothesis/issues/3759:

```python
# repro.py
import libcst

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

compile(source, "", "exec") # for comparison
```
```python-traceback
$ 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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.