Skip to content

Commit 84323ba

Browse files
committed
Update
1 parent 191cf78 commit 84323ba

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

comtypes/client/_events.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
LPVOID,
1313
ULONG,
1414
)
15+
from typing import Any, Callable, Optional, Protocol, Type, TypeVar
1516

1617
import comtypes
1718
import comtypes.automation
1819
import comtypes.connectionpoints
1920
import comtypes.hresult
2021
import comtypes.typeinfo
22+
from comtypes import COMObject, IUnknown
2123
from comtypes.client._generate import GetModule
2224

2325
logger = logging.getLogger(__name__)
@@ -58,29 +60,46 @@ class SECURITY_ATTRIBUTES(Structure):
5860
_CloseHandle.restype = BOOL
5961

6062

63+
_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown)
64+
65+
66+
class _SupportsQueryInterface(Protocol):
67+
def QueryInterface(self, interface: Type[_T_IUnknown]) -> _T_IUnknown: ...
68+
69+
6170
class _AdviseConnection(object):
62-
def __init__(self, source, interface, receiver):
71+
def __init__(
72+
self,
73+
source: IUnknown,
74+
interface: Type[IUnknown],
75+
receiver: _SupportsQueryInterface,
76+
) -> None:
6377
self.cp = None
6478
self.cookie = None
6579
self.receiver = None
6680
self._connect(source, interface, receiver)
6781

68-
def _connect(self, source, interface, receiver):
82+
def _connect(
83+
self,
84+
source: IUnknown,
85+
interface: Type[IUnknown],
86+
receiver: _SupportsQueryInterface,
87+
) -> None:
6988
cpc = source.QueryInterface(comtypes.connectionpoints.IConnectionPointContainer)
7089
self.cp = cpc.FindConnectionPoint(ctypes.byref(interface._iid_))
7190
logger.debug("Start advise %s", interface)
7291
self.cookie = self.cp.Advise(receiver)
7392
self.receiver = receiver
7493

75-
def disconnect(self):
94+
def disconnect(self) -> None:
7695
if self.cookie:
7796
self.cp.Unadvise(self.cookie)
7897
logger.debug("Unadvised %s", self.cp)
7998
self.cp = None
8099
self.cookie = None
81100
del self.receiver
82101

83-
def __del__(self):
102+
def __del__(self) -> None:
84103
try:
85104
if self.cookie is not None:
86105
self.cp.Unadvise(self.cookie)
@@ -89,7 +108,7 @@ def __del__(self):
89108
pass
90109

91110

92-
def FindOutgoingInterface(source):
111+
def FindOutgoingInterface(source: IUnknown) -> Type[IUnknown]:
93112
"""XXX Describe the strategy that is used..."""
94113
# If the COM object implements IProvideClassInfo2, it is easy to
95114
# find the default outgoing interface.
@@ -129,7 +148,7 @@ def FindOutgoingInterface(source):
129148
raise TypeError("cannot determine source interface")
130149

131150

132-
def find_single_connection_interface(source):
151+
def find_single_connection_interface(source: IUnknown) -> Optional[Type[IUnknown]]:
133152
# Enumerate the connection interfaces. If we find a single one,
134153
# return it, if there are more, we give up since we cannot
135154
# determine which one to use.
@@ -187,11 +206,11 @@ class _SinkMethodFinder(_MethodFinder):
187206
event handlers.
188207
"""
189208

190-
def __init__(self, inst, sink):
209+
def __init__(self, inst: COMObject, sink: Any) -> None:
191210
super(_SinkMethodFinder, self).__init__(inst)
192211
self.sink = sink
193212

194-
def find_method(self, fq_name, mthname):
213+
def find_method(self, fq_name: str, mthname: str) -> Callable[..., Any]:
195214
impl = self._find_method(fq_name, mthname)
196215
# Caller of this method catches AttributeError,
197216
# so we need to be careful in the following code
@@ -206,7 +225,7 @@ def find_method(self, fq_name, mthname):
206225
except AttributeError as details:
207226
raise RuntimeError(details)
208227

209-
def _find_method(self, fq_name, mthname):
228+
def _find_method(self, fq_name: str, mthname: str) -> Callable[..., Any]:
210229
try:
211230
return super(_SinkMethodFinder, self).find_method(fq_name, mthname)
212231
except AttributeError:
@@ -216,7 +235,7 @@ def _find_method(self, fq_name, mthname):
216235
return getattr(self.sink, mthname)
217236

218237

219-
def CreateEventReceiver(interface, handler):
238+
def CreateEventReceiver(interface: Type[IUnknown], handler: Any) -> COMObject:
220239
class Sink(comtypes.COMObject):
221240
_com_interfaces_ = [interface]
222241

0 commit comments

Comments
 (0)