mbdevpl / horast

Human-oriented abstract syntax tree (AST) parser/unparser for Python 3 that doesn't discard comments.
Apache License 2.0
16 stars 6 forks source link

docstring not parsed correctly #5

Open ZdenekM opened 4 years ago

ZdenekM commented 4 years ago

Following simple test fails:

import horast
import inspect

class Test:
    """
    multiline
    docstring
    """

original_code = inspect.getsource(Test)
tree = horast.parse(original_code)
unparsed_code = horast.unparse(tree)
assert original_code == unparsed_code

because tree looks like this:

Module(
  body=[ClassDef(
    name='Test',
    bases=[],
    keywords=[],
    body=[Expr(value=Str(
      s='\n    multiline\n    docstring\n    ',
      kind=''))],
    decorator_list=[])],
  type_ignores=[])

Would it be possible to fix this somehow? I can probably use some temporary workaround, but it would be really nice to have this working out of the box. I'm going to generate classes/methods with docstrings.

dferens commented 4 years ago

Hi @ZdenekM , AST produced by horast is almost identical to one produced by ast, the problem is in different package - https://github.com/simonpercivall/astunparse/issues/47 I suggest you to upgrade to 3.9: https://docs.python.org/3.9/library/ast.html#ast.unparse

ZdenekM commented 4 years ago

@dferens Thanks for the tip, I will give it a try!