-
-
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
Intersphinx does not create references to external project when using import as alias #10151
Comments
Was a solution ever found for this issue? |
I didn't get any answer to this issue. So still open?! |
We might have to implement it ourselves via a PR. This looks like the code for the extension: https://github.com/sphinx-doc/sphinx/blob/5.x/sphinx/ext/intersphinx.py |
I had this problem a while ago, which was why I subscribed to this thread. I found using type hints solves this problem. I don't know if it's worth it to you to switch, but... Also with proper type hints you can use the Types in docstringNone of these are "clickable" except def func_2(x, y):
"""
This is the docstring of func_2.
Parameters
----------
x: np.ndarray
The first argument.
y: np.ndarray
The second argument.
Returns
-------
List[float]
The return value.
"""
pass Types as type hintsAll of these are "clickable". def func(x: np.ndarray, y: np.ndarray) -> List[float]:
"""
This is the docstring of func.
Parameters
----------
x
The first argument.
y
The second argument.
Returns
-------
The return value.
"""
pass Conf.pyintersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable/", None),
}
autodoc_typehints = "both"
# autodoc_typehints_format = "short" # This is handy too You can optionally add the def autodoc_process_signature(app, what, name, obj, options, signature, return_annotation):
# Do something
return signature, return_annotation
def setup(app):
app.connect("autodoc-process-signature", autodoc_process_signature) |
OK I think I see the difference now. When you use a docstring generator as you have it in some python IDEs, it typically generates the docstring with the type included as it is defined in the docstring conventions: Numpy Docstring Format. def func(self, val: np.ndarray) -> List[float]:
"""docstring of func
Args:
val (np.ndarray): Input argument
Returns:
List[float]: return value
"""
return --> When you delete all typehints in the docstring, it works. def func(self, val: np.ndarray) -> List[float]:
"""docstring of func
Args:
val: Input argument
Returns:
return value
"""
return Does that mean the link to the numpy doc is overwritten? |
@photoniker how does this function render? def func(self, val: np.ndarray) -> List[float]:
"""docstring of func
Args:
val (int): Input argument
Returns:
str: return value
"""
return |
That's a bummer. It appears that the docstring types supersede the function's actual annotations. I would've hoped for the opposite. There's clearly an issue (as you reported) with how Napoleon processes types from the docstring. The workaround I've used is to simply remove types from the docstring and use type hints. |
Removing the type hints in all my docstrings is quite a bit of work ;-) |
Here's another workaround... You could attach to |
Do you have an example for that? I'd like to ensure that |
Below is an example with OPs function. The same trick can be applied to Here's the full example project. foo2-example.zip BeforeNotice def func(self, val: np.ndarray) -> List[float]:
"""docstring of func
Args:
val (np.ndarray): Input argument
Returns:
List[float]: return value
"""
pass AfterNotice #conf.py
def autodoc_process_docstring(app, what, name, obj, options, lines):
for i in range(len(lines)):
lines[i] = lines[i].replace("np.", "numpy.")
lines[i] = lines[i].replace("List[", "~typing.List[")
def setup(app):
app.connect("autodoc-process-docstring", autodoc_process_docstring) Shorten linksIf you add |
@mhostetter Thank you! Is there an easy way to do something similar in the doc string and parameter descriptions as well? Like making this:
Equivalent to this:
Or does your solution work on the entire doc string? If it works on the entire doc string, how can I only target the parameters? |
Yes, the entire docstring. def func(self, val: np.ndarray) -> List[float]:
"""docstring of func
Here is a detailed docstring. You know :func:`F.interpolate` is a cool function.
Args:
val (np.ndarray): Input argument
Returns:
List[float]: return value
"""
pass # conf.py
intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable/", None),
"torch": ("https://pytorch.org/docs/master/", None),
}
def autodoc_process_docstring(app, what, name, obj, options, lines):
print(name)
print("before:", lines)
for i in range(len(lines)):
lines[i] = lines[i].replace("np.", "numpy.")
# lines[i] = lines[i].replace("np.", "~numpy.") # For shorter links
lines[i] = lines[i].replace("F.", "torch.nn.functional.")
lines[i] = lines[i].replace("List[", "~typing.List[")
print("after:", lines)
def setup(app):
app.connect("autodoc-process-docstring", autodoc_process_docstring) If you print the
And it renders as below, with everything hyperlinked. |
@mhostetter Ah, okay. Thanks for the help!
My code uses doc strings like the one above, so I think that I could do something like this then (including increasing the specificity by targeting values inside / outside brackets, potentially with regex):
|
You can't filter on
You instead would want something like this: for i in range(len(lines)):
if not (lines[i].startswith(":type") or lines[i].startswith(":rtype")):
continue
# Do stuff |
@mhostetter You don't need to use For example:
|
I've discovered a new issue. When manually adding Is there anyway to fix this issue? Edit:
This is an example of HTML code generated by Sphinx without explicitly defining the types:
And this is the same HTML, except for the bool variable being explicitly wrapped in
The |
Would have been nice if my PR had been merged two years ago, then we wouldn’t need workarounds: #9039 |
Hello, Not sure if my problem has the same root as yours, so before opening a duplicate issue I wanted to check. In my project, I am using the traitlets library and basically aliasing their types in my package. In their code, they do then when I compile my docs I see warnings as
Is this the same problem? Sphinx not able to "intersphinx" aliased modules? |
Describe the bug
I'm creating a code docu using the docstring included in the code. In the conf.py file the intersphinx extension is included with mapping dicts to numpy, scipy, matplotlib, ...
E. g. When I import numpy as np or from typing import List, the external references do not exists in the build html docu as expected. It only works for standard python types.
![image](https://user-images.githubusercontent.com/17592823/151758925-7ec7c1d7-7a23-4296-b18e-4eb9278d8c5f.png)
I could not find anything in the Sphinx docu about this. Is this a bug or how must Sphinx be configured here?
How to Reproduce
Expected behavior
When I use absolute import like import numpy, import typing, the external docu is references in the html docu.
![image](https://user-images.githubusercontent.com/17592823/151759356-c9f6ef6b-0bb9-4f2e-80bc-db7d3efa49ca.png)
I would expected that the external docu references are created also for the import aliases.
Your project
--
Screenshots
No response
OS
Win
Python version
3.8
Sphinx version
1.44
Sphinx extensions
Extra tools
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: