Skip to content

Commit

Permalink
REF: lru_cache _formatannotation() output
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 20, 2021
1 parent 53bc605 commit 2f07590
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import typing
from contextlib import contextmanager
from copy import copy
from functools import lru_cache, reduce, partial
from functools import lru_cache, reduce, partial, wraps
from itertools import tee, groupby
from types import ModuleType
from typing import ( # noqa: F401
Expand Down Expand Up @@ -1240,12 +1240,25 @@ def _link_inheritance(self):
del self._super_members


def maybe_lru_cache(func):
cached_func = lru_cache()(func)

@wraps(func)
def wrapper(*args):
try:
return cached_func(*args)
except TypeError:
return func(*args)

return wrapper


@maybe_lru_cache
def _formatannotation(annot):
"""
Format typing annotation with better handling of `typing.NewType`,
`typing.Optional`, `nptyping.NDArray` and other types.
# >>> import typing
>>> _formatannotation(NewType('MyType', str))
'MyType'
>>> _formatannotation(Optional[Tuple[Optional[int], None]])
Expand Down

0 comments on commit 2f07590

Please sign in to comment.