Skip to content

Commit a419476

Browse files
authored
Display Union[a, b] as a | b (PEP 604) (#22)
1 parent 5bb927f commit a419476

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

scanpydoc/elegant_typehints/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def x() -> Tuple[int, float]:
5656
from sphinx.ext.autodoc import ClassDocumenter
5757

5858
from .. import _setup_sig, metadata
59+
from .example import example_func
5960

6061

6162
HERE = Path(__file__).parent.resolve()
@@ -82,6 +83,9 @@ def _init_vars(app: Sphinx, config: Config):
8283
config.html_static_path.append(str(HERE / "static"))
8384

8485

86+
example_func.__module__ = "scanpydoc.elegant_typehints" # Make it show up here
87+
88+
8589
@_setup_sig
8690
def setup(app: Sphinx) -> Dict[str, Any]:
8791
"""Patches :mod:`sphinx_autodoc_typehints` for a more elegant display."""
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Dict, Union, Optional
2+
3+
4+
def example_func(a: Optional[str], b: Union[str, int, None] = None) -> Dict[str, int]:
5+
"""Example function
6+
7+
Hover over the paramter and return type annotations to see the long versions.
8+
9+
Args:
10+
a: An example parameter
11+
b: Another, with a default
12+
Returns:
13+
An example return value
14+
"""
15+
pass

scanpydoc/elegant_typehints/formatting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def _format_terse(annotation: Type[Any], fully_qualified: bool = False) -> str:
4949
tilde = "" if fully_qualified else "~"
5050
fmt = partial(_format_terse, fully_qualified=fully_qualified)
5151

52-
# display `Union[A, B]` as `A, B`
52+
# display `Union[A, B]` as `A | B`
5353
if origin is Union:
5454
# Never use the `Optional` keyword in the displayed docs.
55-
# Use the more verbose `, None` instead, like other numerical packages.
56-
return ", ".join(map(fmt, args))
55+
# Use `| None` instead, similar to other large numerical packages.
56+
return " | ".join(map(fmt, args))
5757

5858
# do not show the arguments of Mapping
5959
if origin is cabc.Mapping:

tests/test_elegant_typehints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_qualname_overrides_exception(app, _testmod):
181181

182182
def test_qualname_overrides_recursive(app, _testmod):
183183
assert _format_terse(t.Union[_testmod.Class, str]) == (
184-
r":py:class:`~test.Class`, :py:class:`str`"
184+
r":py:class:`~test.Class` | :py:class:`str`"
185185
)
186186
assert _format_full(t.Union[_testmod.Class, str]) == (
187187
r":py:data:`~typing.Union`\["
@@ -193,7 +193,7 @@ def test_qualname_overrides_recursive(app, _testmod):
193193

194194
def test_fully_qualified(app, _testmod):
195195
assert _format_terse(t.Union[_testmod.Class, str], True) == (
196-
r":py:class:`test.Class`, :py:class:`str`"
196+
r":py:class:`test.Class` | :py:class:`str`"
197197
)
198198
assert _format_full(t.Union[_testmod.Class, str], True) == (
199199
r":py:data:`typing.Union`\[" r":py:class:`test.Class`, " r":py:class:`str`" r"]"

0 commit comments

Comments
 (0)