Skip to content

del (a, [b]) is currently accepted syntax by the python interpreter #113587

Open
@15r10nk

Description

@15r10nk

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.

echo "del (a,[b])" | python3.12 -m dis
  0           0 RESUME                   0
  1           2 DELETE_NAME              0 (a)
              4 DELETE_NAME              1 (b)
              6 RETURN_CONST             0 (None)

echo "del (a,[b])" | python3.12 -m ast
Module(
   body=[
      Delete(
         targets=[
            Tuple(
               elts=[
                  Name(id='a', ctx=Del()),
                  List(
                     elts=[
                        Name(id='b', ctx=Del())],
                     ctx=Del())],
               ctx=Del())])],
   type_ignores=[])

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) and del a,b generate different python ast.

echo "del (a,b)" | python3.12 -m ast
Module(
   body=[
      Delete(
         targets=[
            Tuple(
               elts=[
                  Name(id='a', ctx=Del()),
                  Name(id='b', ctx=Del())],
               ctx=Del())])],
   type_ignores=[])
echo "del a,b" | python3.12 -m ast
Module(
   body=[
      Delete(
         targets=[
            Name(id='a', ctx=Del()),
            Name(id='b', ctx=Del())])],
   type_ignores=[])

The ast documentation says:

class ast.Delete(targets)
Represents a del statement. targets is a list of nodes, such as Name, Attribute or Subscript nodes

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 that del (a,b) and del a,b would produce the same ast and that del (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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesdocsDocumentation in the Doc dirtopic-parsertype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions