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

Autodoc feature request: Attribute docstring signatures #9025

Closed
drewcassidy opened this issue Mar 21, 2021 · 7 comments
Closed

Autodoc feature request: Attribute docstring signatures #9025

drewcassidy opened this issue Mar 21, 2021 · 7 comments
Labels

Comments

@drewcassidy
Copy link

drewcassidy commented Mar 21, 2021

Is your feature request related to a problem? Please describe.
Right now theres no way for a docstring for an attribute to define its type option, only a type field, which is frustrating when documenting extension modules. I propose the autodoc_docstring_signature feature be extended to include attributes to allow type documentation from within non-python code.

Currently the only workarounds I'm aware of are

  • manually document each attribute
  • use the function signature syntax (e.g. .. autoproperty:: field(type)) which is kinda weird
  • to use the :type: field which makes the documentation a lot messier looking than I would like

Describe the solution you'd like
similar to functions, allow for the first line of an attribute docstring to contain a signature in the form name: type, and have that type automatically added to the :type: option (not field) of the auto documented member.

Describe alternatives you've considered

  • removing the newline from before :type: fields on the first line of a docstring to make them into options instead
  • using a function style signature (e.g. name(self)-> type) (this is probably the easiest to implement by reusing the method signature function)

Additional context

Screen_Shot_2021-03-20_at_7 16 59_PM
Screen_Shot_2021-03-20_at_7 17 07_PM

I'm open to doing a PR for this feature, I just want feedback on what the best way to go about doing it is, and if anyone else wants it.

@drewcassidy drewcassidy added the type:enhancement enhance or introduce a new feature label Mar 21, 2021
@brenthuisman
Copy link

brenthuisman commented Mar 26, 2021

Possibly related:

I have a Pybind project with a class with a property with the following docstring: ":type: int\n\nThe id of the branch.". This generates something like the first example in your image, but I'd like the second.

"the_prop() -> int\n\nThe id of the branch." seems to be picked up, because it disappears from the output, but it's not used to add in the type of the property at all.

When I create some documentation without autodoc, I see that a newline matters. To generate what I see in your example:

.. class:: someclass

    .. attribute:: two_ls_passes

        :type: bool

    .. attribute:: two_ls_passes
        :type: bool

I'm wondering if autodoc adds this newline as well, and if I can remove it somehow.

@tk0miya tk0miya added this to the 4.1.0 milestone Mar 26, 2021
@shimizukawa
Copy link
Member

If sphinx supports pyi files, it would be better than using docstrings to specify types.
i.e. #7630

@drewcassidy
Copy link
Author

Im not thrilled with the idea of using pyi files if only because it means juggling even more files for an extension module

@tk0miya
Copy link
Member

tk0miya commented Apr 3, 2021

I agree with @shimizukawa's idea. The .pyi file is now the standard of Python to distribute type information (see PEP 561; https://www.python.org/dev/peps/pep-0561/). It also helps type checkers, IDEs, and other typing tools. There is no reason to add an original docstring format only for documentation to me.

@tk0miya tk0miya removed this from the 4.1.0 milestone Apr 3, 2021
@drewcassidy
Copy link
Author

Is there any further documentation on pyi files? I have no idea where to put them or how to write them

@drewcassidy
Copy link
Author

never mind, i think I figured it out. I agree that this is the best way to go

@tk0miya
Copy link
Member

tk0miya commented Apr 4, 2021

Thank you for your understanding.
We must hurry to implement .pyi file support!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants