From 55f34ca39abf77781b705f18864f380340f76979 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Thu, 9 Mar 2023 13:22:29 +0100 Subject: [PATCH] typing: Gio: Make ListModel and ListStore generic --- src/gi-stubs/repository/Gio.pyi | 62 ++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/gi-stubs/repository/Gio.pyi b/src/gi-stubs/repository/Gio.pyi index 3eab08c0..a51eddf0 100644 --- a/src/gi-stubs/repository/Gio.pyi +++ b/src/gi-stubs/repository/Gio.pyi @@ -6,10 +6,13 @@ from typing import Optional from typing import Sequence from typing import Tuple from typing import Type +from typing import TypeVar from gi.repository import GLib from gi.repository import GObject +ItemType = TypeVar("ItemType") + DBUS_METHOD_INVOCATION_HANDLED: bool = True DBUS_METHOD_INVOCATION_UNHANDLED: bool = False DEBUG_CONTROLLER_EXTENSION_POINT_NAME: str = "gio-debug-controller" @@ -4035,45 +4038,64 @@ class InputVector(GObject.GPointer): size: int = ... # override -class ListModel(GObject.GInterface): - def __contains__(self, item: object) -> bool: ... - def __getitem__(self, position: int) -> GObject.Object: ... - def __iter__(self) -> Iterator[GObject.Object]: ... +class ListModel(GObject.GInterface, Generic[ItemType]): + # override + def __contains__(self, item: ItemType) -> bool: ... + # override + def __getitem__(self, position: int) -> ItemType: ... + # override + def __iter__(self) -> Iterator[ItemType]: ... def __len__(self) -> int: ... - def get_item(self, position: int) -> Optional[GObject.Object]: ... - def get_item_type(self) -> Type: ... + # override + def get_item(self, position: int) -> Optional[ItemType]: ... + # override + def get_item_type(self) -> Type[ItemType]: ... def get_n_items(self) -> int: ... def items_changed(self, position: int, removed: int, added: int) -> None: ... -class ListModelInterface(GObject.GPointer): +# override +class ListModelInterface(GObject.GPointer, Generic[ItemType]): g_iface: GObject.TypeInterface = ... - get_item_type: Callable[[ListModel], Type] = ... - get_n_items: Callable[[ListModel], int] = ... - get_item: Callable[[ListModel, int], Optional[GObject.Object]] = ... + # override + get_item_type: Callable[[ListModel[ItemType]], Type[ItemType]] = ... + # override + get_n_items: Callable[[ListModel[ItemType]], int] = ... + # override + get_item: Callable[[ListModel[ItemType], int], Optional[ItemType]] = ... -class ListStore(GObject.Object, ListModel): +# override +class ListStore(GObject.Object, Generic[ItemType], ListModel[ItemType]): class Props: - item_type: Type + # override + item_type: Type[ItemType] n_items: int props: Props = ... - def __init__(self, item_type: Type = ...): ... - def append(self, item: GObject.Object) -> None: ... - def find(self, item: GObject.Object) -> Tuple[bool, int]: ... + # override + def __init__(self, item_type: Type[ItemType] = ...): ... + # override + def append(self, item: ItemType) -> None: ... + # override + def find(self, item: ItemType) -> Tuple[bool, int]: ... + # override def find_with_equal_func( - self, item: GObject.Object, equal_func: Callable[[None, None], bool] + self, item: ItemType, equal_func: Callable[[None, None], bool] ) -> Tuple[bool, int]: ... + # override def find_with_equal_func_full( - self, item: GObject.Object, equal_func: Callable[..., bool], *user_data: Any + self, item: ItemType, equal_func: Callable[..., bool], *user_data: Any ) -> Tuple[bool, int]: ... - def insert(self, position: int, item: GObject.Object) -> None: ... + # override + def insert(self, position: int, item: ItemType) -> None: ... def insert_sorted(self, item, compare_func, *user_data): ... # FIXME Function + # override @classmethod - def new(cls, item_type: Type) -> ListStore: ... + def new(cls, item_type: Type[ItemType]) -> ListStore[ItemType]: ... def remove(self, position: int) -> None: ... def remove_all(self) -> None: ... def sort(self, compare_func, *user_data): ... # FIXME Function + # override def splice( - self, position: int, n_removals: int, additions: Sequence[GObject.Object] + self, position: int, n_removals: int, additions: Sequence[ItemType] ) -> None: ... class ListStoreClass(GObject.GPointer):