ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Ruff fails to parse `WithItem` with a starred expression as `optional_vars`

Open LaBatata101 opened this issue 1 year ago • 7 comments
trafficstars

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=[])
>>>

Playground

LaBatata101 avatar Nov 29 '23 23:11 LaBatata101

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

MichaReiser avatar Nov 29 '23 23:11 MichaReiser

Interesting. This does not parse for me in the REPL

I think that might be a runtime error?

dhruvmanila avatar Nov 30 '23 21:11 dhruvmanila

Black throws a parse error for this too.

zanieb avatar Nov 30 '23 21:11 zanieb

Yeah I think this might be a runtime syntax error.

charliermarsh avatar Nov 30 '23 21:11 charliermarsh

For context, here's where the error is being raised in CPython: https://github.com/python/cpython/blob/5b0629966f47542527400b03498d5156846f0da6/Python/compile.c#L6272-L6278

dhruvmanila avatar Dec 01 '23 01:12 dhruvmanila

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?

MichaReiser avatar Dec 01 '23 01:12 MichaReiser

This is fixed with the new parser.

dhruvmanila avatar Apr 08 '24 12:04 dhruvmanila