In the test suite's test_callable_p_r you will see lines like:
# NOTE: Cannot introspect constructor for certain built-in types
# TODO: Define __signature__ for failing internal types
self.assertTryCastFailure(Callable[[], Any], bool)
self.assertTryCastFailure(Callable[[], Any], int)
self.assertTryCastSuccess(Callable[[], Any], float)
self.assertTryCastSuccess(Callable[[], Any], complex)
self.assertTryCastFailure(Callable[[], Any], str)
self.assertTryCastSuccess(Callable[[], Any], list)
self.assertTryCastFailure(Callable[[], Any], dict)
self.assertTryCastSuccess(Callable[[], Any], tuple)
We want all of those lines to all be assertTryCastSuccess (and still have the test pass).
The failing lines above occur because an inspect.Signature cannot be generated for the constructor of certain built-in types:
inspect.signature(bool) # ERROR: ValueError: no signature found for builtin type
inspect.signature(int) # ERROR: ValueError: no signature found for builtin type
inspect.signature(float) # OK
inspect.signature(complex) # OK
inspect.signature(str) # ERROR: ValueError: no signature found for builtin type
inspect.signature(list) # OK
inspect.signature(dict) # ERROR: ValueError: no signature found for builtin type
inspect.signature(tuple) # OK
Based on reading the implementation of inspect.signature() and the underlying _signature_from_callable(), it looks like the desired fix would be to define an __signature__ on the type itself. For example:
sig = inspect.Signature()
bool.__signature__ = sig # ERROR: TypeError: can't set attributes of built-in/extension type 'bool'
assert inspect.signature(bool) == sig
Unfortunately as the ERROR above alludes, there's no way to set __signature__ directly from normal Python code.
Next steps:
In the test suite's
test_callable_p_ryou will see lines like:We want all of those lines to all be
assertTryCastSuccess(and still have the test pass).The failing lines above occur because an
inspect.Signaturecannot be generated for the constructor of certain built-in types:Based on reading the implementation of
inspect.signature()and the underlying_signature_from_callable(), it looks like the desired fix would be to define an__signature__on the type itself. For example:Unfortunately as the ERROR above alludes, there's no way to set
__signature__directly from normal Python code.Next steps:
inspect.signature(bool)(and calls on similar built-in types) not working