Skip to content

Improve autodata docstring detection #6495

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

Open
pakal opened this issue Jun 17, 2019 · 13 comments
Open

Improve autodata docstring detection #6495

pakal opened this issue Jun 17, 2019 · 13 comments
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature

Comments

@pakal
Copy link

pakal commented Jun 17, 2019

Currently, if a variable (eg. a data dict MY_VARIABLE) is imported from a submodule to a parent module, and that an "autodata" pointing to the parent module's MY_VARIABLE is added to documentation, the initial MY_VARIABLE's docstrings (eg. "#: stuffs" is lost. It doesn't seem possible to add this docstring near the "from submodule import MY_VARIABLE).

Autodata "annotation" can be used to workaround this, but in this case the repr() of the variable (that I wanted to be in docs) doesn't appear anymore.

It would be nice for sphinx to follow imports and find the docstring of imported variables, or that it uses a force-injected "doc" one would put in such variables (I tried, it didn't work), or that there is a way to force a display of the repr() of a variable.

@pakal pakal added the type:enhancement enhance or introduce a new feature label Jun 17, 2019
@pakal
Copy link
Author

pakal commented Jun 17, 2019

It might be related to the troubles of #857 btw

@tk0miya
Copy link
Member

tk0miya commented Jun 18, 2019

Reasonable. But it is hard to get the original location of the variable.

@pakal
Copy link
Author

pakal commented Jun 19, 2019

Yes I think the easiest would be to rely on :annotation:, but offer a parameter to "repr()" the variable anyway (useful to autodocument default settings and the likes), wouldn't it ?

@tk0miya
Copy link
Member

tk0miya commented Jun 20, 2019

I think most difficult thing is obtaining comment for the variable. How to rendering is not problem in this case. What do you mean "easy"?

@pakal
Copy link
Author

pakal commented Jun 20, 2019

I mean the ":annotation:" parameter in RST sources seems good enough, imho, to provide documentation for the data when the original docstring can't be found directly in python source.
Actually, all we need is a way to force a repr() even when an ":annotation:" is present, and that'd be good enough for everyone I think.

@tk0miya
Copy link
Member

tk0miya commented Jun 22, 2019

It's not good idea. The :annotation: parameter is only used to give right-hand side annotations from out side. For example, it is used for type of return values of functions (ex. -> int), assigned value (ex. = 1) and so on. It means :annotation: parameter is a part of signature, not a document body (in other word, content). In addition, it was introduced for internal use. So it is still undocumented option.

If you can write autodata directive manually, you can give contents for the directive manually.

.. autodoc:: modname.CONST

   document for CONST

You don't need to use any workaround for this.

@pakal
Copy link
Author

pakal commented Jun 22, 2019

Alright I was mislead by the sphinx docs mentioning "autodata" : http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute

Maybe the doc should be updated to reflect what you just wrote? But I'm still a bit confused about the real usecases for :annotation:, so I wouldn't know how to turn it exactly. Anyway putting a document body lets the repr() visible in sphinx docs, so it's perfect for my current usecase.

@tk0miya
Copy link
Member

tk0miya commented Jun 29, 2019

Ah, sorry. My last comment was wrong.

In addition, it was introduced for internal use. So it is still undocumented option.

:annotation: option has been described in our document as you pointed. So it is public option. I confused with other options.

And I pushed a PR #6530 to fix the document. I hope this helps you. I guess no :annotation: option will resolve your case. Could you check it please?

@pakal
Copy link
Author

pakal commented Jun 29, 2019

Looks good to me :)

tk0miya added a commit that referenced this issue Jun 29, 2019
doc: Update document for :annotation: option (refs: #6495)
@tk0miya
Copy link
Member

tk0miya commented Jun 29, 2019

Thank you for reviewing!

@kconnour
Copy link

I'd just add one thing that I haven't seen discussed (here or elsewhere): if importing a variable that's type hinted, the type's docstring is what Sphinx displays (not the repr()). Example from package pkg:

constants.py:

foo: float = 12.34
""" My docstring for foo """

__init__.py:

from .constants import *

Then in my documentation if I try to use
.. autodata:: pkg.foo
it displays: "Convert a string or number to a floating point number, if possible."

but if I use
.. autodata:: pkg.constants.foo
it displays what I'd expect: "My docstring for foo "

I presume this is due to the difficulty finding where the variable is actually located. In any case I figured it was closely enough related to the original issue to mention here.

@vepain
Copy link

vepain commented Mar 3, 2022

I agree with @kconnour: using __init__.py import to hide too deep submodules paths creates a docstring issue.

Even if you explicitly import your constants in __init__.py:

# pkg/__init__.py

from pkg.constants import foo

And even if you use alternative docstring comments:

#: My docstring for foo
foo: float = 12.34

Is there a way to extract correctly the docstring, or tell to sphinx where exactly is the constant (with hiding the real path)?

Thank you for your help.

EDIT:
One idea would be to provide an actual link to the constants, so that autodata can extract the correct docstring; and tell autodata (with an option, such as :modpath:) where the constant would be imported from.

In the previous example, this would give:

.. autodata:: pkg.constants::foo
    :modpath: pkg.foo

However, this is quite ugly.

asmeurer added a commit to asmeurer/array-api that referenced this issue Jul 15, 2023
Sphinx cannot find the docstring corresponding to a constant unless you point
it to the exact file where it is, since it doesn't actually live on the object
itself. See sphinx-doc/sphinx#6495

Fixes data-apis#601
@smarie
Copy link

smarie commented Apr 7, 2025

In the previous example, this would give:

.. autodata:: pkg.constants::foo
:modpath: pkg.foo

However, this is quite ugly.

Also it would not work if this is done through autosummary and not autodata, see #12020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

5 participants