Skip to content

Commit 4d5867d

Browse files
Handle UnionType (#221)
Co-authored-by: Bernát Gábor <[email protected]>
1 parent 13ca2b4 commit 4d5867d

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.18.0
44

55
- Support and require `nptyping>=2`
6+
- Handle `UnionType`
67

78
## 1.17.1
89

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901 # t
175175
formatted_args = f"\\[\\[{', '.join(fmt[:-1])}], {fmt[-1]}]"
176176
elif full_name == "typing.Literal":
177177
formatted_args = f"\\[{', '.join(repr(arg) for arg in args)}]"
178+
elif full_name == "types.UnionType":
179+
return " | ".join([format_annotation(arg, config) for arg in args])
178180

179181
if args and not formatted_args:
180182
try:

tests/roots/test-dummy/dummy_module_future_annotations.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

33

4-
def function_with_py310_annotations(self, x: bool, y: int, z: str | None = None) -> str: # noqa: U100
4+
def function_with_py310_annotations(
5+
self, x: bool | None, y: int | str | float, z: str | None = None # noqa: U100
6+
) -> str:
57
"""
68
Method docstring.
79

tests/test_sphinx_autodoc_typehints.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def set_python_path() -> None:
370370
def maybe_fix_py310(expected_contents: str) -> str:
371371
if PY310_PLUS:
372372
for old, new in [
373+
("*bool** | **None*", '"bool" | "None"'),
374+
("*int** | **str** | **float*", '"int" | "str" | "float"'),
373375
("*str** | **None*", '"Optional"["str"]'),
374376
("(*bool*)", '("bool")'),
375377
("(*int*", '("int"'),
@@ -704,9 +706,9 @@ def test_sphinx_output_future_annotations(app: SphinxTestApp, status: StringIO)
704706
Method docstring.
705707
706708
Parameters:
707-
* **x** (*bool*) -- foo
709+
* **x** (*bool** | **None*) -- foo
708710
709-
* **y** (*int*) -- bar
711+
* **y** (*int** | **str** | **float*) -- bar
710712
711713
* **z** (*str** | **None*) -- baz
712714

0 commit comments

Comments
 (0)