Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility to get docstring node for module/class/function #180

Closed
Jackenmen opened this issue Dec 28, 2019 · 9 comments
Closed

Add a utility to get docstring node for module/class/function #180

Jackenmen opened this issue Dec 28, 2019 · 9 comments

Comments

@Jackenmen
Copy link

Something similar to ast.get_docstring() could be useful to find docstrings more easily.

Could be even better, if it were available at Module.docstring, ClassDef.docstring, FunctionDef.docstring but this might not fit your API design that well, especially that docstring isn't a special part of python's grammar and is just a regular string and only special thing about it is that it's a first statement in module/class/function.

My own use case for such utility would be modifying a class's docstring or adding it if it's missing in a visitor. I can obviously do it without such utility, but I think it would make it easier.

@mvismonte
Copy link
Member

@jack1142 I can definitely see how this would be useful. As far as I know, we don't have any doc string APIs. The recommended way of doing something like this would be to create a Metadata Provider and reference that in your visitor/transforms in order to determine if a module/class/function has a docstring and making the appropriate action based on that.

I'm sure that's something that would be useful for others, so feel free to put up a PR if you're interested in putting that together =)

@jimmylai
Copy link
Contributor

Yeah, build a DocstringProvider will be very useful.

@Jackenmen
Copy link
Author

I'll let someone else make this, I won't have enough time to make this, just wanted to put up this feature request :)

@lensvol
Copy link

lensvol commented Feb 19, 2020

@jack1142 Will you accept a PR for this or it would be done by someone from your team?

@Jackenmen
Copy link
Author

@lensvol I'm not in the team :) But from what I know, maintainers of this project are welcome to PRs from outside.

@lensvol
Copy link

lensvol commented Feb 19, 2020

@jack1142 Sorry, I was actually trying to mention @mvismonte but my thumb slipped :) I am currently working on something similar, will try to see if it fits the purpose of this issue.

@jimmylai
Copy link
Contributor

@lensvol Yes, your PR will be very welcome! We've accepted PRs from a couple external contributors. https://github.com/Instagram/LibCST/graphs/contributors

@jimmylai
Copy link
Contributor

jimmylai commented Feb 19, 2020

We recently start adding more helper functions to achieve write less and do more.
We recommend add simple and read-only helpers as attributes of CSTNode, e.g. SimpleString.prefix, quote, raw_value.
https://libcst.readthedocs.io/en/latest/nodes.html#libcst.SimpleString

Docstring is the first string literal statement of module, function or class. It falls under the simple and read-only helper category. So make them as Node.docstring with type Optionoal[str] makes sense.

@jimmylai
Copy link
Contributor

jimmylai commented Mar 5, 2020

get_docstring helper is implemented in #254
It'll be available in the next LibCST release.

@jimmylai jimmylai closed this as completed Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants