Anyone know of a library for getting docstrings for attributes like Sphinx does, where it can find the triple-quote string after an attribute by inspecting the AST? I would like to use those docs, but I don't want to write the AST inspector myself 😅 #Python

@davidism

Depending on what you mean by attribute - you just want to extract the #docstrings?

On class (and class instances) and function/method objects, it's available as `<obj>.__doc__`. Same for modules. That covers most types of objects...

If you mean extracting information from type annotations, there's an inspection interface for that, too.

@cazabon I'm talking about docstrings for attributes, which are not accessible at runtime, but are handled by documentation tools like Sphinx and mkdocs. Accessing `attr.__doc__` would access the docstring of the type of `attr`, which is not the same thing as the docstring for `attr` itself.

@davidism

Ah - you mean something like a *data* attribute on a class, like:

class Foo:
"sorta-docstring comment before"
a = 6
"sorta-docstring comment after"
foo = Foo()

Then `foo.a.__doc__` gives you the docstring for the int class/function, where you want to extract a comment that's before or after it instead?

If so, then the AST is the way, but I'm afraid I don't personally know of a PyPI package that already exists to do it.

Sorry.