The grammar for del statement:
del_stmt: 'del' (expr|star_expr) (',' (expr|star_expr))* [',']
It allows deleting starred expressions. So, the parser doesn't mind this, even though it would be causing SyntaxError: can't use starred expression here
at runtime:
>>> ast.parse("del *a")
<_ast.Module at 0xcafef00d>
>>> ast.dump(compile('del *a', filename='wtf.py', mode='single', flags=ast.PyCF_ONLY_AST))
"Interactive(body=[Delete(targets=[Starred(value=Name(id='a', ctx=Del()), ctx=Del())])])"
Is there some usage I'm missing which can have a starred delete target? Shouldn't it be instead the simpler grammar:
del_stmt: 'del' expr (',' expr)* [',']
Why does the grammar allow for deleting starred expressions?
The actual grammar is defined in terms of exprlist
; it looks like that association dates back to before 3.0, when star_expr
became a thing; previously, it was expr
exclusively.
Basically, the grammar allows it because they added a new feature to exprlist
, and didn't go to the trouble of making it illegal by the grammar in all the cases where star_expr
doesn't actually make sense, they just made the compiler reject nonsensical cases at a later stage.
It's hacky, but it works, and I assume either helps simplify the parser, and/or it's just too much trouble to define and implement separate concepts for exprlist
vs. maybe_star_exprlist
.