◐ Shell
clean mode source ↗

bpo-40599: Improve error messages with expected keywords by pablogsal · Pull Request #20039 · python/cpython

I have a bit of feedback on some of the examples. When/if I find the time, I'll experiment further with the PR locally and try to provide more in-depth feedback.

>>> from x impirt l
  File "<stdin>", line 1
    from x impirt l
           ^
SyntaxError: Invalid syntax. Expected one of: import

Huge +1 for offering a specific suggestion when it's unambiguous. However, I think that it could look a significantly better to omit the "one of" when there is only when possible suggestion (assuming it's not too much of an implementation issue). E.g. SyntaxError: Invalid syntax. Expected: import.

>>> with x os y
  File "<stdin>", line 1
    with x os y
           ^
SyntaxError: Invalid syntax. Expected one of: as, or, if, in, is, not, and

I'm -0 on the behavior in the above example. Offering 5+ possible suggestions (7 in this case) for keywords doesn't seem especially helpful and results in a rather long exception message.

My personal preference would be to only display the possible suggestion(s) if there are only 4 or less (maybe 3?) possible keywords, and omit "one of" when there's only one.

As for the implementation details, my knowledge of the C-API and especially the parser is rather limited, but would it be possible to check the number of possible keywords with something like Py_ssize_t err_tokens_size = PySet_Size(p->err_tokens)?

If the above or something along those lines would be feasible, I would be +1 on the PR overall. Otherwise, I'm +0. It seems like a good idea, but I think the error message would be a bit too bloated when there's too many possible keywords.