simonpercivall / astunparse

An AST unparser for Python
Other
223 stars 53 forks source link

Docstrings are unparsed as simple string values #13

Closed gaborbernat closed 7 years ago

gaborbernat commented 7 years ago
# coding=utf-8
""" Test. """

import ast
import inspect

import astunparse

def x():
    """
    Test
    """
    pass

if __name__ == '__main__':
    p = ast.parse(inspect.getsource(x))
    r = astunparse.unparse(p)
    print(r)

becomes

def x():
    '\n    Test\n    '
    pass
simonpercivall commented 7 years ago

Hi! I must have forgotten to reply. astunparse makes no effort to exactly replicate the original source, and triple-quoted strings are functionally no different from regular strings.

mskoh52 commented 5 years ago

Hi, just finding this issue now. Is there any reason not to use ast.get_docstring to check if the first item should be interpreted as a triple-quoted string? (p.s. happy to provide a PR)

simonpercivall commented 5 years ago

The biggest reason it hasn't been done: astunparse is meant to replicate as closely as possible what the Tools/parser/unparse.py code in the official Python source is doing.

If this would be a trivial PR, feel free to go ahead. However, the project has had several edge case bugs due to diffs vs. the source, so I'm generally wary of straying far.