Skip to content

Commit b4a55ca

Browse files
committed
Support Union type on its own
Signed-off-by: Bernát Gábor <[email protected]>
1 parent ddac8c8 commit b4a55ca

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ def format_annotation(annotation: Any, config: Config, *, short_literals: bool =
300300
return f"\\{' | '.join(f'``{arg!r}``' for arg in args)}"
301301
formatted_args = f"\\[{', '.join(f'``{arg!r}``' for arg in args)}]"
302302
elif is_bars_union:
303+
if not args:
304+
return f":py:{'class' if sys.version_info >= (3, 14) else 'data'}:`{prefix}typing.Union`"
303305
return " | ".join([format_annotation(arg, config, short_literals=short_literals) for arg in args])
304306

305307
if args and not formatted_args:

tests/test_sphinx_autodoc_typehints.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
244244
r":py:data:`~typing.Tuple`\ \[:py:class:`str`, :py:data:`...<Ellipsis>`]",
245245
id="Tuple-str-Ellipsis",
246246
),
247-
pytest.param(Union, "" if sys.version_info >= (3, 14) else ":py:data:`~typing.Union`", id="Union"),
247+
pytest.param(Union, f":py:{'class' if sys.version_info >= (3, 14) else 'data'}:`~typing.Union`", id="Union"),
248+
pytest.param(
249+
types.UnionType, f":py:{'class' if sys.version_info >= (3, 14) else 'data'}:`~typing.Union`", id="UnionType"
250+
),
248251
pytest.param(
249252
Union[str, bool],
250253
":py:class:`str` | :py:class:`bool`"
@@ -280,6 +283,11 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
280283
else r":py:data:`~typing.Optional`\ \[:py:class:`str`]",
281284
id="Optional-str-None",
282285
),
286+
pytest.param(
287+
type[T] | types.UnionType,
288+
":py:class:`type`\\ \\[:py:class:`~typing.TypeVar`\\ \\(``T``)] | :py:class:`~typing.Union`",
289+
id="typevar union bar uniontype",
290+
),
283291
pytest.param(
284292
Optional[str | bool],
285293
":py:class:`str` | :py:class:`bool` | :py:obj:`None`"

0 commit comments

Comments
 (0)