Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6837,6 +6837,24 @@ def test_get_type_hints_wrapped_decoratored_func(self):
self.assertEqual(gth(ForRefExample.func), expects)
self.assertEqual(gth(ForRefExample.nested), expects)

def test_get_type_hints_wrapped_cycle_self(self):
# gh-146553: __wrapped__ self-reference must raise ValueError,
# not loop forever.
def f(x: int) -> str: ...
f.__wrapped__ = f
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(f)

def test_get_type_hints_wrapped_cycle_mutual(self):
# gh-146553: mutual __wrapped__ cycle (a -> b -> a) must raise
# ValueError, not loop forever.
def a(): ...
def b(): ...
a.__wrapped__ = b
b.__wrapped__ = a
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(a)

def test_get_type_hints_annotated(self):
def foobar(x: List['X']): ...
X = Annotated[int, (1, 10)]
Expand Down
26 changes: 12 additions & 14 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ def __getattr__(self, attr):
_lazy_annotationlib = _LazyAnnotationLib()


class _LazyInspect:
def __getattr__(self, attr):
global _lazy_inspect
import inspect
_lazy_inspect = inspect
return getattr(inspect, attr)

_lazy_inspect = _LazyInspect()
Comment on lines +175 to +182
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.

Why do we have this construction? Please don't use an LLM to generate this. Just use lazy import inspect and check if there is nothing wrong with that construction as per the DPO thread.

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.

For annotationlib, we'll clean it up either in a follow-up if codeowners do want it, or we will keep it as is until we need to modify annotationlib calls somewhere.



def _type_convert(arg, module=None, *, allow_special_forms=False, owner=None):
"""For converting None to type(None), and strings to ForwardRef."""
if arg is None:
Expand Down Expand Up @@ -1962,16 +1972,6 @@ def _allow_reckless_class_checks(depth=2):
}


@functools.cache
def _lazy_load_getattr_static():
# Import getattr_static lazily so as not to slow down the import of typing.py
# Cache the result so we don't slow down _ProtocolMeta.__instancecheck__ unnecessarily
from inspect import getattr_static
return getattr_static


_cleanups.append(_lazy_load_getattr_static.cache_clear)

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

Expand Down Expand Up @@ -2104,7 +2104,7 @@ def __instancecheck__(cls, instance):
if _abc_instancecheck(cls, instance):
return True

getattr_static = _lazy_load_getattr_static()
getattr_static = _lazy_inspect.getattr_static
for attr in cls.__protocol_attrs__:
try:
val = getattr_static(instance, attr)
Expand Down Expand Up @@ -2483,10 +2483,8 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
if isinstance(obj, types.ModuleType):
globalns = obj.__dict__
else:
nsobj = obj
# Find globalns for the unwrapped object.
while hasattr(nsobj, '__wrapped__'):
nsobj = nsobj.__wrapped__
nsobj = _lazy_inspect.unwrap(obj)
globalns = getattr(nsobj, '__globals__', {})
if localns is None:
localns = globalns
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :func:`typing.get_type_hints` hanging indefinitely when a callable has a
circular ``__wrapped__`` chain (e.g. ``f.__wrapped__ = f``). A
:exc:`ValueError` is now raised on cycle detection, matching the behavior of
:func:`inspect.unwrap`.
Loading