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: Use type stub files when documenting native modules #13253

Merged
merged 10 commits into from
Jan 21, 2025

Conversation

AA-Turner
Copy link
Member

Purpose

This builds on work originally proposed by @alliefitter in #4824. Autodoc now attempts to load a .pyi stub file for any native modules being documented.

Currently this will become the default behaviour, but we could put it behind an environment variable if it might be disruptive (SPHINX_AUTODOC_USE_NATIVE_STUB_FILES?).

cc commenters from original threads: @JosXa, @hoefling, @Vanuan, @cmpute, @brenthuisman, @kdschlosser, @jonnor.

References

A

@AA-Turner AA-Turner added type:enhancement enhance or introduce a new feature extensions:autodoc labels Jan 19, 2025
@AA-Turner AA-Turner added this to the 8.2.0 milestone Jan 19, 2025
@AA-Turner AA-Turner changed the title Refactor import_module autodoc: Use type stub files when documenting native modules Jan 19, 2025
@electric-coder
Copy link

electric-coder commented Jan 19, 2025

This is a ground shaking improvement for the whole ecosystem. I have to ask:

  • Will Sphinx only extract types or also docstrings from the .pyi (because apparently you can also outsource your docstrings to the stub files and that would make for much cleaner .py)?

Edit: @hoefling points out problems with putting docstrings in stub files

@kdschlosser
Copy link

OH YES!!! This would be awesome to have available... FINALLY!!!

Fingers crossed it gets merged.

@kdschlosser
Copy link

as a note. if there is a stub file then the docstrings should be in that file. The purpose for the stub file is not just for typing. It is also to remove code that doesn't need to be apart of the running code.

The only place where I can see this being an issue is with python's built in help() function. I have never tested to see if help() pulls from a stub file if it is available. If it doesn't anmd it only pulls from the source file then all that needs to bed one is moving the types and docstrings into the source file and then help() would function like it should.

that is the only thing I can see as being a problem other than dynamic creation of classes and in that case there would not be a stub file unless one is generated.

@electric-coder
Copy link

electric-coder commented Jan 19, 2025

The only place where I can see this being an issue is with (...)

Last I checked mypy stops type checking your .py internal annotations if you have a stub file for it, see python/mypy#5028 so that would now be the main blocking factor. If you notice the mypy issue is 7 years old and still with no solution in sight.

Likely we'll get several more years of new typing PEPs without proper support for them.

@AA-Turner AA-Turner merged commit fe06909 into sphinx-doc:master Jan 21, 2025
22 checks passed
@AA-Turner AA-Turner deleted the autodoc-importlib branch January 21, 2025 19:37
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

Successfully merging this pull request may close these issues.

Use PEP 484 stub files when documenting native extensions with autodoc
3 participants