Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
gh-146553: Use inspect.unwrap() for cycle detection instead of custom…
… helper
  • Loading branch information
raminfp committed Mar 28, 2026
commit 4e9afee252693b009bb523eda601e35aa14d97bd
14 changes: 2 additions & 12 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6842,7 +6842,7 @@ def test_get_type_hints_wrapped_cycle_self(self):
# not loop forever.
def f(x: int) -> str: ...
f.__wrapped__ = f
with self.assertRaises(ValueError):
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(f)

def test_get_type_hints_wrapped_cycle_mutual(self):
Expand All @@ -6852,19 +6852,9 @@ def a(): ...
def b(): ...
a.__wrapped__ = b
b.__wrapped__ = a
with self.assertRaises(ValueError):
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(a)

def test_get_type_hints_wrapped_chain_no_cycle(self):
# gh-146553: a valid (non-cyclic) __wrapped__ chain must still work.
def inner(x: int) -> str: ...
def middle(x: int) -> str: ...
middle.__wrapped__ = inner
def outer(x: int) -> str: ...
outer.__wrapped__ = middle
# No cycle — should return the annotations without raising.
self.assertEqual(get_type_hints(outer), {'x': int, 'return': str})

def test_get_type_hints_annotated(self):
def foobar(x: List['X']): ...
X = Annotated[int, (1, 10)]
Expand Down
16 changes: 3 additions & 13 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,7 @@ def _lazy_load_getattr_static():

_cleanups.append(_lazy_load_getattr_static.cache_clear)


Comment thread
raminfp marked this conversation as resolved.
Outdated
def _pickle_psargs(psargs):
return ParamSpecArgs, (psargs.__origin__,)

Expand Down Expand Up @@ -2483,20 +2484,9 @@ 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.
# Use an id-based visited set to detect and break on cycles in the
# __wrapped__ chain (e.g. f.__wrapped__ = f), matching the behavior
# of inspect.unwrap().
_seen_ids = {id(nsobj)}
while hasattr(nsobj, '__wrapped__'):
nsobj = nsobj.__wrapped__
_nsobj_id = id(nsobj)
if _nsobj_id in _seen_ids:
raise ValueError(
f'wrapper loop when unwrapping {obj!r}'
)
_seen_ids.add(_nsobj_id)
import inspect
nsobj = inspect.unwrap(obj)
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.

Everytime I see local imports for inspect, I feel that we need to have a lower-level inspect utililty that can have less functions but more common to avoid having to import inspect.

What I can suggest for this specific PR is to use lazy import inspect instead at the top-level to make it a bit cleaner. For backports, we'll however need to use a local import. Note that we have other places with a local import to inspect, which you can eventually remove since we would have both a lazy and a local import (which would be redundant). I'll let @JelleZijlstra decide whether to want lazy imports.

If we switch to lazy imports, we'll also need to update the lazy import tests somewhere to check that inspect is lazily imported.

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.

Maybe inspect.unwrap should live in functools instead, it's closely related to functools.wraps after all. But it's probably too late for that. Lazy importing sounds good to me.

globalns = getattr(nsobj, '__globals__', {})
if localns is None:
localns = globalns
Expand Down
Loading