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

adding a text_signature to a function breaks inspect.signature() #4932

Open
DanielT opened this issue Feb 23, 2025 · 4 comments
Open

adding a text_signature to a function breaks inspect.signature() #4932

DanielT opened this issue Feb 23, 2025 · 4 comments
Labels

Comments

@DanielT
Copy link

DanielT commented Feb 23, 2025

Bug Description

I started adding text_signature annotations to my python module, in order to offer better support for IDEs.
When trying to read back my new annotations with inspect.signature(), I got a ValueError. Before adding the text_signature it worked, but only provided the automatically derived information.

Steps to Reproduce

My code:

#[pymethods]
struct Test {
    #[pyo3(text_signature = "(self, greet_text: str) -> None")] // <- this is new
    fn hello(&self, greet_text: &str) {
        // ...
    }
}

Before adding the text_signature:

>>> import mymodule
>>> import inspect
>>> inspect.signature(mymodule.Test.hello)
<Signature (self, /, greet_text)>

after adding the text_signature:

>>> import mymodule
>>> import inspect
>>> inspect.signature(mymodule.Test.hello)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    inspect.signature(mymodule.Test.hello)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\inspect.py", line 3344, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                   globals=globals, locals=locals, eval_str=eval_str)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\inspect.py", line 3070, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
                                    follow_wrapper_chains=follow_wrapped,
                                    globals=globals, locals=locals, eval_str=eval_str)
  File "C:\Program Files\Python313\Lib\inspect.py", line 2582, in _signature_from_callable
    return _signature_from_builtin(sigcls, obj,
                                   skip_bound_arg=skip_bound_arg)
  File "C:\Program Files\Python313\Lib\inspect.py", line 2368, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <method 'hello' of 'mymodule.Test' objects>

After some poking around I found that the __text__signature__ attribute is now missing:

>>> mymodule.Test.hello.__text__signature__ is None
True

This looks like a bug to me.

Backtrace

Your operating system and version

Windows 10

Your Python version (python --version)

Python 3.13.2

Your Rust version (rustc --version)

rust 1.85

Your PyO3 version

0.23.4

How did you install python? Did you use a virtualenv?

I'm using a venv, which I re-created before reporting this bug, just to make sure I wasn't chasing an already-fixed bug

Additional Info

No response

@DanielT DanielT added the bug label Feb 23, 2025
@DanielT
Copy link
Author

DanielT commented Feb 23, 2025

I've also discovered that the function signature turns up in __doc__ when __text_signature__ is broken

@DanielT
Copy link
Author

DanielT commented Feb 23, 2025

I figured it out - the text signature does not support a return type, so adding -> None made my signature invalid

@DanielT DanielT closed this as completed Feb 23, 2025
@LilyFoote
Copy link
Contributor

@DanielT is -> None always invalid here or is it only invalid for your use case? If the former, I think we should re-open and aim to fail to compile here.

@DanielT
Copy link
Author

DanielT commented Feb 23, 2025

The return type is always invalid here.
I dug into the CPython source code, and it is looking for a separator pattern between the text signature and the doc comment, and that pattern must end with a ")" on the text signature.

Edit: Additonally, as far as I can tell inspect.signature would be unable to handle the return type while building a Signature object

@LilyFoote LilyFoote reopened this Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants