Skip to content

Commit 083e358

Browse files
authored
add typehints to funcs in comtypes/__init__.py (#370)
1 parent 26ad993 commit 083e358

2 files changed

Lines changed: 96 additions & 10 deletions

File tree

comtypes/__init__.py

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ def Release(self):
12981298
"""Decrease the internal refcount by one and return it."""
12991299
return self.__com_Release()
13001300

1301+
1302+
################################################################
13011303
# IPersist is a trivial interface, which allows to ask an object about
13021304
# its clsid.
13031305
class IPersist(IUnknown):
@@ -1307,16 +1309,23 @@ class IPersist(IUnknown):
13071309
COMMETHOD([], HRESULT, 'GetClassID',
13081310
( ['out'], POINTER(GUID), 'pClassID' )),
13091311
]
1312+
if TYPE_CHECKING:
1313+
# Returns the CLSID that uniquely represents an object class that
1314+
# defines the code that can manipulate the object's data.
1315+
GetClassID = hints.AnnoField() # type: Callable[[], GUID]
1316+
13101317

13111318
class IServiceProvider(IUnknown):
13121319
_iid_ = GUID('{6D5140C1-7436-11CE-8034-00AA006009FA}')
1313-
1320+
if TYPE_CHECKING:
1321+
_QueryService = hints.AnnoField() # type: Callable[[Any, Any, Any], int]
13141322
# Overridden QueryService to make it nicer to use (passing it an
13151323
# interface and it returns a pointer to that interface)
13161324
def QueryService(self, serviceIID, interface):
1325+
# type: (GUID, Type[_T_IUnknown]) -> _T_IUnknown
13171326
p = POINTER(interface)()
13181327
self._QueryService(byref(serviceIID), byref(interface._iid_), byref(p))
1319-
return p
1328+
return p # type: ignore
13201329

13211330
_methods_ = [
13221331
COMMETHOD([], HRESULT, 'QueryService',
@@ -1326,7 +1335,19 @@ def QueryService(self, serviceIID, interface):
13261335
]
13271336

13281337
################################################################
1338+
1339+
if TYPE_CHECKING:
1340+
@overload
1341+
def CoGetObject(displayname, interface): # `interface` can't be missing
1342+
# type: (str, None) -> IUnknown
1343+
pass
1344+
@overload
1345+
def CoGetObject(displayname, interface): # it should be called this way
1346+
# type: (str, Type[_T_IUnknown]) -> _T_IUnknown
1347+
pass
1348+
13291349
def CoGetObject(displayname, interface):
1350+
# type: (str, Optional[Type[IUnknown]]) -> IUnknown
13301351
"""Convert a displayname to a moniker, then bind and return the object
13311352
identified by the moniker."""
13321353
if interface is None:
@@ -1337,9 +1358,23 @@ def CoGetObject(displayname, interface):
13371358
None,
13381359
byref(interface._iid_),
13391360
byref(punk))
1340-
return punk
1361+
return punk # type: ignore
1362+
1363+
1364+
if TYPE_CHECKING:
1365+
pUnkOuter = Type[_Pointer[IUnknown]]
1366+
1367+
@overload
1368+
def CoCreateInstance(clsid, interface=None, clsctx=None, punkouter=None):
1369+
# type: (GUID, None, Optional[int], Optional[pUnkOuter]) -> IUnknown
1370+
pass
1371+
@overload
1372+
def CoCreateInstance(clsid, interface, clsctx=None, punkouter=None):
1373+
# type: (GUID, Type[_T_IUnknown], Optional[int], Optional[pUnkOuter]) -> IUnknown
1374+
pass
13411375

13421376
def CoCreateInstance(clsid, interface=None, clsctx=None, punkouter=None):
1377+
# type: (GUID, Optional[Type[IUnknown]], Optional[int], Optional[pUnkOuter]) -> IUnknown
13431378
"""The basic windows api to create a COM class object and return a
13441379
pointer to an interface.
13451380
"""
@@ -1350,9 +1385,21 @@ def CoCreateInstance(clsid, interface=None, clsctx=None, punkouter=None):
13501385
p = POINTER(interface)()
13511386
iid = interface._iid_
13521387
_ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
1353-
return p
1388+
return p # type: ignore
1389+
1390+
1391+
if TYPE_CHECKING:
1392+
@overload
1393+
def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
1394+
# type: (GUID, Optional[int], Optional[COSERVERINFO], None) -> hints.IClassFactory
1395+
pass
1396+
@overload
1397+
def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
1398+
# type: (GUID, Optional[int], Optional[COSERVERINFO], Type[_T_IUnknown]) -> _T_IUnknown
1399+
pass
13541400

13551401
def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
1402+
# type: (GUID, Optional[int], Optional[COSERVERINFO], Optional[Type[IUnknown]]) -> IUnknown
13561403
if clsctx is None:
13571404
clsctx = CLSCTX_SERVER
13581405
if interface is None:
@@ -1364,20 +1411,37 @@ def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None):
13641411
pServerInfo,
13651412
interface._iid_,
13661413
byref(p))
1367-
return p
1414+
return p # type: ignore
1415+
1416+
1417+
if TYPE_CHECKING:
1418+
@overload
1419+
def GetActiveObject(clsid, interface=None):
1420+
# type: (GUID, None) -> IUnknown
1421+
pass
1422+
@overload
1423+
def GetActiveObject(clsid, interface):
1424+
# type: (GUID, Type[_T_IUnknown]) -> _T_IUnknown
1425+
pass
13681426

13691427
def GetActiveObject(clsid, interface=None):
1428+
# type: (GUID, Optional[Type[IUnknown]]) -> IUnknown
13701429
"""Retrieves a pointer to a running object"""
13711430
p = POINTER(IUnknown)()
13721431
oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
13731432
if interface is not None:
1374-
p = p.QueryInterface(interface)
1375-
return p
1433+
p = p.QueryInterface(interface) # type: ignore
1434+
return p # type: ignore
1435+
13761436

13771437
class MULTI_QI(Structure):
13781438
_fields_ = [("pIID", POINTER(GUID)),
13791439
("pItf", POINTER(c_void_p)),
13801440
("hr", HRESULT)]
1441+
if TYPE_CHECKING:
1442+
pIID = hints.AnnoField() # type: GUID
1443+
pItf = hints.AnnoField() # type: _Pointer[c_void_p]
1444+
hr = hints.AnnoField() # type: HRESULT
13811445

13821446
class _COAUTHIDENTITY(Structure):
13831447
_fields_ = [
@@ -1410,6 +1474,11 @@ class _COSERVERINFO(Structure):
14101474
('pAuthInfo', POINTER(_COAUTHINFO)),
14111475
('dwReserved2', c_ulong),
14121476
]
1477+
if TYPE_CHECKING:
1478+
dwReserved1 = hints.AnnoField() # type: int
1479+
pwszName = hints.AnnoField() # type: Optional[str]
1480+
pAuthInfo = hints.AnnoField() # type: _COAUTHINFO
1481+
dwReserved2 = hints.AnnoField() # type: int
14131482
COSERVERINFO = _COSERVERINFO
14141483
_CoGetClassObject = _ole32.CoGetClassObject
14151484
_CoGetClassObject.argtypes = [POINTER(GUID), DWORD, POINTER(COSERVERINFO),
@@ -1468,10 +1537,24 @@ class _SOLE_AUTHENTICATION_LIST(Structure):
14681537
]
14691538
SOLE_AUTHENTICATION_LIST = _SOLE_AUTHENTICATION_LIST
14701539

1540+
1541+
if TYPE_CHECKING:
1542+
@overload
1543+
def CoCreateInstanceEx(
1544+
clsid, interface=None, clsctx=None, machine=None, pServerInfo=None):
1545+
# type: (GUID, None, Optional[int], Optional[str], Optional[COSERVERINFO]) -> IUnknown
1546+
pass
1547+
@overload
1548+
def CoCreateInstanceEx(
1549+
clsid, interface=None, clsctx=None, machine=None, pServerInfo=None):
1550+
# type: (GUID, Type[_T_IUnknown], Optional[int], Optional[str], Optional[COSERVERINFO]) -> _T_IUnknown
1551+
pass
1552+
14711553
def CoCreateInstanceEx(clsid, interface=None,
14721554
clsctx=None,
14731555
machine=None,
14741556
pServerInfo=None):
1557+
# type: (GUID, Optional[Type[IUnknown]], Optional[int], Optional[str], Optional[COSERVERINFO]) -> IUnknown
14751558
"""The basic windows api to create a COM class object and return a
14761559
pointer to an interface, possibly on another machine.
14771560
@@ -1488,19 +1571,19 @@ def CoCreateInstanceEx(clsid, interface=None,
14881571
elif machine is not None:
14891572
serverinfo = COSERVERINFO()
14901573
serverinfo.pwszName = machine
1491-
pServerInfo = byref(serverinfo)
1574+
pServerInfo = byref(serverinfo) # type: ignore
14921575

14931576
if interface is None:
14941577
interface = IUnknown
14951578
multiqi = MULTI_QI()
1496-
multiqi.pIID = pointer(interface._iid_)
1579+
multiqi.pIID = pointer(interface._iid_) # type: ignore
14971580
_ole32.CoCreateInstanceEx(byref(clsid),
14981581
None,
14991582
clsctx,
15001583
pServerInfo,
15011584
1,
15021585
byref(multiqi))
1503-
return cast(multiqi.pItf, POINTER(interface))
1586+
return cast(multiqi.pItf, POINTER(interface)) # type: ignore
15041587

15051588

15061589
################################################################

comtypes/hints.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ from typing import (
33
Type, TypeVar, Union as _UnionT,
44
)
55

6+
# symbols those what might occur recursive imports in runtime.
7+
from comtypes.server import IClassFactory as IClassFactory
8+
69

710
def AnnoField() -> Any:
811
"""**THIS IS `TYPE_CHECKING` ONLY SYMBOL.

0 commit comments

Comments
 (0)