Private helper function to get signature for arbitrary callable objects.
(obj, *,
follow_wrapper_chains=True,
skip_bound_arg=True,
globals=None,
locals=None,
eval_str=False,
sigcls,
annotation_format=Format.VALUE)
| 2417 | |
| 2418 | |
| 2419 | def _signature_from_callable(obj, *, |
| 2420 | follow_wrapper_chains=True, |
| 2421 | skip_bound_arg=True, |
| 2422 | globals=None, |
| 2423 | locals=None, |
| 2424 | eval_str=False, |
| 2425 | sigcls, |
| 2426 | annotation_format=Format.VALUE): |
| 2427 | |
| 2428 | """Private helper function to get signature for arbitrary |
| 2429 | callable objects. |
| 2430 | """ |
| 2431 | |
| 2432 | _get_signature_of = functools.partial(_signature_from_callable, |
| 2433 | follow_wrapper_chains=follow_wrapper_chains, |
| 2434 | skip_bound_arg=skip_bound_arg, |
| 2435 | globals=globals, |
| 2436 | locals=locals, |
| 2437 | sigcls=sigcls, |
| 2438 | eval_str=eval_str, |
| 2439 | annotation_format=annotation_format) |
| 2440 | |
| 2441 | if not callable(obj): |
| 2442 | raise TypeError('{!r} is not a callable object'.format(obj)) |
| 2443 | |
| 2444 | if isinstance(obj, types.MethodType): |
| 2445 | # In this case we skip the first parameter of the underlying |
| 2446 | # function (usually `self` or `cls`). |
| 2447 | sig = _get_signature_of(obj.__func__) |
| 2448 | |
| 2449 | if skip_bound_arg: |
| 2450 | return _signature_bound_method(sig) |
| 2451 | else: |
| 2452 | return sig |
| 2453 | |
| 2454 | # Was this function wrapped by a decorator? |
| 2455 | if follow_wrapper_chains: |
| 2456 | # Unwrap until we find an explicit signature or a MethodType (which will be |
| 2457 | # handled explicitly below). |
| 2458 | obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__") |
| 2459 | or isinstance(f, types.MethodType))) |
| 2460 | if isinstance(obj, types.MethodType): |
| 2461 | # If the unwrapped object is a *method*, we might want to |
| 2462 | # skip its first parameter (self). |
| 2463 | # See test_signature_wrapped_bound_method for details. |
| 2464 | return _get_signature_of(obj) |
| 2465 | |
| 2466 | try: |
| 2467 | sig = obj.__signature__ |
| 2468 | except AttributeError: |
| 2469 | pass |
| 2470 | else: |
| 2471 | if sig is not None: |
| 2472 | if not isinstance(sig, Signature): |
| 2473 | raise TypeError( |
| 2474 | 'unexpected object {!r} in __signature__ ' |
| 2475 | 'attribute'.format(sig)) |
| 2476 | return sig |
no test coverage detected
searching dependent graphs…