-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add option to include docstrings with stubgen #13284
Conversation
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I gave it some more thought and I will have to fix another mistake - Thanks @sobolevn for you comments, I'll fix it. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
can you also consider adding variable docstring? a_variable: int = 0
"""this is the docstring for a_variable""" class Fields_Obj:
DefaultValue=None
"""Get/set the default value of the data field""" https://stackoverflow.com/questions/6060813/how-to-document-fields-and-properties-in-python |
Is there any ETA for this feature to be merged? |
@chylek Could you please resolve merge conflicts? This adds a frequently requested feature, so I would merge this (unless there are objections from other maintainers). |
This comment has been minimized.
This comment has been minimized.
mypy/stubgenc.py
Outdated
function=name, | ||
args=", ".join(args), | ||
ret=strip_or_import(signature.ret_type, module, known_modules, imports), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this part completely repeats the other branch? If yes, then please use
if foo:
bar
baz
instead of
if foo:
bar
baz
else:
baz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be more precise, in this case you need:
bar
if foo:
baz
since you always need a type, and docstring should come after (only if the flag is enabled)
@chylek While a docs issue unrelated to your PR is being fixed, could you please fix a style issue I found? |
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Thank you for merging my pull request and for the guidance and feedback. |
This addresses several regressions identified in #16486 The primary regression from #15770 is that pybind11 properties with docstrings were erroneously assigned `typeshed. Incomplete`. The reason for the regression is that as of the introduction of the `--include-docstring` feature (#13284, not my PR, ftr), `./misc/test-stubgenc.sh` began always reporting success. That has been fixed. It was also pointed out that `--include-docstring` does not work for C-extensions. This was not actually a regression as it turns out this feature was never implemented for C-extensions (though the tests suggested it had been), but luckily my efforts to unify the pure-python and C-extension code-paths made fixing this super easy (barely an inconvenience)! So that is working now. I added back the extended list of `typing` objects that generate implicit imports for the inspection-based stub generator. I originally removed these because I encountered an issue generating stubs for `PySide2` (and another internal library) where there was an object with the same name as one of the `typing` objects and the auto-import created broken stubs. I felt somewhat justified in this decision as there was a straightforward solution -- e.g. use `list` or `typing.List` instead of `List`. That said, I recognize that the problem that I encountered is more niche than the general desire to add import statements for typing objects, so I've changed the behavior back for now, with the intention to eventually add a flag to control this behavior.
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:
output:
Test Plan
Tests
testIncludeDocstrings
andtestIgnoreDocstrings
were added totest-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 intest-data/pybind11_mypy_demo/stubgen-include-docs
in same fashion as in an already existing test.