pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

`text_signature` not available for `#pyclass`

Open piotryordanov opened this issue 1 year ago • 5 comments

Bug Description

I expect to be able to add the text_signature to the pyclass, but it's failing. Running pyo3 version 0.22.3.

Steps to Reproduce

#[pyclass(text_signature = "(index=1)")]
#[derive(Debug, Clone)]
pub struct Signal {
    pub index: i32,
}

Backtrace

error: expected one of: `crate`, `dict`, `eq`, `eq_int`, `extends`, `freelist`, `frozen`, `get_all`, `hash`, `mapping`, `module`, `name`, `ord`, `rename_all`, `sequence`, `set_all`, `subclass`, `unsendable`, `weakref`


### Your operating system and version

arch

### Your Python version (`python --version`)

3.10.12

### Your Rust version (`rustc --version`)

1.71.1

### Your PyO3 version

0.22.3

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

python env is managed by rye

### Additional Info

_No response_

piotryordanov avatar Sep 30 '24 20:09 piotryordanov

#[pyo3(text_signature = "(index=1)"] should go on your #[new] function since PyO3 0.19.

davidhewitt avatar Sep 30 '24 20:09 davidhewitt

Ah that solves it. Thx for the fast reply.

What confused me was that it's still mentioned as a valid option in here and here

piotryordanov avatar Sep 30 '24 20:09 piotryordanov

Ah, thanks for flagging - docs need fixing!

davidhewitt avatar Sep 30 '24 20:09 davidhewitt

@davidhewitt I tried the change you suggested, but for some reason, it's not propagatd to python.

#[pymethods]
impl Signal {
    /// This is a signal that does xyz
    #[new]
    #[pyo3(text_signature = "(index)")]
    fn new(index: i32) -> Self {
        Signal { index }
    }

And in python, the signature for __new__ shows as:

($type, *args, **kwargs)

piotryordanov avatar Sep 30 '24 21:09 piotryordanov

The text signature ends up just below the class header, rather than on the __new__ help, e.g.:

Help on class Foo in module pyo3_scratch:

class Foo(builtins.object)
 |  Foo(index, /)                         <----- here

To put it on __new__, would require the same kind of solution as in #4326

davidhewitt avatar Oct 01 '24 20:10 davidhewitt