-
Notifications
You must be signed in to change notification settings - Fork 769
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
Autogenerate __text_signature__
#310
Comments
would you accept a pull request that allows adding a user-provided signature using an annotation and have pyo3 automatically put it in the correct location in the docstring? /// doc string 1
#[pyclass(signature = "MyClass(arg1, *, arg2=0)")]
/// doc string 2
struct MyClass {}
#[pymethods]
impl MyClass {
#[new]
/// doc string 1
#[signature = "__new__($cls, arg1, *, arg2=0)"]
/// doc string 2
#[args(arg1, "*", arg2 = "0")]
fn new(obj: &PyRawObject, arg1: i32, arg2: i32) {
obj.init(Self {});
}
/// doc string 1
#[signature = "my_method($self)"]
/// doc string 2
fn my_method(&self) {}
#[staticmethod]
#[signature = "method_without_docs()"]
fn method_without_docs() {}
}
/// doc string 1
#[pyfunction(signature = "my_function(arg1, arg2)")]
/// doc string 2
fn my_function(arg1: i32, arg2: i32) -> i32 {
0
} The generated docstrings would be:
|
🤔 |
yes, it basically tells CPython what the function would look like if it were written in Python. According to CPython's docs, it is a part of Argument Clinic, which doesn't have the same stability guarantees as the rest of the C API, however, it is implemented in pretty much the same way in both CPython and PyPy. Note that My proposal is relatively low implementation effort -- it just copies the provided string into the front of the doc string with the appropriate |
I'm working on creating a pull request that implements the design in #310 (comment) except that I'm using |
Created: #675 |
I plan to have a go at seeing how hard it would be to autogerate |
One good example of auto-generated signatures is in pybind11, where, for instance, you get signatures very close to being mypy-like, e.g. if your function takes |
A first implementation of this was completed in #2784 |
Two notes:
|
As discussed in #305, we can't use
__annotations__
in native code. We could however generate__text_signature__
, using a similar implementation approach as described in #305.The text was updated successfully, but these errors were encountered: