diff options
author | Ćukasz Langa <lukasz@langa.pl> | 2021-07-17 08:04:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-17 08:04:01 (GMT) |
commit | ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21 (patch) | |
tree | 299a205e4512aebff54f90a08cf29a8be6cdb9e8 /Lib | |
parent | 298ee657ab8170adf75a186c0414b7ca3baf1991 (diff) | |
download | cpython-ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21.zip cpython-ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21.tar.gz cpython-ed2db9b5940ccbc0bc72d01bf62d8b3095ccef21.tar.bz2 |
bpo-40897: Partially backport GH-22583's refactor of inspect.py to allow bugfix backports (#27193)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 53 |
1 files changed, 13 insertions, 40 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 18bed90..0d24b4f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2250,17 +2250,18 @@ def _signature_from_callable(obj, *, callable objects. """ + _get_signature_of = functools.partial(_signature_from_callable, + follow_wrapper_chains=follow_wrapper_chains, + skip_bound_arg=skip_bound_arg, + sigcls=sigcls) + if not callable(obj): raise TypeError('{!r} is not a callable object'.format(obj)) if isinstance(obj, types.MethodType): # In this case we skip the first parameter of the underlying # function (usually `self` or `cls`). - sig = _signature_from_callable( - obj.__func__, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + sig = _get_signature_of(obj.__func__) if skip_bound_arg: return _signature_bound_method(sig) @@ -2274,11 +2275,7 @@ def _signature_from_callable(obj, *, # If the unwrapped object is a *method*, we might want to # skip its first parameter (self). # See test_signature_wrapped_bound_method for details. - return _signature_from_callable( - obj, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + return _get_signature_of(obj) try: sig = obj.__signature__ @@ -2305,11 +2302,7 @@ def _signature_from_callable(obj, *, # (usually `self`, or `cls`) will not be passed # automatically (as for boundmethods) - wrapped_sig = _signature_from_callable( - partialmethod.func, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + wrapped_sig = _get_signature_of(partialmethod.func) sig = _signature_get_partial(wrapped_sig, partialmethod, (None,)) first_wrapped_param = tuple(wrapped_sig.parameters.values())[0] @@ -2335,11 +2328,7 @@ def _signature_from_callable(obj, *, skip_bound_arg=skip_bound_arg) if isinstance(obj, functools.partial): - wrapped_sig = _signature_from_callable( - obj.func, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + wrapped_sig = _get_signature_of(obj.func) return _signature_get_partial(wrapped_sig, obj) sig = None @@ -2350,29 +2339,17 @@ def _signature_from_callable(obj, *, # in its metaclass call = _signature_get_user_defined_method(type(obj), '__call__') if call is not None: - sig = _signature_from_callable( - call, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + sig = _get_signature_of(call) else: # Now we check if the 'obj' class has a '__new__' method new = _signature_get_user_defined_method(obj, '__new__') if new is not None: - sig = _signature_from_callable( - new, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + sig = _get_signature_of(new) else: # Finally, we should have at least __init__ implemented init = _signature_get_user_defined_method(obj, '__init__') if init is not None: - sig = _signature_from_callable( - init, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + sig = _get_signature_of(init) if sig is None: # At this point we know, that `obj` is a class, with no user- @@ -2418,11 +2395,7 @@ def _signature_from_callable(obj, *, call = _signature_get_user_defined_method(type(obj), '__call__') if call is not None: try: - sig = _signature_from_callable( - call, - follow_wrapper_chains=follow_wrapper_chains, - skip_bound_arg=skip_bound_arg, - sigcls=sigcls) + sig = _get_signature_of(call) except ValueError as ex: msg = 'no signature found for {!r}'.format(obj) raise ValueError(msg) from ex |