Skip to content

Incorrect unparsing of extended slices in Python 3.9 #62

@mdanatg

Description

@mdanatg

Repro:

Python 3.9.2 (default, Mar  3 2021, 20:02:32) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import astunparse
>>> import ast
>>> n = ast.parse('return foo(x)[:, tf.newaxis] - foo(x)[tf.newaxis, :]')
>>> astunparse.unparse(n)
'\nreturn (foo(x)[(:, tf.newaxis)] - foo(x)[(tf.newaxis, :)])\n'
>>> ast.parse(_)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mdan/miniconda3/envs/py39test/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 2
    return (foo(x)[(:, tf.newaxis)] - foo(x)[(tf.newaxis, :)])
                    ^
SyntaxError: invalid syntax
>>> 

It seems that the extra parens inserted around the slice are at fault. They do not appear in Python 3.7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions