Skip to content

Commit 3a3e7fe

Browse files
authored
imprv: asyncio-related stubs in events and Gio (#309)
- Rework `events.pyi` to mirror upstream pygobject structure - In `Gio.pyi`, annotate `Application.create_asyncio_task` as accepting `asyncio._CoroutineLike[_T]` and returning `asyncio.Task[_T]`
1 parent 26eff58 commit 3a3e7fe

2 files changed

Lines changed: 66 additions & 20 deletions

File tree

src/gi-stubs/events.pyi

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,74 @@
1-
from typing import Any
21
from typing_extensions import Self
2+
from typing_extensions import TypeVar
3+
from typing_extensions import TypeVarTuple
4+
from typing_extensions import Unpack
35

6+
import asyncio
7+
import sys
48
from collections.abc import Callable
5-
from signal import Signals
9+
from collections.abc import Generator
10+
from contextlib import contextmanager
11+
from types import TracebackType
612

713
from gi.repository import GLib
814

9-
class GLibTask:
10-
def __init__(self, *args: object, **kwargs: object) -> None: ...
15+
if sys.version_info >= (3, 14):
16+
from asyncio.events import _AbstractEventLoopPolicy
17+
else:
18+
from asyncio.events import AbstractEventLoopPolicy as _AbstractEventLoopPolicy
19+
20+
_T_co = TypeVar("_T_co", covariant=True)
21+
_Ts = TypeVarTuple("_Ts")
22+
23+
class GLibTask(asyncio.Task[_T_co]):
1124
def set_priority(self, priority: int) -> None: ...
1225
def get_priority(self) -> int: ...
1326

14-
class GLibEventLoop:
15-
def __init__(self, main_context: GLib.MainContext) -> None: ...
16-
def add_signal_handler(
17-
self,
18-
sig: Signals,
19-
callback: Callable[..., Any],
20-
*args: object,
21-
) -> None: ...
22-
def remove_signal_handler(self, sig: Signals) -> bool: ...
23-
def close(self) -> None: ...
27+
class _GLibEventLoopMixin:
28+
def __init__(self, main_context: GLib.MainContext | None) -> None: ...
29+
@contextmanager
30+
def paused(self) -> Generator[None]: ...
31+
@contextmanager
32+
def running(self, quit_func: Callable[[], object]) -> Generator[None]: ...
33+
def stop(self) -> None: ...
34+
def time(self) -> float: ...
35+
36+
class _GLibEventLoopRunMixin:
37+
def run_forever(self) -> None: ...
2438

25-
class GLibEventLoopPolicy:
39+
if sys.platform == "win32":
40+
class GLibEventLoop(
41+
_GLibEventLoopMixin, _GLibEventLoopRunMixin, asyncio.ProactorEventLoop
42+
):
43+
def __init__(self, main_context: GLib.MainContext | None = None) -> None: ...
44+
45+
else:
46+
class GLibEventLoop(
47+
_GLibEventLoopMixin, _GLibEventLoopRunMixin, asyncio.SelectorEventLoop
48+
):
49+
def __init__(self, main_context: GLib.MainContext | None = None) -> None: ...
50+
def add_signal_handler(
51+
self,
52+
sig: int,
53+
callback: Callable[[Unpack[_Ts]], object],
54+
*args: Unpack[_Ts],
55+
) -> None: ...
56+
def remove_signal_handler(self, sig: int) -> bool: ...
57+
def close(self) -> None: ...
58+
59+
class GLibEventLoopPolicy(_AbstractEventLoopPolicy):
2660
def __init__(self) -> None: ...
2761
def __enter__(self) -> Self: ...
28-
def __exit__(self, exc_type, exc_value, traceback) -> None: ...
62+
def __exit__(
63+
self,
64+
exc_type: type[BaseException] | None,
65+
exc_value: BaseException | None,
66+
traceback: TracebackType | None,
67+
) -> None: ...
2968
def get_event_loop(self) -> GLibEventLoop: ...
3069
def get_event_loop_for_context(self, ctx: GLib.MainContext) -> GLibEventLoop: ...
31-
def set_event_loop(self, loop: GLibEventLoop) -> None: ...
70+
def set_event_loop(self, loop: asyncio.AbstractEventLoop | None) -> None: ...
71+
def new_event_loop(self) -> GLibEventLoop: ...
72+
73+
if sys.platform != "win32" and sys.version_info < (3, 12):
74+
def get_child_watcher(self) -> asyncio.AbstractChildWatcher: ...

src/gi-stubs/repository/Gio.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ from typing_extensions import TypeVar
1212
from typing_extensions import TypeVarTuple
1313
from typing_extensions import Unpack
1414

15+
import asyncio
1516
from collections.abc import Callable
1617
from collections.abc import Iterator
1718
from collections.abc import Sequence
@@ -21,6 +22,7 @@ from gi.repository import GioUnix
2122
from gi.repository import GLib
2223
from gi.repository import GObject
2324

25+
_T = TypeVar("_T")
2426
ObjectItemType = TypeVar("ObjectItemType", bound=GObject.Object, default=Any)
2527
ObjectPropsItemType = TypeVar("ObjectPropsItemType", bound=GObject.Object, default=Any)
2628
_DataTs = TypeVarTuple("_DataTs", default=Unpack[tuple[()]])
@@ -1230,7 +1232,8 @@ class Application(GObject.Object, ActionGroup, ActionMap):
12301232
def add_main_option_entries(self, entries: Sequence[GLib.OptionEntry]) -> None: ...
12311233
def add_option_group(self, group: GLib.OptionGroup) -> None: ...
12321234
def bind_busy_property(self, object: GObject.Object, property: str) -> None: ...
1233-
def create_asyncio_task(self, coro):
1235+
# override
1236+
def create_asyncio_task(self, coro: asyncio._CoroutineLike[_T]) -> asyncio.Task[_T]:
12341237
"""
12351238
Safely create an asyncio task. The application will not quit until the
12361239
task completes. For potentially longer running tasks, you should add
@@ -1245,7 +1248,7 @@ class Application(GObject.Object, ActionGroup, ActionMap):
12451248
You can deal with this by either only storing a weak reference to the
12461249
Task, by explicitly collecting the result, or by only cancelling it if
12471250
it is not done already.
1248-
""" # FIXME: Override is missing typing annotation
1251+
"""
12491252
def do_activate(self) -> None: ...
12501253
def do_add_platform_data(self, builder: GLib.VariantBuilder, /) -> None: ...
12511254
def do_after_emit(self, platform_data: GLib.Variant, /) -> None: ...
@@ -1293,7 +1296,7 @@ class Application(GObject.Object, ActionGroup, ActionMap):
12931296
def register(self, cancellable: Cancellable | None = None) -> bool: ...
12941297
def release(self) -> None: ...
12951298
# override
1296-
def run(self, argv: list[str] | None) -> int: ...
1299+
def run(self, argv: list[str] | None = None) -> int: ...
12971300
def send_notification(self, id: str | None, notification: Notification) -> None: ...
12981301
def set_action_group(self, action_group: ActionGroup | None = None) -> None: ...
12991302
def set_application_id(self, application_id: str | None = None) -> None: ...

0 commit comments

Comments
 (0)