Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,17 @@ def _lazy_load_getattr_static():
_cleanups.append(_lazy_load_getattr_static.cache_clear)


Comment thread
raminfp marked this conversation as resolved.
Outdated
@functools.cache
def _lazy_load_unwrap():
# Import unwrap lazily so as not to slow down the import of typing.py
# Cache the result so we don't slow down get_type_hints() unnecessarily
from inspect import unwrap
return unwrap


_cleanups.append(_lazy_load_unwrap.cache_clear)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use lazy import inspect now and get rid of those lazy load stuff?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some controversy about using PEP 810 too widely in the stdlib https://discuss.python.org/t/concerns-about-x-lazy-imports-none/106203

I don't have a strong opinion myself, to be clear

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@picnixz Done, replaced both _lazy_load_getattr_static and _lazy_load_unwrap with a _LazyInspect class following the existing _LazyAnnotationLib pattern already in the file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some controversy about using PEP 810 too widely in the stdlib

Jelle said he was fine so I think we can do the switch unless it's breaking something. @raminfp please check that the concerns expressed in the DPO thread are not concerns, but do so without using an LLM.



def _pickle_psargs(psargs):
return ParamSpecArgs, (psargs.__origin__,)

Expand Down Expand Up @@ -2485,8 +2496,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
globalns = obj.__dict__
else:
# Find globalns for the unwrapped object.
import inspect
nsobj = inspect.unwrap(obj)
nsobj = _lazy_load_unwrap()(obj)
globalns = getattr(nsobj, '__globals__', {})
if localns is None:
localns = globalns
Expand Down
Loading