-
Notifications
You must be signed in to change notification settings - Fork 887
Closed as not planned
Closed as not planned
Copy link
Description
Would it be possible to extend the text_signature
macro to be able to support python type annotation.
Example
/// Formats the sum of two numbers as string.
#[pyfunction()]
#[text_signature = "(a: int, b: int) -> str"] // <-- this line
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a+b).to_string())
}
Expected documentation:
sum_as_string(a: int, b: int) -> str
Formats the sum of two numbers as string.
- All arguments should be able to have a type annotation
- The return type of the function could be added with
->
type
- According to the PEP any expression should be valid, but I think that limiting it only to types (and maybe quoted string) should be enough
- I think that the types should be python types (python's
int
instead of rust'si64
for example) - Types used in the annotations don't need to be checked in the macro
- In the example, I used
int
for the annotation ofa
andb
even if Rust expect anusize
since python doesn't have unsized integers - In the example, the return type of the function is just
str
and notPyResult
- In the example, I used
Note
After reading #310 (comment) I think that it's not possible to use the same machinery than type annotation in python code (which, from what I understand, would allow IDE to give better auto-completion hints). While waiting for the cpython bug to be fixed, it should still be possible that the text returned by help(sum_as_string)
contains the type annotation even if __annotations__
isn't populated correctly (yet).
Turakar, hombit, LucaCappelletti94, sbdchd, alexkirsz and 13 more