-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Type hints are irregularly documented (+link generation broken) #9813
Comments
I'm hitting this too with this source: from typing import Callable, TYPE_CHECKING, Iterable, TypeVar, Optional
if TYPE_CHECKING:
from .example import Example
#: This is an evaluator
Evaluator = Callable[['Example'], Optional[str]] Which is hit by autodoc from this: .. automodule:: sybil.typing
:members: Evaluator
from typing import Callable, TYPE_CHECKING, Iterable, TypeVar, Optional
if TYPE_CHECKING:
from .example import Example
#: This is an evaluator
Evaluator = Callable[['sybil.example.Example'], Optional[str]] ...but is that a valid type definition? I'm also on Sphinx 4.2.0. |
@gschwaer Did you ever find something like a temporary workaround for this? I am currently trying to migrate an existing codebase to Sphinx and am receiving thousands of warnings of broken links because of this, since the library makes extensive use of the |
@provinzkraut Sorry, I did not. Back when I reported the issue I took a quick look into the Sphinx code to see if I could fix it myself. But the code was very confusing to me, so I gave up and reported it here. For the project I was working on, it was a hassle, but no deal breaker, so we just left it as ugly as it was. If you by chance find a solution yourself (or a better suited framework) please post it here. Thx |
I know there have been changes in this area recently, but even on the last version, none of the types in the parameters are being linked: |
@AA-Turner Could you explain why this was added to "some future version"? That's a bit of a blow honestly since it means autodoc (and therefore Sphinx) is not actually usable for projects that wish to derive their documentation from type hints, and probably won't be for the foreseeable future. |
@provinzkraut the links can be made to work (in the cases where autodoc doesn't resolve them automatically) by coding them explicitly into the |
They are "breaking bugs" because the feature being talked about here does not work correctly. The fact that you can get the desired result in a different way, simply by not using this feature doesn't change that. This issue is specifically about type hints and the automatic documentation of them. Saying "it works if you don't document these things with type hints" doesn't address this issue. The feature is broken in its current state as it does not work as advertised in a way that's usable for a larger code base. Even the workaround you mention isn't really usable, because if you were to do that, you'd end up with an inconsistent docstring format, which not only is confusing and requires extra attention, but can also throw linters of if you configure them to enforce a particular format. |
Hi Janek, This is as I don't have time to work on this, and I don't want to offer false hope of a quick fix. As with everything--constrained resources (especially in voluntary work &c. &c.)! I'm very happy to review PRs though, if you would point me in their direction. Thanks, |
Hey Adam, thanks for the explanation! That's certainly a valid reason.
I have since spent a good amount of time on this effort and realised myself that this will be anything but a quick fix. There are a lot of open issues related to how autodoc deals with type annotations, and most of them boil down to what has been outlined here. The only way to properly solve this is to fundamentally change how autodoc resolves with type hints, and create a consistent resolution. I have had some partial success with an AST-based approach, as did the excellent sphinx-resolve-py-references extension, and I believe this would be the way to go. A merge of this into Sphinx has been discussed in #4961, but has not progressed as of today. I am not sure why that is, but I'd be willing to help move this along if actionable blockers could be identified. Maybe it would be worth tracking this in a separate issue? |
Another case that hasn't been mentioned: When you use |
So I hit this one when documenting the testing module. I think the next thing to do (after fixing the testing module and Windows tests) is to address this issue. The issue I faced is inside a signature and this is not handled by |
@picnixz this is likely the one issue that's affecting most docs in the wild at the moment, together with some types not linking properly (like |
I ran into a similar issue with dataclasses and TYPE_CHECKING, basically my code is: from __future__ import annotations
if TYPE_CHECKING:
from foo.bar import Engine
@dataclass
class Machine:
engine: Engine And I get a I try to workaround this issue by importing differently such that the type hint "string" itself becomes unambiguous: Any progress on the issue is appreciated! |
Describe the bug
Sphinx processes and outputs type hints differently to html depending on
"Type" vs Type
),__future__.annotations
is present, andimport module
from module import name
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:
F
== full path,C
== class-only,L
== link works,N
== no link,n/a
== does not apply (__future__.annotations
is required)Observations:
schema behave differently that unquoted types and some links are broken
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
Check out Test.py which has the include of
__future__.annotations
at the top.Expected behavior
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.
The text was updated successfully, but these errors were encountered: