Skip to content

Commit 27dd8e7

Browse files
authored
Set role to data when creating an Optional from | None (#285)
1 parent 2747ae2 commit 27dd8e7

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,13 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901 # t
168168
elif full_name in ("typing.Union", "types.UnionType") and type(None) in args:
169169
if len(args) == 2:
170170
full_name = "typing.Optional"
171+
role = "data"
171172
args = tuple(x for x in args if x is not type(None)) # noqa: E721
172173
else:
173174
simplify_optional_unions: bool = getattr(config, "simplify_optional_unions", True)
174175
if not simplify_optional_unions:
175176
full_name = "typing.Optional"
177+
role = "data"
176178
args_format = f"\\[:py:data:`{prefix}typing.Union`\\[{{}}]]"
177179
args = tuple(x for x in args if x is not type(None)) # noqa: E721
178180
elif full_name == "typing.Callable" and args and args[0] is not ...:

tests/test_sphinx_autodoc_typehints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
203203
),
204204
),
205205
(Optional[str], ":py:data:`~typing.Optional`\\[:py:class:`str`]"),
206+
(Union[str, None], ":py:data:`~typing.Optional`\\[:py:class:`str`]"),
206207
(
207208
Optional[Union[str, bool]],
208209
":py:data:`~typing.Union`\\[:py:class:`str`, " ":py:class:`bool`, :py:obj:`None`]",

0 commit comments

Comments
 (0)