Description
Describe the bug
When using autodoc
to document type aliases in a module, references to those aliases in the signature of a function cannot be resolved, even when using from __future__ import annotations
and autodoc_type_aliases
.
How to Reproduce
Create module.py
with these contents:
from __future__ import annotations
import pathlib
#: Any type of path
pathlike = str | pathlib.Path
def read_file(path: pathlike) -> bytes:
"""Read a file and return its contents."""
with open(path, "rb") as f:
return f.read()
and index.rst
with these contents:
.. automodule:: module
:members:
:member-order: bysource
and then run Sphinx, enabling autodoc and using autodoc_type_aliases
:
$ python -m sphinx -aE -C -D 'extensions=sphinx.ext.autodoc' -D 'autodoc_type_aliases.pathlike=pathlike' . output
Expected behavior
On the module.read_file(path: pathlike) → bytes
line, pathlike
should be a link to the module.pathlike
type alias, but it is not a link at all.
Running with nitpicky mode shows:
module.py:docstring of module.read_file:1: WARNING: py:class reference target not found: pathlike
This is because autodoc
is generating a py:attr
entry for pathlike
, and Sphinx is trying to resolve a py:class
entry instead.
Your project
See "how to reproduce"
Screenshots
No response
OS
Linux
Python version
3.10.6
Sphinx version
5.1.1
Sphinx extensions
sphinx.ext.autodoc
Extra tools
No response
Additional context
I'm working around this with a hack in my docs/conf.py
:
TYPE_ALIASES = ["pathlike", "filelike"]
def resolve_type_aliases(app, env, node, contnode):
"""Resolve :class: references to our type aliases as :attr: instead."""
if (
node["refdomain"] == "py"
and node["reftype"] == "class"
and node["reftarget"] in TYPE_ALIASES
):
return app.env.get_domain("py").resolve_xref(
env, node["refdoc"], app.builder, "attr", node["reftarget"], node, contnode
)
def setup(app):
app.connect("missing-reference", resolve_type_aliases)