google / pasta

Library to refactor python code through AST manipulation.
Apache License 2.0
341 stars 40 forks source link

Support for async code #83

Closed cript0nauta closed 4 years ago

cript0nauta commented 4 years ago

Hi! I noted that pasta isn't able to parse async Python code. For example, this script will raise an error instead of succeeding:


import pasta

source_code = """
async def f():
    return
"""

pasta.parse(source_code)

I got the following traceback:

Traceback (most recent call last):
  File "sscce.py", line 8, in <module>
    pasta.parse(source_code)
  File "/src/.venv/src/pasta/pasta/__init__.py", line 25, in parse
    annotator.visit(t)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 1214, in visit
    super(AstAnnotator, self).visit(node)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 133, in visit
    super(BaseVisitor, self).visit(node)
  File "/nix/store/hmcha8bv2ziwabla76hd612vgxr20pnd-python3-3.7.6/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 47, in wrapped
    f(self, node, *args, **kwargs)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 225, in visit_Module
    self.generic_visit(node)
  File "/nix/store/hmcha8bv2ziwabla76hd612vgxr20pnd-python3-3.7.6/lib/python3.7/ast.py", line 279, in generic_visit
    self.visit(item)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 1214, in visit
    super(AstAnnotator, self).visit(node)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 133, in visit
    super(BaseVisitor, self).visit(node)
  File "/nix/store/hmcha8bv2ziwabla76hd612vgxr20pnd-python3-3.7.6/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "/nix/store/hmcha8bv2ziwabla76hd612vgxr20pnd-python3-3.7.6/lib/python3.7/ast.py", line 279, in generic_visit
    self.visit(item)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 1214, in visit
    super(AstAnnotator, self).visit(node)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 133, in visit
    super(BaseVisitor, self).visit(node)
  File "/nix/store/hmcha8bv2ziwabla76hd612vgxr20pnd-python3-3.7.6/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 47, in wrapped
    f(self, node, *args, **kwargs)
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 680, in visit_Return
    self.token('return')
  File "/src/.venv/src/pasta/pasta/base/annotate.py", line 1353, in token
    token_val, token.src, token.start[0], token.line))
pasta.base.annotate.AnnotationError: Expected 'return' but found 'async'
line 2: async def f():

It would be nice if it could handle it properly.

soupytwist commented 4 years ago

This should be fixed in 0.2.0.