diff --git a/pdoc/__init__.py b/pdoc/__init__.py index 50c4e7ba..6917bc66 100644 --- a/pdoc/__init__.py +++ b/pdoc/__init__.py @@ -1294,17 +1294,17 @@ def _formatannotation(annot): >>> _formatannotation(NewType('MyType', str)) 'pdoc.MyType' >>> _formatannotation(Optional[Tuple[Optional[int], None]]) - 'Optional[Tuple[Optional[int], None]]' + 'Tuple[int | None, None] | None' >>> _formatannotation(Optional[Union[int, float, None]]) - 'Optional[int | float]' + 'int | float | None' >>> _formatannotation(Union[int, float]) 'int | float' >>> from typing import Callable >>> _formatannotation(Callable[[Optional[int]], float]) - 'Callable[[Optional[int]], float]' + 'Callable[[int | None], float]' >>> from collections.abc import Callable >>> _formatannotation(Callable[[Optional[int]], float]) - 'Callable[[Optional[int]], float]' + 'Callable[[int | None], float]' """ class force_repr(str): __repr__ = str.__str__ @@ -1322,7 +1322,7 @@ def maybe_replace_reprs(a): t = ' | '.join(inspect.formatannotation(maybe_replace_reprs(x)) for x in union_args) if is_optional: - t = f'Optional[{t}]' + t += ' | None' return force_repr(t) # typing.NewType('T', foo) -> T module = getattr(a, '__module__', '') diff --git a/pdoc/test/__init__.py b/pdoc/test/__init__.py index b33e22bb..8e4e8477 100644 --- a/pdoc/test/__init__.py +++ b/pdoc/test/__init__.py @@ -975,7 +975,7 @@ def prop(self) -> typing.Optional[int]: mod = DUMMY_PDOC_MODULE cls = pdoc.Class('Foobar', mod, Foobar) - self.assertEqual(cls.doc['prop'].type_annotation(), 'Optional[int]') + self.assertEqual(cls.doc['prop'].type_annotation(), 'int\N{NBSP}|\N{NBSP}None') @ignore_warnings def test_Variable_type_annotation_py36plus(self):