t3rn0 / ast-comments

Extension to the built-in ast module. Finds comments in source code and adds them to the parsed tree.
MIT License
31 stars 10 forks source link

Add support for docstrings #7

Closed ghadagd closed 1 year ago

ghadagd commented 1 year ago

I made this more for personal convenience this doesn't need to be merged it just makes sure docstrings are also added to the "comments" field

Also added some tests for them

ghadagd commented 1 year ago

pr sort of breaks some stuff dont merge yet if ur planning to

t3rn0 commented 1 year ago

Hi. Thank you for making this PR.

However, I wouldn't recommend mixing comments and docstrings. After all, they are not the same. It's like we're losing some information storing them this way. Moreover, this functionality is already in the built-in ast module. And it works perfectly. Yes, you have to check (or handle exceptions) if a node is not a function, class, etc. But still, this information is already there. One can even make parse-unparse roundtrip and the docstring will be well-preserved.

>>> source = """
... def my_func():
...     '''my docstring'''
... """
>>> print(ast.unparse(ast.parse(source)))
def my_func():
    """my docstring"""

One thing to consider: The major issue with the current implementation is that the library does not preserve comments after ast_comments.unparse. Mixing comments and docstrings will make it harder to separate one from another.

ghadagd commented 1 year ago

Hi. thanks for the very fast response

as I said this was more made for personal convenience so I could use .comments always and not need to have checks for docstrings etc.

If you think this isn't a good fit for the repo feel free to close it.