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

stubgen should also include docstrings #11965

Closed
robertaistleitner opened this issue Jan 10, 2022 · 5 comments · Fixed by #13284
Closed

stubgen should also include docstrings #11965

robertaistleitner opened this issue Jan 10, 2022 · 5 comments · Fixed by #13284

Comments

@robertaistleitner
Copy link

Feature

When generating *.pyi files, docstrings describing the contained classes, methods and members are not included in the file.

Pitch

In my opinion it would be helpful to have them and other tools for pyi generation do exactly this.

@sobolevn
Copy link
Member

sobolevn commented Jan 13, 2022

I think that it should be an option 🤔
Because, for example in https://github.com/python/typeshed/ we don't need docstrings.

I can imagine many users do not want that as well. But, as a configurable feature - why not?
Somethink like --include-docstrings

Are you interested in working on this? 🙂

@tttapa
Copy link

tttapa commented Jan 14, 2022

For what it's worth, sizmailov/pybind11-stubgen already supports extracting docstrings from C modules and including them in the stub files.

I think this would be most useful for external C or C++ modules, because some IDEs don't look for signatures or docstrings in compiled modules.
Using stubgen at least fixes autocomplete for compiled modules, but if the stub files don't include docstrings, IDEs might not show any documentation when hovering over the function/class/variable in the editor (unlike with plain Python modules).

For example, Pylance in VSCode doesn't show documentation for functions in compiled modules, even with stub files generated by stubgen.

@nschloe
Copy link

nschloe commented Mar 9, 2022

Subscribing and +1 on --include-docstrings. (My use case: Nuitka-compiled modules.)

ghost pushed a commit to nv-morpheus/Morpheus that referenced this issue May 13, 2022
This MR allows python classes created in C++ to show up in IDEs and work with linters. It works by using pybind11-stubgen to create .pyi files that are then included in the package. The result is this:

![image](https://user-images.githubusercontent.com/42954918/167733517-86305e60-2ed4-4a4b-96e8-45e67617c569.png)

Only downside is the pybind11-stubgen isnt too actively maintained (but its also not complex). And the option for generating the output location is limited. So in our output we get:

```
common/
   __init__.pyi
cudf_helpers/
   __init__.pyi
messages/
   __init__.pyi
stages/
   __init__.pyi
common.so
cudf_helpers.so
messages.so
stages.so
```
It would be better to be organized like the following:
```
common.pyi
common.so
cudf_helpers.pyi
cudf_helpers.so
messages.pyi
messages.so
stages.pyi
stages.so
```

The only alternative is to use mypy. However, they dont support docstrings according to this issue: python/mypy#11965

Closes #99

Authors:
  - Michael Demoret (https://github.com/mdemoret-nv)

Approvers:
  - David Gardner (https://github.com/dagardner-nv)
  - Devin Robison (https://github.com/drobison00)
  - Bartley Richardson (https://github.com/BartleyR)

URL: #100
@moi90
Copy link

moi90 commented Jul 14, 2022

Starting point would be: https://github.com/python/mypy/blob/master/mypy/stubgen.py

chylek added a commit to chylek/mypy that referenced this issue Jul 29, 2022
Add a --include-docstrings flag to stubgen
This was suggested in python#11965.
When using this flag, the .pyi files will include
docstrings for Python classes and functions and
for C extension functions.
ilevkivskyi pushed a commit that referenced this issue Aug 13, 2023
### Description

Closes #11965.

Add a --include-docstrings flag to stubgen. This was suggested in #11965
along with a use case.
When using this flag, the .pyi files will include docstrings for Python
classes and functions and for C extension functions.
The flag is optional and does not change the default stubgen behaviour.
When using the flag, the resulting function stubs that contain docstring
will no longer be one-liners, but functions without a docstring still
retain the default one-liner style.

Example input:
```python
class A:
    """class docstring"""
    def func():
        """func docstring"""
        ...
    def nodoc():
        ...
```
output:
```python
class A:
    """class docstring"""
    def func() -> None:
        """func docstring"""
        ...
    def nodoc() -> None: ...
```
## Test Plan

Tests `testIncludeDocstrings` and `testIgnoreDocstrings` were added to
`test-data/unit/stubgen.test` to ensure the code works as intended. All
other tests passed as well.

C extension docstrings are tested using an updated bash script
`misc/test_stubgenc.sh` with test data in
`test-data/pybind11_mypy_demo/stubgen-include-docs` in same fashion as
in an already existing test.

---------

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
@robertaistleitner
Copy link
Author

Thanks for making this happen @chylek and @ilevkivskyi! 🎉

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

Successfully merging a pull request may close this issue.

6 participants