Skip to content

Commit 2f07590

Browse files
committed
REF: lru_cache _formatannotation() output
1 parent 53bc605 commit 2f07590

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pdoc/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import typing
2020
from contextlib import contextmanager
2121
from copy import copy
22-
from functools import lru_cache, reduce, partial
22+
from functools import lru_cache, reduce, partial, wraps
2323
from itertools import tee, groupby
2424
from types import ModuleType
2525
from typing import ( # noqa: F401
@@ -1240,12 +1240,25 @@ def _link_inheritance(self):
12401240
del self._super_members
12411241

12421242

1243+
def maybe_lru_cache(func):
1244+
cached_func = lru_cache()(func)
1245+
1246+
@wraps(func)
1247+
def wrapper(*args):
1248+
try:
1249+
return cached_func(*args)
1250+
except TypeError:
1251+
return func(*args)
1252+
1253+
return wrapper
1254+
1255+
1256+
@maybe_lru_cache
12431257
def _formatannotation(annot):
12441258
"""
12451259
Format typing annotation with better handling of `typing.NewType`,
12461260
`typing.Optional`, `nptyping.NDArray` and other types.
12471261
1248-
# >>> import typing
12491262
>>> _formatannotation(NewType('MyType', str))
12501263
'MyType'
12511264
>>> _formatannotation(Optional[Tuple[Optional[int], None]])

0 commit comments

Comments
 (0)