Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@
from sphinx.ext.autodoc import Options

_LOGGER = logging.getLogger(__name__)
_PYDATA_ANNOTATIONS = {"Any", "AnyStr", "Callable", "ClassVar", "Literal", "NoReturn", "Optional", "Tuple", "Union"}
_PYDATA_ANNOTS_TYPING = {"Any", "AnyStr", "Callable", "ClassVar", "Literal", "NoReturn", "Optional", "Tuple", "Union"}
_PYDATA_ANNOTS_TYPES = {
*("AsyncGeneratorType", "BuiltinFunctionType", "BuiltinMethodType"),
*("CellType", "ClassMethodDescriptorType", "CoroutineType"),
"EllipsisType",
*("FrameType", "FunctionType"),
*("GeneratorType", "GetSetDescriptorType"),
"LambdaType",
*("MemberDescriptorType", "MethodDescriptorType", "MethodType", "MethodWrapperType"),
*("NoneType", "NotImplementedType"),
"WrapperDescriptorType",
}
_PYDATA_ANNOTATIONS = {
*(("typing", n) for n in _PYDATA_ANNOTS_TYPING),
*(("types", n) for n in _PYDATA_ANNOTS_TYPES),
}

# types has a bunch of things like ModuleType where ModuleType.__module__ is
# "builtins" and ModuleType.__name__ is "module", so we have to check for this.
Expand Down Expand Up @@ -219,7 +234,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
full_name = f"{module}.{class_name}" if module != "builtins" else class_name
fully_qualified: bool = getattr(config, "typehints_fully_qualified", False)
prefix = "" if fully_qualified or full_name == class_name else "~"
role = "data" if module == "typing" and class_name in _PYDATA_ANNOTATIONS else "class"
role = "data" if (module, class_name) in _PYDATA_ANNOTATIONS else "class"
args_format = "\\[{}]"
formatted_args: str | None = ""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
pytest.param(str, ":py:class:`str`", id="str"),
pytest.param(int, ":py:class:`int`", id="int"),
pytest.param(StringIO, ":py:class:`~io.StringIO`", id="StringIO"),
pytest.param(FunctionType, ":py:class:`~types.FunctionType`", id="FunctionType"),
pytest.param(FunctionType, ":py:data:`~types.FunctionType`", id="FunctionType"),
pytest.param(ModuleType, ":py:class:`~types.ModuleType`", id="ModuleType"),
pytest.param(type(None), ":py:obj:`None`", id="type None"),
pytest.param(type, ":py:class:`type`", id="type"),
Expand Down Expand Up @@ -414,7 +414,7 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
assert format_annotation(annotation, conf) == expected_result

# Test for the correct role (class vs data) using the official Sphinx inventory
if "typing" in expected_result:
if any(modname in expected_result for modname in ("typing", "types")):
m = re.match(r"^:py:(?P<role>class|data|func):`~(?P<name>[^`]+)`", result)
assert m, "No match"
name = m.group("name")
Expand Down
Loading