mypyc icon indicating copy to clipboard operation
mypyc copied to clipboard

inspect.signature fails on compiled functions

Open twoertwein opened this issue 3 years ago • 5 comments

signature.py

import inspect

import compiled

inspect.signature(compiled.fun)

compiled.py

def fun(test: str = "test") -> int:
    return 1
$ mypyc compiled.py
$ python signature.py
Traceback (most recent call last):
  File "/pool01/home/twoertwe/git/python-tools/signature.py", line 5, in <module>
    inspect.signature(compiled.fun)
  File "/home/twoertwe/local/lib/python3.9/inspect.py", line 3130, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "/home/twoertwe/local/lib/python3.9/inspect.py", line 2879, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "/home/twoertwe/local/lib/python3.9/inspect.py", line 2334, in _signature_from_callable
    return _signature_from_builtin(sigcls, obj,
  File "/home/twoertwe/local/lib/python3.9/inspect.py", line 2145, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <built-in function fun>

I encountered this issue when using a compiled function with client.register_worker_callbacks in dask https://docs.dask.org/en/latest/futures.html?highlight=register_worker_callbacks#distributed.Client.register_worker_callbacks

twoertwein avatar Apr 19 '21 02:04 twoertwein

This would be fixable by adding __text_signature__ attribute to the compiled function object.

JelleZijlstra avatar Apr 19 '21 02:04 JelleZijlstra

@JelleZijlstra is there a dynamic way of doing it? Besides manually editing the sources generated by mypyc...

insilications avatar Nov 24 '21 17:11 insilications

Looking into https://github.com/mypyc/mypyc/issues/437 ...

Maybe this would be the starting point to make mypyc utilize docstrings in a useful manner. I'm trying to understand mypy AST and IR generation to see if this can be easily implemented

insilications avatar Nov 24 '21 17:11 insilications

This would need to happen during IR building. I don't expect it to be very difficult to implement but not quite trivial either.

JukkaL avatar Nov 25 '21 14:11 JukkaL

I'll look into it. I know advanced C/C++, but very little experience with complex python. I will try this as a study target.

insilications avatar Nov 27 '21 18:11 insilications

@JelleZijlstra and @JukkaL I can take this on if no one is working on it, since this is preventing me from using Mypyc to compile one of my projects.

I am still looking into things, but from what I can tell, __text_signature__ is not officially documented, and has to be stuffed in via the __doc__ field of the compiled function, is that correct? I tried setting the __text_signature__ attribute on the function, but kept getting write-protected errors.

dosisod avatar Dec 11 '22 07:12 dosisod