Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callable[[Any * N], Any]: Trouble matching against constructors for certain built-in types #15

Open
2 tasks
davidfstr opened this issue May 9, 2022 · 2 comments
Labels
enhancement Enhancement to existing feature upstream Requires fix in dependency, or dependency upgrade
Milestone

Comments

@davidfstr
Copy link
Owner

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:

  • Find/create issue on upstream Python issue tracker RE inspect.signature(bool) (and calls on similar built-in types) not working
  • Shepherd that issue to resolution
@davidfstr davidfstr added upstream Requires fix in dependency, or dependency upgrade enhancement Enhancement to existing feature labels May 9, 2022
@davidfstr davidfstr changed the title Callable[[Any], Any]: Trouble matching against constructors for certain built-in types Callable[[Any * N], Any]: Trouble matching against constructors for certain built-in types May 9, 2022
@davidfstr
Copy link
Owner Author

The inspect.signature() documentation alludes:

Note: 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.

@davidfstr davidfstr added this to the Someday milestone May 18, 2022
@davidfstr
Copy link
Owner Author

I think this issue should be considered a bug (and therefore prioritized higher than a normal enhancement) because its existence means that when checking against a Callable[[Any * N], Any] type, the output of trycast is NOT Decidable, which is a goal articulated in trycast's Philosophy.

However this issue is NOT a regression in functionality, so doesn't merit the "regression" label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to existing feature upstream Requires fix in dependency, or dependency upgrade
Projects
None yet
Development

No branches or pull requests

1 participant