10000 bpo-40897: Partially backport GH-22583's refactor of inspect.py to al… · python/cpython@ed2db9b · GitHub
[go: up one dir, main page]

Skip to content

Commit ed2db9b

Browse files
authored
bpo-40897: Partially backport GH-22583's refactor of inspect.py to allow bugfix backports (#27193)
1 parent 298ee65 commit ed2db9b

File tree

1 file changed

+13
-40
lines changed

1 file changed

+13
-40
lines changed

Lib/inspect.py

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,17 +2250,18 @@ def _signature_from_callable(obj, *,
22502250
callable objects.
22512251
"""
22522252

2253+
_get_signature_of = functools.partial(_signature_from_callable,
2254+
follow_wrapper_chains=follow_wrapper_chains,
2255+
skip_bound_arg=skip_bound_arg,
2256+
sigcls=sigcls)
2257+
22532258
if not callable(obj):
22542259
raise TypeError('{!r} is not a callable object'.format(obj))
22552260

22562261
if isinstance(obj, types.MethodType):
22572262
# In this case we skip the first parameter of the underlying
22582263
# function (usually `self` or `cls`).
2259-
sig = _signature_from_callable(
2260-
obj.__func__,
2261-
follow_wrapper_chains=follow_wrapper_chains,
2262-
skip_bound_arg=skip_bound_arg,
2263-
sigcls=sigcls)
2264+
sig = _get_signature_of(obj.__func__)
22642265

22652266
if skip_bound_arg:
22662267
return _signature_bound_method(sig)
@@ -2274,11 +2275,7 @@ def _signature_from_callable(obj, *,
22742275
# If the unwrapped object is a *method*, we might want to
22752276
# skip its first parameter (self).
22762277
# See test_signature_wrapped_bound_method for details.
2277-
return _signature_from_callable(
2278-
obj,
2279-
follow_wrapper_chains=follow_wrapper_chains,
2280-
skip_bound_arg=skip_bound_arg,
2281-
sigcls=sigcls)
2278+
return _get_signature_of(obj)
22822279

22832280
try:
22842281
sig = obj.__signature__
@@ -2305,11 +2302,7 @@ def _signature_from_callable(obj, *,
23052302
# (usually `self`, or `cls`) will not be passed
23062303
# automatically (as for boundmethods)
23072304

2308-
wrapped_sig = _signature_from_callable(
2309-
partialmethod.func,
2310-
follow_wrapper_chains=follow_wrapper_chains,
2311-
skip_bound_arg=skip_bound_arg,
2312-
sigcls=sigcls)
2305+
wrapped_sig = _get_signature_of(partialmethod.func)
23132306

23142307
sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
23152308
first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
@@ -2335,11 +2328,7 @@ def _signature_from_callable(obj, *,
23352328
skip_bound_arg=skip_bound_arg)
23362329

23372330
if isinstance(obj, functools.partial):
2338-
wrapped_sig = _signature_from_callable(
2339-
obj.func,
2340-
follow_wrapper_chains=follow_wrapper_chains,
2341-
skip_bound_arg=skip_bound_arg,
2342-
sigcls=sigcls)
2331+
wrapped_sig = _get_signature_of(obj.func)
23432332
return _signature_get_partial(wrapped_sig, obj)
23442333

23452334
sig = None
@@ -2350,29 +2339,17 @@ def _signature_from_callable(obj, *,
23502339
# in its metaclass
23512340
call = _signature_get_user_defined_method(type(obj), '__call__')
23522341
if call is not None:
2353-
sig = _signature_from_callable(
2354-
call,
2355-
follow_wrapper_chains=follow_wrapper_chains,
2356-
skip_bound_arg=skip_bound_arg,
2357-
sigcls=sigcls)
2342+
sig = _get_signature_of(call)
23582343
else:
23592344
# Now we check if the 'obj' class has a '__new__' method
23602345
new = _signature_get_user_defined_method(obj, '__new__')
23612346
if new is not None:
2362-
sig = _signature_from_callable(
2363-
new,
2364-
follow_wrapper_chains=follow_wrapper_chains,
2365-
skip_bound_arg=skip_bound_arg,
2366-
sigcls=sigcls)
2347+
sig = _get_signature_of(new)
23672348
else:
23682349
# Finally, we should have at least __init__ implemented
23692350
init = _signature_get_user_defined_method(obj, '__init__')
23702351
if init is not None:
2371-
sig = _signature_from_callable(
2372-
init,
2373-
follow_wrapper_chains=follow_wrapper_chains,
2374-
skip_bound_arg=skip_bound_arg,
2375-
sigcls=sigcls)
2352+
sig = _get_signature_of(init)
23762353

23772354
if sig is None:
23782355
# At this point we know, that `obj` is a class, with no user-
@@ -2418,11 +2395,7 @@ def _signature_from_callable(obj, *,
24182395
call = _signature_get_user_defined_method(type(obj), '__call__')
24192396
if call is not None:
24202397
try:
2421-
sig = _signature_from_callable(
2422-
call,
2423-
follow_wrapper_chains=follow_wrapper_chains,
2424-
skip_bound_arg=skip_bound_arg,
2425-
sigcls=sigcls)
2398+
sig = _get_signature_of(call)
24262399
except ValueError as ex:
24272400
msg = 'no signature found for {!r}'.format(obj)
24282401
raise ValueError(msg) from ex

0 commit comments

Comments
 (0)
0