Return the argument signature for any callable. All pure-Python callables are accepted, including functions, methods, classes, objects with __call__; builtins and other edge cases like functools.partial() objects raise a TypeError.
(
fn: Callable[..., Any], no_self: bool = False, _is_init: bool = False
)
| 459 | |
| 460 | |
| 461 | def get_callable_argspec( |
| 462 | fn: Callable[..., Any], no_self: bool = False, _is_init: bool = False |
| 463 | ) -> compat.FullArgSpec: |
| 464 | """Return the argument signature for any callable. |
| 465 | |
| 466 | All pure-Python callables are accepted, including |
| 467 | functions, methods, classes, objects with __call__; |
| 468 | builtins and other edge cases like functools.partial() objects |
| 469 | raise a TypeError. |
| 470 | |
| 471 | """ |
| 472 | if inspect.isbuiltin(fn): |
| 473 | raise TypeError("Can't inspect builtin: %s" % fn) |
| 474 | elif inspect.isfunction(fn) or ( |
| 475 | hasattr(fn, "__code__") |
| 476 | and not inspect.isclass(fn) |
| 477 | and not inspect.ismethod(fn) |
| 478 | ): |
| 479 | if _is_init and no_self: |
| 480 | spec = compat.inspect_getfullargspec(fn) |
| 481 | return compat.FullArgSpec( |
| 482 | spec.args[1:], |
| 483 | spec.varargs, |
| 484 | spec.varkw, |
| 485 | spec.defaults, |
| 486 | spec.kwonlyargs, |
| 487 | spec.kwonlydefaults, |
| 488 | spec.annotations, |
| 489 | ) |
| 490 | else: |
| 491 | return compat.inspect_getfullargspec(fn) |
| 492 | elif inspect.ismethod(fn): |
| 493 | if no_self and (_is_init or fn.__self__): |
| 494 | spec = compat.inspect_getfullargspec(fn.__func__) |
| 495 | return compat.FullArgSpec( |
| 496 | spec.args[1:], |
| 497 | spec.varargs, |
| 498 | spec.varkw, |
| 499 | spec.defaults, |
| 500 | spec.kwonlyargs, |
| 501 | spec.kwonlydefaults, |
| 502 | spec.annotations, |
| 503 | ) |
| 504 | else: |
| 505 | return compat.inspect_getfullargspec(fn.__func__) |
| 506 | elif inspect.isclass(fn): |
| 507 | return get_callable_argspec( |
| 508 | fn.__init__, no_self=no_self, _is_init=True |
| 509 | ) |
| 510 | elif hasattr(fn, "__func__"): |
| 511 | return compat.inspect_getfullargspec(fn.__func__) |
| 512 | elif hasattr(fn, "__call__"): |
| 513 | if inspect.ismethod(fn.__call__): |
| 514 | return get_callable_argspec(fn.__call__, no_self=no_self) |
| 515 | else: |
| 516 | raise TypeError("Can't inspect callable: %s" % fn) |
| 517 | else: |
| 518 | raise TypeError("Can't inspect callable: %s" % fn) |
no outgoing calls