PyO3 / PyO3/pyo3

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

Open
#4,932 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the Rust #[pymethods] text_signature example and Python's inspect.signature() entry point using the reported versions. Trace how PyO3 exposes text_signature and verify that text_signature remains available; done means inspect.signature() returns the annotated signature without raising ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.