sphinx-doc / sphinx-doc/sphinx
Type hints are irregularly documented (+link generation broken)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
Sphinx processes and outputs type hints differently to html depending on
- the use of quotes on type hints (
"Type" vs Type), - if
__future__.annotationsis present, and - on the type of import
- plain import:
import module - from import:
from module import name - type checking import:
from typing import TYPE_CHECKING if TYPE_CHECKING: from module import name
- plain import:
In some cases the full canonical path including all levels of module path is
shown (like: module.Name), some others show only the class name
(like: Name). Some of the combinations also break links.
Here is what I found:
| __future__.annotations | no __future__.annotations |
| unquoted | quoted | unquoted | quoted |
--------------+-----------------+-------------+-----------+--------------+------------|
plain import | module function | F, L | F, L | F, L | F, L |
| class attribute | F, L | F, L | F, L | F, L |
| class property | F, L | F, L | F, L | F, L |
| class method | F, L | F, L | F, L | F, L |
from import | module function | F, L | C, L | F, L | F, L |
| class attribute | F, L | C, N | F, L | F, L |
| class property | F, L | C, N | F, L | F, L |
| class method | F, L | C, L | F, L | F, L |
type checking | module function | C, L | C, L | n/a | C, L |
| class attribute | C, N | C, N | n/a | C, N |
| class property | C, N | C, N | n/a | C, N |
| class method | C, L | C, L | n/a | C, L |
F == full path,
C == class-only,
L == link works,
N == no link,
n/a == does not apply (__future__.annotations is required)
Observations:
- Plain imports always generate canonical paths and working links.
- with future annotations, quoted types that were imported with the from-import
schema behave differently that unquoted types and some links are broken - the same behavior occurs if type checking imports are used no matter if quoted
or not
Note: Showing classes-only is really neat (especially for small projects) as it is more concise. But it would be best if this could be configured (enabled/disabled).
How to Reproduce
$ git clone https://github.com/gschwaer/sphinx_test/
$ cd some_project
$ git checkout autodoc_irregular_type_resolving
$ python3 -m venv .venv
$ pip install -r requirements.txt
$ make
$ # open _build/html/index.html
Check out Test.py which has the include of __future__.annotations at the top.
Expected behavior
- The generated documentation should not depend on the way a type is imported.
- Types should be linked correctly.
- Showing types without the module prefix makes a lot of sense in some situations and in others, it doesn't. This should be configurable.
Your project
https://github.com/gschwaer/sphinx_test/tree/autodoc_irregular_type_resolving
Screenshots
Example showing the case where types are shown as full canonical path and links work (with future annotations, from-import schema, type hints not quoted):

Example showing the case where types are shown as class-only and some links are not generated (the generated links work) (with future annotations, from-import schema, quoted type hints):

OS
Linux
Python version
3.8.10
Sphinx version
4.2.0
Sphinx extensions
sphinx.ext.autodoc
Extra tools
No response
Additional context
I am using RTD theme because alabaster is really not helping to showcase this issue. The effects (class-only vs full path and missing links) are the same for both themes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue using the linked sphinx_test repository and its autodoc_irregular_type_resolving branch; install requirements and run make. Start with Test.py and inspect the generated _build/html/index.html across the import and annotation combinations described. Done means type rendering is consistent, links work, and the module-prefix behavior is configurable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100