Skip to content

Commit 545bcd7

Browse files
authored
Add Python annotations for the return type for async functions (#1984)
1 parent 5e8fa89 commit 545bcd7

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

fixtures/futures/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ pub async fn sleep(ms: u16) -> bool {
101101
true
102102
}
103103

104+
/// Async function that sleeps with no return type
105+
#[uniffi::export]
106+
pub async fn sleep_no_return(ms: u16) {
107+
TimerFuture::new(Duration::from_millis(ms.into())).await;
108+
}
109+
104110
// Our error.
105111
#[derive(thiserror::Error, uniffi::Error, Debug)]
106112
pub enum MyError {

fixtures/futures/tests/bindings/test_futures.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,11 @@ async def test():
208208
await use_shared_resource(SharedResourceOptions(release_after_ms=0, timeout_ms=1000))
209209
asyncio.run(test())
210210

211+
def test_function_annotations(self):
212+
async def test():
213+
assert sleep.__annotations__ == {"ms": "int", "return": "bool"}
214+
assert sleep_no_return.__annotations__ == {"ms": "int", "return": None}
215+
asyncio.run(test())
216+
211217
if __name__ == '__main__':
212218
unittest.main()

uniffi_bindgen/src/bindings/python/templates/TopLevelFunctionTemplate.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{%- if func.is_async() %}
22

3-
def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}):
3+
{%- match func.return_type() -%}
4+
{%- when Some with (return_type) %}
5+
async def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> "{{ return_type|type_name }}":
6+
{% when None %}
7+
async def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> None:
8+
{% endmatch %}
9+
410
{%- call py::docstring(func, 4) %}
511
{%- call py::setup_args(func) %}
6-
return _uniffi_rust_call_async(
12+
return await _uniffi_rust_call_async(
713
_UniffiLib.{{ func.ffi_func().name() }}({% call py::arg_list_lowered(func) %}),
814
_UniffiLib.{{func.ffi_rust_future_poll(ci) }},
915
_UniffiLib.{{func.ffi_rust_future_complete(ci) }},
@@ -34,7 +40,7 @@ def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> "{{ retur
3440
return {{ return_type|lift_fn }}({% call py::to_ffi_call(func) %})
3541
{% when None %}
3642

37-
def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}):
43+
def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> None:
3844
{%- call py::docstring(func, 4) %}
3945
{%- call py::setup_args(func) %}
4046
{% call py::to_ffi_call(func) %}

uniffi_bindgen/src/bindings/python/templates/macros.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,16 @@
115115
{%- macro method_decl(py_method_name, meth) %}
116116
{% if meth.is_async() %}
117117

118-
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
118+
{%- match meth.return_type() %}
119+
{%- when Some with (return_type) %}
120+
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}":
121+
{%- when None %}
122+
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
123+
{% endmatch %}
124+
119125
{%- call docstring(meth, 8) %}
120126
{%- call setup_args_extra_indent(meth) %}
121-
return _uniffi_rust_call_async(
127+
return await _uniffi_rust_call_async(
122128
_UniffiLib.{{ meth.ffi_func().name() }}(
123129
self._uniffi_clone_pointer(), {% call arg_list_lowered(meth) %}
124130
),
@@ -155,7 +161,7 @@ def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_typ
155161

156162
{%- when None %}
157163

158-
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
164+
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
159165
{%- call docstring(meth, 8) %}
160166
{%- call setup_args_extra_indent(meth) %}
161167
{% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %}

0 commit comments

Comments
 (0)