astpp.parseprint gives a wrong value sometimes, in case of Starred nodes

Issue #10 invalid
Bhumil Haria created an issue

astpp.prettyprint() seems to fail for specific (not all) cases of Starred nodes. In my tests, it works for Store contexts, but fails for Load ones.

Repro Steps:

>>> from astpp import parseprint
>>> parseprint('a,b,c = [ *[1,2], 3]')
Module(body=[
    Assign(targets=[
        Tuple(elts=[
            Name(id='a', ctx=Store()),
            Name(id='b', ctx=Store()),
            Name(id='c', ctx=Store()),
          ], ctx=Store()),
      ], value=List(elts=[
        Starred(value=List(elts=[
            Num(n=1),
            Num(n=2),
          ], ctx=Load()), ctx=Load()),
        Num(n=3),
      ], ctx=Load())),
  ])

For the same input text, however, the AST module output differs.

>>> import ast
>>> t = ast.parse('a,b,c = [ *[1,2], 3]')
>>> t.body[0].value
<_ast.Tuple at 0x7f9ec2bc6278>

So the t.body[0].value shows List node in one case and Tuple node in another.

Do let me know if any more details are needed.

PS: I've been using this excellent AST pretty printer (and even better documentation) for the past few days. It's great!

Comments (3)

  1. Thomas Kluyver repo owner

    That's weird. parseprint is just calling astparse and then printing the results. I see a List for both ways:

     3 ast.parse('a,b,c = [ *[1,2], 3]').body[0].value
     3> <_ast.List at 0x7f921251ac18>
    
     4 astpp.parseprint('a,b,c = [ *[1,2], 3]')
    Module(body=[
        Assign(targets=[
            Tuple(elts=[
                Name(id='a', ctx=Store()),
                Name(id='b', ctx=Store()),
                Name(id='c', ctx=Store()),
              ], ctx=Store()),
          ], value=List(elts=[
            Starred(value=List(elts=[
                Num(n=1),
                Num(n=2),
              ], ctx=Load()), ctx=Load()),
            Num(n=3),
          ], ctx=Load())),
      ])
    

    Is it possible you ran them in two different versions of Python?

  2. Bhumil Haria reporter

    No, I ran them in the same shell, should've taken a screenshot.

    However, when I try the same thing in a fresh shell, I do see that they're the same. I'm not sure what happened, so I'm closing this for now. Sorry for wasting your time.

    If I see this again I'll investigate more to find more concrete repro steps, and share results.

  3. Log in to comment