Skip to content

Commit 72a0385

Browse files
committed
Fix roles for types module
1 parent 3af8fb6 commit 72a0385

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@
3636
from sphinx.ext.autodoc import Options
3737

3838
_LOGGER = logging.getLogger(__name__)
39-
_PYDATA_ANNOTATIONS = {"Any", "AnyStr", "Callable", "ClassVar", "Literal", "NoReturn", "Optional", "Tuple", "Union"}
39+
_PYDATA_ANNOTS_TYPING = {"Any", "AnyStr", "Callable", "ClassVar", "Literal", "NoReturn", "Optional", "Tuple", "Union"}
40+
_PYDATA_ANNOTS_TYPES = {
41+
*("AsyncGeneratorType", "BuiltinFunctionType", "BuiltinMethodType"),
42+
*("CellType", "ClassMethodDescriptorType", "CoroutineType"),
43+
"EllipsisType",
44+
*("FrameType", "FunctionType"),
45+
*("GeneratorType", "GetSetDescriptorType"),
46+
"LambdaType",
47+
*("MemberDescriptorType", "MethodDescriptorType", "MethodType", "MethodWrapperType"),
48+
*("NoneType", "NotImplementedType"),
49+
"WrapperDescriptorType",
50+
}
51+
_PYDATA_ANNOTATIONS = {
52+
*(("typing", n) for n in _PYDATA_ANNOTS_TYPING),
53+
*(("types", n) for n in _PYDATA_ANNOTS_TYPES),
54+
}
4055

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

tests/test_sphinx_autodoc_typehints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
168168
pytest.param(str, ":py:class:`str`", id="str"),
169169
pytest.param(int, ":py:class:`int`", id="int"),
170170
pytest.param(StringIO, ":py:class:`~io.StringIO`", id="StringIO"),
171-
pytest.param(FunctionType, ":py:class:`~types.FunctionType`", id="FunctionType"),
171+
pytest.param(FunctionType, ":py:data:`~types.FunctionType`", id="FunctionType"),
172172
pytest.param(ModuleType, ":py:class:`~types.ModuleType`", id="ModuleType"),
173173
pytest.param(type(None), ":py:obj:`None`", id="type None"),
174174
pytest.param(type, ":py:class:`type`", id="type"),
@@ -257,7 +257,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
257257
id="Union-str-Any",
258258
),
259259
pytest.param(
260-
Optional[str], # noqa: UP007
260+
Optional[str],
261261
r":py:data:`~typing.Optional`\ \[:py:class:`str`]",
262262
id="Optional-str",
263263
),
@@ -267,7 +267,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
267267
id="Optional-str-None",
268268
),
269269
pytest.param(
270-
Optional[str | bool], # noqa: UP007
270+
Optional[str | bool],
271271
r":py:data:`~typing.Union`\ \[:py:class:`str`, :py:class:`bool`, :py:obj:`None`]",
272272
id="Optional-Union-str-bool",
273273
),
@@ -414,7 +414,7 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
414414
assert format_annotation(annotation, conf) == expected_result
415415

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

0 commit comments

Comments
 (0)