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

Support PEP 484 type hinting to populate docstrings #25

Open
jonathansick opened this issue Apr 22, 2018 · 4 comments
Open

Support PEP 484 type hinting to populate docstrings #25

jonathansick opened this issue Apr 22, 2018 · 4 comments

Comments

@jonathansick
Copy link
Member

There are two Sphinx extensions that support PEP 484 type hints for functions parameters:

Although I won't confirm it as the optimal choice, https://github.com/agronholm/sphinx-autodoc-typehints seems to be a more active project.

Based on my reading, it seems that these projects are already compatible with tools that parse docstrings. The key is to add the sphinx_autodoc_typehints extension after autodoc, but before a parser like numpydoc or Napoleon.

extensions = [
    'sphinx.ext.autodoc',
    'sphinx_autodoc_typehints',
    'numpydoc'
]

I haven't yet confirmed that numpydoc works, though it should given that Napoleon does work.

@kennywlo
Copy link

kennywlo commented Nov 28, 2018

Hi @jonathansick, has there been any update on this issue? I would like to use type annotations in my DAX API code.

@jonathansick
Copy link
Member Author

I haven't done the work yet on the documentation production side, but there's nothing stopping you from using type annotations in DAX code.

@jonathansick
Copy link
Member Author

@jonathansick
Copy link
Member Author

jonathansick commented Jan 8, 2020

I had a chance to test sphinx-autodoc-typehints with the Documenteer documentation now that we're using type annotations here. Overall, type annotations aren't yet a full replacement for documented types in docstrings. The main issues are:

Since both sphinx-autodoc-typehints and numpydoc both manipulate docstrings, it's possible that the return type issue could be resolved with more sophisticated post-processing of the docstring.

The following are screenshots of the output for different types of APIs with different versions of the build chain. I looked at numpydoc with sphinx-autodoc-typehints, as well as using Napoleon with sphinx-autodoc-typehints. Numpydoc and Napoleon present parameter slightly differently (definition list versus list). There's no clear advantage to using Napoleon from what I can see here.

Typed function

numpydoc with sphinx-autodoc-typehints. Notice how the return type is on a separate line from the return description.

typed-func-numpydoc-type

numpydoc without sphinx-autodoc-typehints.

typed-func-numpydoc

napoleon with sphinx-autodoc-typehints.

typed-func-napoleon

Data class

numpydoc with sphinx-autodoc-typehints.

dataclass-numpydoc-type

napoleon with sphinx-autodoc-typehints.

dataclass-napoleon-type

Function without type annotations

numpydoc with sphinx-autodoc-typehints.

regular-func-numpydoc-type

numpydoc without sphinx-autodoc-typehints.

regular-func-numpydoc

napoleon with sphinx-autodoc-typehints

regular-func-napoleon-type

napoleon without sphinx-autodoc-typehints

regular-func-napoleon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants