Skip to content

Commit

Permalink
Handle UnionType (#221)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernát Gábor <[email protected]>
  • Loading branch information
bryanforbes and gaborbernat authored Apr 12, 2022
1 parent 13ca2b4 commit 4d5867d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.18.0

- Support and require `nptyping>=2`
- Handle `UnionType`

## 1.17.1

Expand Down
2 changes: 2 additions & 0 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901 # t
formatted_args = f"\\[\\[{', '.join(fmt[:-1])}], {fmt[-1]}]"
elif full_name == "typing.Literal":
formatted_args = f"\\[{', '.join(repr(arg) for arg in args)}]"
elif full_name == "types.UnionType":
return " | ".join([format_annotation(arg, config) for arg in args])

if args and not formatted_args:
try:
Expand Down
4 changes: 3 additions & 1 deletion tests/roots/test-dummy/dummy_module_future_annotations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations


def function_with_py310_annotations(self, x: bool, y: int, z: str | None = None) -> str: # noqa: U100
def function_with_py310_annotations(
self, x: bool | None, y: int | str | float, z: str | None = None # noqa: U100
) -> str:
"""
Method docstring.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_sphinx_autodoc_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def set_python_path() -> None:
def maybe_fix_py310(expected_contents: str) -> str:
if PY310_PLUS:
for old, new in [
("*bool** | **None*", '"bool" | "None"'),
("*int** | **str** | **float*", '"int" | "str" | "float"'),
("*str** | **None*", '"Optional"["str"]'),
("(*bool*)", '("bool")'),
("(*int*", '("int"'),
Expand Down Expand Up @@ -704,9 +706,9 @@ def test_sphinx_output_future_annotations(app: SphinxTestApp, status: StringIO)
Method docstring.
Parameters:
* **x** (*bool*) -- foo
* **x** (*bool** | **None*) -- foo
* **y** (*int*) -- bar
* **y** (*int** | **str** | **float*) -- bar
* **z** (*str** | **None*) -- baz
Expand Down

0 comments on commit 4d5867d

Please sign in to comment.