Skip to content

[3.10] gh-74044: inspect.signature for wrappers around decorated bound methods (GH-736) #99329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
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
5 changes: 4 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2406,7 +2406,10 @@ def _signature_from_callable(obj, *,

# Was this function wrapped by a decorator?
if follow_wrapper_chains:
obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
# Unwrap until we find an explicit signature or a MethodType (which will be
# handled explicitly below).
obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")
or isinstance(f, types.MethodType)))
if isinstance(obj, types.MethodType):
# If the unwrapped object is a *method*, we might want to
# skip its first parameter (self).
Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2957,8 +2957,6 @@ def foo(a): pass
self.assertEqual(str(inspect.signature(foo)), '(a)')

def test_signature_on_decorated(self):
import functools

def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs) -> int:
Expand All @@ -2970,6 +2968,8 @@ class Foo:
def bar(self, a, b):
pass

bar = decorator(Foo().bar)

self.assertEqual(self.signature(Foo.bar),
((('self', ..., ..., "positional_or_keyword"),
('a', ..., ..., "positional_or_keyword"),
Expand All @@ -2988,6 +2988,11 @@ def bar(self, a, b):
# from "func" to "wrapper", hence no
# return_annotation

self.assertEqual(self.signature(bar),
((('a', ..., ..., "positional_or_keyword"),
('b', ..., ..., "positional_or_keyword")),
...))

# Test that we handle method wrappers correctly
def decorator(func):
@functools.wraps(func)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug where :func:`inspect.signature` reported incorrect arguments for decorated methods.