Skip to content

Commit 4b31b9f

Browse files
committed
Also do non-qualified tuples
1 parent 9111c6d commit 4b31b9f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

scanpydoc/elegant_typehints.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ def _init_vars(app: Sphinx, config: Config):
6767
config.html_static_path.append(str(HERE / "static"))
6868

6969

70+
def _fixup(fmt: str) -> str:
71+
# work around https://github.com/agronholm/sphinx-autodoc-typehints/issues/94
72+
fmt = fmt.replace(":py:class:`typing.Tuple`", ":py:data:`typing.Tuple`")
73+
fmt = fmt.replace(":py:class:`~typing.Tuple`", ":py:data:`~typing.Tuple`")
74+
return fmt
75+
76+
7077
def _format_full(annotation: Type[Any], fully_qualified: bool = False):
7178
if inspect.isclass(annotation) and annotation.__module__ == "builtins":
7279
return _format_orig(annotation, fully_qualified)
7380
annotation_cls = annotation if inspect.isclass(annotation) else type(annotation)
7481
if annotation_cls.__module__ == "typing":
75-
formatted = _format_orig(annotation, fully_qualified)
76-
# work around https://github.com/agronholm/sphinx-autodoc-typehints/issues/94
77-
formatted = formatted.replace(
78-
":py:class:`typing.Tuple`", ":py:data:`typing.Tuple`"
79-
)
80-
return formatted
82+
return _fixup(_format_orig(annotation, fully_qualified))
8183

8284
# Only if this is a real class we override sphinx_autodoc_typehints
8385
if inspect.isclass(annotation) or inspect.isclass(
@@ -88,7 +90,7 @@ def _format_full(annotation: Type[Any], fully_qualified: bool = False):
8890
if override is not None:
8991
return f":py:class:`{'' if fully_qualified else '~'}{override}`"
9092

91-
return _format_orig(annotation, fully_qualified)
93+
return _fixup(_format_orig(annotation, fully_qualified))
9294

9395

9496
def _format_terse(annotation: Type[Any], fully_qualified: bool = False) -> str:

tests/test_elegant_typehints.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ def test_typing_classes(app, annotation, formatter):
118118
elif len(annotation.__args__) == 2 and type(None) in annotation.__args__:
119119
name = "Optional"
120120
assert formatter(annotation, True).startswith(f":py:data:`typing.{name}")
121+
122+
123+
def test_typing_class_nested(app):
124+
assert _format_full(t.Optional[t.Tuple[int, str]]) == (
125+
":py:data:`~typing.Optional`\\["
126+
":py:data:`~typing.Tuple`\\[:py:class:`int`, :py:class:`str`]"
127+
"]"
128+
)

0 commit comments

Comments
 (0)