File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change 1919import typing
2020from contextlib import contextmanager
2121from copy import copy
22- from functools import lru_cache , reduce , partial
22+ from functools import lru_cache , reduce , partial , wraps
2323from itertools import tee , groupby
2424from types import ModuleType
2525from 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
12431257def _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]])
You can’t perform that action at this time.
0 commit comments