pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

__new__ and __init__ get no call signature

Open twuebi opened this issue 5 years ago • 1 comments

I am trying to add my doc-string and an explicit signature for __new__ and __init__ to Foo:

/// Foo(arg1, arg2)
/// --
///
/// A Foo
#[pyclass(name=Foo)]
pub struct PyFoo {
    inner: Rc<RefCell<Foo>>,
}

#[pymethods]
impl PyFoo {
    /// __new__(arg1, arg2)
    /// --
    ///
    /// Construct a new Foo
    #[new]
    fn __new__(
        obj: &PyRawObject,
        arg1: Vec<&str>,
        arg2: Vec<&str>
    ) -> PyResult<()> {
...
}

So far, I'm getting the call signature of Foo via inspect.signature(Foo) or with IPython Foo? but not through help(Foo). Both __new__ and __init__ remain with their generic doc-strings and signatures defined in typeobject.c.

As far as I understand, CPython expects functions for tp_new and tp_init which is why the ml_doc fields of their PyMethodDef get discarded in pyo3::type_object::py_class_method_defs. Is there some other way to modify the doc-strings of these built-ins?

twuebi avatar Jul 21 '19 21:07 twuebi

The init signature has been added to help(type) on some version >3.6.5 which I am on:

https://github.com/python/cpython/commit/ccb5f3cee9ebb5f20e33f31432a46579f8e9bf8e#diff-cbbbf1954ca2586f5d1d6241674fab9a

It still would be nice to be able to set the doc strings though.

twuebi avatar Jul 22 '19 08:07 twuebi