PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.06k stars 744 forks source link

`text_signature` not available for `#pyclass` #4586

Open piotryordanov opened 3 hours ago

piotryordanov commented 3 hours ago

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_
davidhewitt commented 3 hours ago

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

piotryordanov commented 3 hours ago

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

davidhewitt commented 2 hours ago

Ah, thanks for flagging - docs need fixing!

piotryordanov commented 1 hour ago

@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)