ruff
ruff copied to clipboard
Ruff fails to parse `WithItem` with a starred expression as `optional_vars`
Ruff version: v0.1.6
Ruff reports a syntax error for the following code
with 0 as *x: pass
While the python ast module parses with no error.
$ python
Python 3.12.0 (main, Oct 3 2023, 01:27:23) [Clang 17.0.1 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> print(ast.dump(ast.parse("with a as *a: pass"), indent=4))
Module(
body=[
With(
items=[
withitem(
context_expr=Name(id='a', ctx=Load()),
optional_vars=Starred(
value=Name(id='a', ctx=Store()),
ctx=Store()))],
body=[
Pass()])],
type_ignores=[])
>>>
Interesting. This does not parse for me in the REPL
>>> with 0 as *x: pass
...
File "<stdin>", line 1
SyntaxError: starred assignment target must be in a list or tuple
But I would expect it to parse from the grammar spec
with_item:
| expression 'as' star_target &(',' | ')' | ':')
| expression
...
star_target:
| '*' (!'*' star_target)
| target_with_star_atom
target_with_star_atom:
| t_primary '.' NAME !t_lookahead
| t_primary '[' slices ']' !t_lookahead
| star_atom
star_atom:
| NAME
| '(' target_with_star_atom ')'
| '(' [star_targets_tuple_seq] ')'
| '[' [star_targets_list_seq] ']'
https://docs.python.org/3/reference/grammar.html
Interesting. This does not parse for me in the REPL
I think that might be a runtime error?
Black throws a parse error for this too.
Yeah I think this might be a runtime syntax error.
For context, here's where the error is being raised in CPython: https://github.com/python/cpython/blob/5b0629966f47542527400b03498d5156846f0da6/Python/compile.c#L6272-L6278
I wonder why they don't raise the error at parse time. Ideally this would be a lint rule that warns you about these runtime syntax errors but, considering that it isn't a valid Python program, fixing this doesn't seem urgent to me. Maybe something we can do when rewriting our parser?
This is fixed with the new parser.