Skip to content
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
21 changes: 19 additions & 2 deletions bindings/py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ Generated async methods return typed, asyncio-compatible operation objects:
```python
operation = writer.store_async()
stored_bytes = await operation

task = asyncio.create_task(writer.store_async())
stored_bytes = await task

async with asyncio.TaskGroup() as group:
task = group.create_task(writer.store_async())
```

Their public types are `WinRTAsync[T]` and
`WinRTAsyncWithProgress[T, P]`; the concrete runtime wrappers remain private.
Generated methods return `WinRTCoroutine[T]` and
`WinRTCoroutineWithProgress[T, P]`, which retain the structural
`WinRTAsync[T]` and `WinRTAsyncWithProgress[T, P]` contracts while also being
typed as coroutines for `asyncio.create_task()`. The concrete runtime wrappers
remain private.

Regenerated bindings no longer block inside async methods. Existing code that
expects an immediate result must use `await operation` or `operation.wait()`.
Expand All @@ -39,6 +48,14 @@ WinRT operation. Operations with supported progress values also expose
`operation.progress(callback)`. Fast operations can finish before registration;
in that case no future progress exists and registration is a no-op.

An operation can be awaited directly more than once; every direct await observes
the same converted completion, failure, or cancellation. Its coroutine driver is
one-shot like a native Python coroutine, so pass a given operation to
`create_task()`, `TaskGroup.create_task()`, or `ensure_future()` only once.
Directly awaiting that operation after its task completes remains supported.
Calling `close()` cancels pending WinRT work and permanently closes the coroutine
driver.

For scripts without an event loop, `operation.wait()` remains available as an
explicit blocking API. It rejects started operations when called from a running
asyncio loop or an STA thread, where blocking could freeze or deadlock the
Expand Down
17 changes: 16 additions & 1 deletion bindings/py/dynwinrt.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Awaitable, Callable, List, Literal, Mapping, Optional, Protocol, Sequence, TypeVar, Union, final, overload
from collections.abc import Coroutine
from typing import Any, Awaitable, Callable, List, Literal, Mapping, Optional, Protocol, Sequence, TypeVar, Union, final, overload
from uuid import UUID

_T = TypeVar("_T", covariant=True)
Expand Down Expand Up @@ -35,6 +36,8 @@ __all__ = [
"DynWinRtElementFactory",
"WinRTAsync",
"WinRTAsyncWithProgress",
"WinRTCoroutine",
"WinRTCoroutineWithProgress",
"ProjectedLifetimeScope",
"projected_lifetime_scope",
"project_as",
Expand Down Expand Up @@ -388,6 +391,18 @@ class WinRTAsyncWithProgress(WinRTAsync[_T], Protocol[_T, _P]):
def progress(self, callback: Callable[[_P], object]) -> None: ...


class WinRTCoroutine( # type: ignore[misc]
WinRTAsync[_T], Coroutine[Any, Any, _T], Protocol[_T]
): ...


class WinRTCoroutineWithProgress(
WinRTAsyncWithProgress[_T, _P],
WinRTCoroutine[_T],
Protocol[_T, _P],
): ...


@final
class DynWinRTArray:
def __len__(self) -> int: ...
Expand Down
Loading
Loading