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 19
19
import typing
20
20
from contextlib import contextmanager
21
21
from copy import copy
22
- from functools import lru_cache , reduce , partial
22
+ from functools import lru_cache , reduce , partial , wraps
23
23
from itertools import tee , groupby
24
24
from types import ModuleType
25
25
from typing import ( # noqa: F401
@@ -1240,12 +1240,25 @@ def _link_inheritance(self):
1240
1240
del self ._super_members
1241
1241
1242
1242
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
1243
1257
def _formatannotation (annot ):
1244
1258
"""
1245
1259
Format typing annotation with better handling of `typing.NewType`,
1246
1260
`typing.Optional`, `nptyping.NDArray` and other types.
1247
1261
1248
- # >>> import typing
1249
1262
>>> _formatannotation(NewType('MyType', str))
1250
1263
'MyType'
1251
1264
>>> _formatannotation(Optional[Tuple[Optional[int], None]])
You can’t perform that action at this time.
0 commit comments