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
)
| 500 | |
| 501 | |
| 502 | def get_callable_argspec( |
| 503 | fn: Callable[..., Any], no_self: bool = False, _is_init: bool = False |
| 504 | ) -> compat.FullArgSpec: |
| 505 | """Return the argument signature for any callable. |
| 506 | |
| 507 | All pure-Python callables are accepted, including |
| 508 | functions, methods, classes, objects with __call__; |
| 509 | builtins and other edge cases like functools.partial() objects |
| 510 | raise a TypeError. |
| 511 | |
| 512 | """ |
| 513 | if inspect.isbuiltin(fn): |
| 514 | raise TypeError("Can't inspect builtin: %s" % fn) |
| 515 | elif inspect.isfunction(fn) or ( |
| 516 | hasattr(fn, "__code__") |
| 517 | and not inspect.isclass(fn) |
| 518 | and not inspect.ismethod(fn) |
| 519 | ): |
| 520 | if _is_init and no_self: |
| 521 | spec = compat.inspect_getfullargspec(fn) |
| 522 | return compat.FullArgSpec( |
| 523 | spec.args[1:], |
| 524 | spec.varargs, |
| 525 | spec.varkw, |
| 526 | spec.defaults, |
| 527 | spec.kwonlyargs, |
| 528 | spec.kwonlydefaults, |
| 529 | spec.annotations, |
| 530 | ) |
| 531 | else: |
| 532 | return compat.inspect_getfullargspec(fn) |
| 533 | elif inspect.ismethod(fn): |
| 534 | if no_self and (_is_init or fn.__self__): |
| 535 | spec = compat.inspect_getfullargspec(fn.__func__) |
| 536 | return compat.FullArgSpec( |
| 537 | spec.args[1:], |
| 538 | spec.varargs, |
| 539 | spec.varkw, |
| 540 | spec.defaults, |
| 541 | spec.kwonlyargs, |
| 542 | spec.kwonlydefaults, |
| 543 | spec.annotations, |
| 544 | ) |
| 545 | else: |
| 546 | return compat.inspect_getfullargspec(fn.__func__) |
| 547 | elif inspect.isclass(fn): |
| 548 | return get_callable_argspec( |
| 549 | fn.__init__, no_self=no_self, _is_init=True |
| 550 | ) |
| 551 | elif hasattr(fn, "__func__"): |
| 552 | return compat.inspect_getfullargspec(fn.__func__) |
| 553 | elif hasattr(fn, "__call__"): |
| 554 | if inspect.ismethod(fn.__call__): |
| 555 | return get_callable_argspec(fn.__call__, no_self=no_self) |
| 556 | else: |
| 557 | raise TypeError("Can't inspect callable: %s" % fn) |
| 558 | else: |
| 559 | raise TypeError("Can't inspect callable: %s" % fn) |
no outgoing calls