The built-in int
takes two parameters:
>>> print(int.__doc__)
int(x=0) -> integer
int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments
...
However, (in CPython 3.4.0) inspect.signature
shows 0:
>>> len(inspect.signature(int).parameters)
0
in contrast with a user-defined function:
>>> def my_int(x, base=10):
... return int(x, base)
...
>>> len(inspect.signature(my_int).parameters)
2
The docs for inspect.signature
do say:
Some callables may not be introspectable in certain implementations of Python. For example, in CPython, some built-in functions defined in C provide no metadata about their arguments.
But they also say:
Raises ValueError if no signature can be provided, and TypeError if that type of object is not supported.
So I am surprised that I did not get a ValueError
and instead got what appears to be an incorrect signature.
Is there a way to reliably (and programmatically) determine when it is not possible to get the parameters for a callable with inspect
? That is, if I am given something like int
, is there a way to distinguish between "this thing does not have any parameters" and "it is not possible to determine what parameters this thing has"?