Skip to content

Commit 27c948d

Browse files
committed
gh-146553: Replace _lazy_load_* helpers with _LazyInspect for lazy inspect import
1 parent e205384 commit 27c948d

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

Lib/typing.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ def __getattr__(self, attr):
172172
_lazy_annotationlib = _LazyAnnotationLib()
173173

174174

175+
class _LazyInspect:
176+
def __getattr__(self, attr):
177+
global _lazy_inspect
178+
import inspect
179+
_lazy_inspect = inspect
180+
return getattr(inspect, attr)
181+
182+
_lazy_inspect = _LazyInspect()
183+
184+
175185
def _type_convert(arg, module=None, *, allow_special_forms=False, owner=None):
176186
"""For converting None to type(None), and strings to ForwardRef."""
177187
if arg is None:
@@ -1962,28 +1972,6 @@ def _allow_reckless_class_checks(depth=2):
19621972
}
19631973

19641974

1965-
@functools.cache
1966-
def _lazy_load_getattr_static():
1967-
# Import getattr_static lazily so as not to slow down the import of typing.py
1968-
# Cache the result so we don't slow down _ProtocolMeta.__instancecheck__ unnecessarily
1969-
from inspect import getattr_static
1970-
return getattr_static
1971-
1972-
1973-
_cleanups.append(_lazy_load_getattr_static.cache_clear)
1974-
1975-
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-
19871975
def _pickle_psargs(psargs):
19881976
return ParamSpecArgs, (psargs.__origin__,)
19891977

@@ -2116,7 +2104,7 @@ def __instancecheck__(cls, instance):
21162104
if _abc_instancecheck(cls, instance):
21172105
return True
21182106

2119-
getattr_static = _lazy_load_getattr_static()
2107+
getattr_static = _lazy_inspect.getattr_static
21202108
for attr in cls.__protocol_attrs__:
21212109
try:
21222110
val = getattr_static(instance, attr)
@@ -2496,7 +2484,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
24962484
globalns = obj.__dict__
24972485
else:
24982486
# Find globalns for the unwrapped object.
2499-
nsobj = _lazy_load_unwrap()(obj)
2487+
nsobj = _lazy_inspect.unwrap(obj)
25002488
globalns = getattr(nsobj, '__globals__', {})
25012489
if localns is None:
25022490
localns = globalns

0 commit comments

Comments
 (0)