Open 15r10nk opened 8 months ago
The language reference: has this grammar rule for the del statement:
del_stmt ::= "del" target_list
Target_list
is also used for the LHS of an assignment statement.
All examples are valid python code. I'm not enough of a parser expert to have an opinion on the generated AST.
target can be "[" target_list "]" so that's not a bug in the current version
indeed, the ast turns out different, but this is supported by the python grammar
so that's not a bug in the current version
This maybe depends on the definition of bug, but I don't think that the behavior was intended. target_list
is only used for del
and assignments. It looks like it was original build for assignments and later reused for del.
There are explicit tests which check if you delete anything which would be allowed by the target_list syntax but can not be deleted like star-expressions. I think that []
was just forgotten.
However, a clear documentation would be nice if ast.Tuple
or maybe even ast.List
should be valid childs for the ast.Del
. Otherwise anyone who tries to analyse code which works with ast.Del
will be surprised by its possible values.
I take it back. I found tests for this syntax:
del a, [b, c], (d, [e, f])
I think this should be added to the documentation of ast.Del
.
So, now it's a doc issue?
So, now it's a doc issue?
I think so. I'm still curious as to why it has to be that way, but I can live with it if there is intent behind it (the test is that case)
Is backport to 3.8-3.12 needed?
Is backport to 3.8-3.12 needed?
Only to 3.11 and 3.12. 3.8-3.10 accepts only security fixes.
Bug report
Bug description:
I found the following while working on pysource-codegen, which generates random python code. I don't know any real world python code which does this.
I think that this is most likely an bug in the parser.
Another issue that I found while working on this is that
del (a,b)
anddel a,b
generate different python ast.The ast documentation says:
I don't know what the best solution to this problem should be, but I would like to have a correct documentation. Especially the case that
ast.Tuple
could be a del target is missing and was confusing for me. I had expected thatdel (a,b)
anddel a,b
would produce the same ast and thatdel (a,(b,c))
is invalid syntax.del [a]
looks like a bug to me and I hope it can be fixed.CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs