Skip to content

Commit e205384

Browse files
committed
gh-146553: Use _lazy_load_unwrap() helper instead of local import in get_type_hints()
1 parent 4e9afee commit e205384

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Lib/typing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,17 @@ def _lazy_load_getattr_static():
19731973
_cleanups.append(_lazy_load_getattr_static.cache_clear)
19741974

19751975

1976+
@functools.cache
1977+
def _lazy_load_unwrap():
1978+
# Import unwrap lazily so as not to slow down the import of typing.py
1979+
# Cache the result so we don't slow down get_type_hints() unnecessarily
1980+
from inspect import unwrap
1981+
return unwrap
1982+
1983+
1984+
_cleanups.append(_lazy_load_unwrap.cache_clear)
1985+
1986+
19761987
def _pickle_psargs(psargs):
19771988
return ParamSpecArgs, (psargs.__origin__,)
19781989

@@ -2485,8 +2496,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
24852496
globalns = obj.__dict__
24862497
else:
24872498
# Find globalns for the unwrapped object.
2488-
import inspect
2489-
nsobj = inspect.unwrap(obj)
2499+
nsobj = _lazy_load_unwrap()(obj)
24902500
globalns = getattr(nsobj, '__globals__', {})
24912501
if localns is None:
24922502
localns = globalns

0 commit comments

Comments
 (0)