Skip to content

Add Python return type for async functions #1984

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

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ pub async fn sleep(ms: u16) -> bool {
true
}

/// Async function that sleeps with no return type
#[uniffi::export]
pub async fn sleep_no_return(ms: u16) {
TimerFuture::new(Duration::from_millis(ms.into())).await;
}

// Our error.
#[derive(thiserror::Error, uniffi::Error, Debug)]
pub enum MyError {
Expand Down
6 changes: 6 additions & 0 deletions fixtures/futures/tests/bindings/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,11 @@ async def test():
await use_shared_resource(SharedResourceOptions(release_after_ms=0, timeout_ms=1000))
asyncio.run(test())

def test_function_annotations(self):
async def test():
assert sleep.__annotations__ == {"ms": "int", "return": "bool"}
assert sleep_no_return.__annotations__ == {"ms": "int", "return": None}
asyncio.run(test())

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{%- if func.is_async() %}

def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}):
{%- match func.return_type() -%}
{%- when Some with (return_type) %}
async def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> "{{ return_type|type_name }}":
{% when None %}
async def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> None:
{% endmatch %}

{%- call py::docstring(func, 4) %}
{%- call py::setup_args(func) %}
return _uniffi_rust_call_async(
return await _uniffi_rust_call_async(
_UniffiLib.{{ func.ffi_func().name() }}({% call py::arg_list_lowered(func) %}),
_UniffiLib.{{func.ffi_rust_future_poll(ci) }},
_UniffiLib.{{func.ffi_rust_future_complete(ci) }},
Expand Down Expand Up @@ -34,7 +40,7 @@ def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> "{{ retur
return {{ return_type|lift_fn }}({% call py::to_ffi_call(func) %})
{% when None %}

def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}):
def {{ func.name()|fn_name }}({%- call py::arg_list_decl(func) -%}) -> None:
{%- call py::docstring(func, 4) %}
{%- call py::setup_args(func) %}
{% call py::to_ffi_call(func) %}
Expand Down
12 changes: 9 additions & 3 deletions uniffi_bindgen/src/bindings/python/templates/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,16 @@
{%- macro method_decl(py_method_name, meth) %}
{% if meth.is_async() %}

def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
{%- match meth.return_type() %}
{%- when Some with (return_type) %}
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}":
{%- when None %}
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
{% endmatch %}

{%- call docstring(meth, 8) %}
{%- call setup_args_extra_indent(meth) %}
return _uniffi_rust_call_async(
return await _uniffi_rust_call_async(
_UniffiLib.{{ meth.ffi_func().name() }}(
self._uniffi_clone_pointer(), {% call arg_list_lowered(meth) %}
),
Expand Down Expand Up @@ -155,7 +161,7 @@ def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_typ

{%- when None %}

def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
{%- call docstring(meth, 8) %}
{%- call setup_args_extra_indent(meth) %}
{% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %}
Expand Down